Linux command
antlr 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Generate parser
antlr4 [Grammar.g4]
Generate parser
antlr4 -Dlanguage=[Python3] [Grammar.g4]
Generate parser with visitor
antlr4 -visitor [Grammar.g4]
Specify output directory
antlr4 -o [output/dir] [Grammar.g4]
Generate parser
antlr4 -package [com.example.parser] [Grammar.g4]
Generate both listener and visitor
antlr4 -listener -visitor [Grammar.g4]
Generate parser
antlr4 -lib [path/to/imports] [Grammar.g4]
说明
ANTLR (ANother Tool for Language Recognition) is a parser generator that creates lexers, parsers, and tree walkers from grammar files. It generates code that can read, process, and translate structured text or binary data. ANTLR grammars use an extended BNF notation with semantic actions. The tool processes .g4 grammar files and generates source code for the target language. Generated parsers build parse trees that can be traversed using listener or visitor patterns. The grammar defines both lexical rules (tokens) and parser rules. ANTLR handles left-recursive rules automatically and supports features like semantic predicates, actions, and grammar imports. The generated parser uses an adaptive LL(*) parsing algorithm called ALL(*). Common workflow: write a grammar file, run antlr4 to generate code, compile the generated code with your application, and use the parser to process input text.
参数
- -o _directory_
- Output directory for generated files (default: current directory)
- -lib _directory_
- Location to search for imported grammars and token files
- -Dlanguage=_lang_
- Target language: Java (default), Python3, CSharp, JavaScript, TypeScript, Go, Cpp, Swift, Dart, PHP. Use -D_option_=_value_ to override any grammar-level option.
- -package _name_
- Package or namespace for generated code
- -listener
- Generate parse tree listener classes (default)
- -no-listener
- Do not generate parse tree listener
- -visitor
- Generate parse tree visitor classes
- -no-visitor
- Do not generate parse tree visitor (default)
- -encoding _name_
- Grammar file encoding (default: UTF-8)
- -atn
- Generate DOT graph files for ATN visualization
- -depend
- Generate file dependency information
- -message-format _format_
- Output message format: antlr, gnu, vs2005
- -long-messages
- Show detailed exception information for errors
- -Werror
- Treat warnings as errors
- -Xlog
- Create detailed log file (antlr-timestamp.log)
- -Xexact-output-dir
- Place all output directly into -o directory regardless of paths or package structure
- -XdbgST
- Launch StringTemplate visualizer on generated code
FAQ
What is the antlr command used for?
ANTLR (ANother Tool for Language Recognition) is a parser generator that creates lexers, parsers, and tree walkers from grammar files. It generates code that can read, process, and translate structured text or binary data. ANTLR grammars use an extended BNF notation with semantic actions. The tool processes .g4 grammar files and generates source code for the target language. Generated parsers build parse trees that can be traversed using listener or visitor patterns. The grammar defines both lexical rules (tokens) and parser rules. ANTLR handles left-recursive rules automatically and supports features like semantic predicates, actions, and grammar imports. The generated parser uses an adaptive LL(*) parsing algorithm called ALL(*). Common workflow: write a grammar file, run antlr4 to generate code, compile the generated code with your application, and use the parser to process input text.
How do I run a basic antlr example?
Run `antlr4 [Grammar.g4]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -o _directory_ do in antlr?
Output directory for generated files (default: current directory)