Esta página se muestra en inglés. Una traducción al español está en curso.
ScriptsMay 27, 2026·3 min de lectura

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.

Listo para agents

Instalación con revisión previa

Este activo requiere revisión. El prompt copiado pide dry-run, muestra escrituras y continúa solo tras confirmación.

Needs Confirmation · 64/100Política: confirmar
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
ANTLR Parser Generator
Comando con revisión previa
npx -y tokrepo@latest install ee8b07fb-5a09-11f1-9bc6-00163e2b0d79 --target codex

Primero dry-run, confirma las escrituras y luego ejecuta este comando.

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

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados