# ANTLR — Powerful Parser Generator for Structured Text > ANTLR (ANother Tool for Language Recognition) generates parsers in Java, Python, C#, JavaScript, Go, C++, Swift, and Dart from formal grammars. It is the standard tool for building language processors, DSLs, and code analysis tools. ## Install Save as a script file and run: # ANTLR — Powerful Parser Generator for Structured Text ## Quick Use ```bash # Install via pip (Python target) pip install antlr4-tools antlr4-python3-runtime # Or download the jar for Java target curl -O https://www.antlr.org/download/antlr-4.13.2-complete.jar # Generate parser from a grammar antlr4 MyGrammar.g4 ``` ## Introduction ANTLR (ANother Tool for Language Recognition) is a parser generator that reads, processes, and translates structured text. Created by Terence Parr at the University of San Francisco, it has become the de facto standard for building parsers, interpreters, compilers, and domain-specific languages across the software industry. ## What ANTLR Does - Generates lexers and parsers from ANTLR grammar files (.g4) for multiple target languages - Produces parse trees that can be walked with listeners or visitors - Supports context-free grammars with adaptive LL(*) parsing (ALL(*)) - Ships with a large repository of pre-built grammars for common languages (SQL, JSON, Java, etc.) - Provides interactive debugging and visualization of parse trees ## Architecture Overview ANTLR takes a grammar specification written in its own notation and generates source code for a lexer and parser in your chosen target language. The ALL(*) parsing algorithm handles ambiguity dynamically at runtime, making it more flexible than traditional LL or LR parsers. Generated parsers produce concrete parse trees that applications traverse using either the listener pattern (event-driven callbacks) or the visitor pattern (explicit tree traversal). ## Self-Hosting & Configuration - Install the ANTLR tool via pip (`antlr4-tools`), Homebrew, or by downloading the Java jar directly - Runtime libraries are available for each target language (e.g., `antlr4-python3-runtime` for Python) - Grammar files (.g4) define both lexer and parser rules in a single file or split across two - Use the `antlr4` command to generate source code, specifying the target language with `-Dlanguage=Python3` - Integrate into build systems via Maven/Gradle plugins, or invoke the jar in CI pipelines ## Key Features - Multi-language code generation: Java, Python, C#, JavaScript/TypeScript, Go, C++, Swift, Dart - Adaptive LL(*) parsing handles complex grammars without manual disambiguation - Grammars-v4 repository provides 200+ ready-to-use grammars for popular languages and formats - Tree walkers via listeners and visitors simplify AST processing - Rich IDE support with plugins for IntelliJ, VS Code, and Eclipse ## Comparison with Similar Tools - **Yacc/Bison** — generates LALR parsers in C/C++; more manual, less portable across languages - **PEG.js / Peggy** — PEG-based parsing for JavaScript; simpler but less powerful for ambiguous grammars - **Tree-sitter** — incremental parsing for editors; optimized for syntax highlighting, not general-purpose transformation - **LALRPOP** — Rust-specific LR parser generator; good for Rust projects but single-language - **Lark** — Python-only parser using Earley/LALR; easier for Python-only use cases ## FAQ **Q: Which target language should I choose?** A: Pick whatever language your project uses. Java and Python have the most mature runtimes, but all targets are production-ready. **Q: Can ANTLR handle left-recursive grammars?** A: Yes. ANTLR 4 supports direct left recursion and automatically rewrites it internally. **Q: How does performance compare to hand-written parsers?** A: Generated parsers are fast enough for most use cases. For extremely hot paths, a hand-written recursive-descent parser may be faster, but ANTLR's development speed advantage usually outweighs the difference. **Q: Is there a grammar for my language?** A: Check the grammars-v4 repository on GitHub — it has grammars for SQL dialects, JSON, XML, Java, Python, C, and many more. ## Sources - https://github.com/antlr/antlr4 - https://www.antlr.org/ --- Source: https://tokrepo.com/en/workflows/asset-ee8b07fb Author: Script Depot