ANTLR4 Tools
raw JSON → 0.2.2 verified Mon Apr 27 auth: no python
Provides command-line tools (`antlr4`, `grun`) to run ANTLR4 (All-Star) and the TestRig grammar interpreter/profiler. This package is a convenience wrapper for ANTLR4, often used in conjunction with ANTLR4 runtime libraries. Current version 0.2.2, low release cadence.
pip install antlr4-tools Common errors
error ImportError: No module named 'antlr4' ↓
cause Missing ANTLR4 runtime for Python.
fix
pip install antlr4-python3-runtime error antlr4: command not found ↓
cause antlr4-tools not installed or not in PATH.
fix
Run
pip install antlr4-tools and ensure the scripts directory is in PATH. error java -jar antlr-4.13.1-complete.jar fails with 'Could not find or load main class org.antlr.v4.Tool' ↓
cause Using standalone jar without proper classpath.
fix
Use
antlr4 command from antlr4-tools or run java -jar /path/to/antlr-4.13.1-complete.jar. Warnings
deprecated antlr4-tools includes `grun` alias for `org.antlr.v4.gui.TestRig`; this is legacy and may be removed. ↓
fix Use `java org.antlr.v4.gui.TestRig` directly.
gotcha Package does not include the ANTLR4 runtime for Python. You must install `antlr4-python3-runtime` separately. ↓
fix Run `pip install antlr4-python3-runtime`.
gotcha The `antlr4` command only works if Java is installed. No Python-based ANTLR tool is provided. ↓
fix Install Java (JDK 8+).
Imports
- antlr4 wrong
import antlr4_toolscorrectfrom antlr4 import *
Quickstart
import sys
import antlr4
from antlr4 import *
# Generate parser (run separately: antlr4 -Dlanguage=Python3 MyGrammar.g4)
from MyGrammarLexer import MyGrammarLexer
from MyGrammarParser import MyGrammarParser
def main():
input_stream = FileStream(sys.argv[1])
lexer = MyGrammarLexer(input_stream)
stream = CommonTokenStream(lexer)
parser = MyGrammarParser(stream)
tree = parser.prog()
print(tree.toStringTree(recog=parser))
if __name__ == '__main__':
main()