Scripts2026年5月27日·1 分钟阅读

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.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
ANTLR Parser Generator
先审查命令
npx -y tokrepo@latest install ee8b07fb-5a09-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产