Trying to get TypeScript grammar running in C#

15 views
Skip to first unread message

Steve Naughton

unread,
Jan 22, 2026, 1:09:21 AM (9 days ago) Jan 22
to antlr-discussion
Hi all, 

I downloaded TypeScript lexer and parser grammars from https://github.com/antlr/grammars-v4/tree/master/javascript/typescript, and ran the java command 

> java -jar antlr-4.13.2-complete.jar  -Dlanguage=CSharp TypeScriptLexer.g4 TypeScriptParser.g4 

But the code it generates has a few errors, mainly that classes TypeScriptLexerBase and TypeScriptParserBase do not exist.

What's going on here, is the grammar an old version or does it have problems?

TIA


Mike Lischke

unread,
Jan 22, 2026, 3:16:16 AM (9 days ago) Jan 22
to antlr-di...@googlegroups.com

Ken Domino

unread,
Jan 22, 2026, 5:46:32 AM (9 days ago) Jan 22
to antlr-discussion
Hi Steve. When using a grammar in grammars-v4, please first check the instructions for testing: https://github.com/antlr/grammars-v4/wiki#how-do-i-test-the-grammars. And, check the desc.xml (https://github.com/antlr/grammars-v4/blob/master/javascript/typescript/desc.xml). The "<targets>" element indicates whether a port exists for the grammar. Fortunately, there is one for CSharp. Sometimes people save incomplete work, or the port does work, but it is impractical because it is too slow to test. For CSharp, ideally, you use the Antlr4BuildTasks package ( https://www.nuget.org/packages/Antlr4BuildTasks ; https://github.com/kaby76/Antlr4BuildTasks?tab=readme-ov-file#new-approach ). That removes you from installing the prerequisites, e.g., downloading the Antrl Tool JAR, downloading and installing Java (you may have multiple installations), or manually working out the build dependencies. "About: Grammars written for ANTLR v4; expectation that the grammars are free of actions." Grammars in the repo are free of semantics, except when the grammar is not context-free, i.e., ambiguous. In those cases, the grammar uses semantics strictly to disambiguate the CFG, implemented via Antlr4 actions written in a given target language, such as CSharp. These are implemented as "semantic predicates" and state-altering "actions" in the .g4. The Typescript grammar is one of those. That is why the base classes are required for the application.  If you have questions, you can ask on https://github.com/antlr/grammars-v4/issues. --Ken
Reply all
Reply to author
Forward
0 new messages