ANTLR 4 Python 3 Runtime

4.13.2 · active · verified Sat Mar 28

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator. This library provides the Python 3 runtime for lexers and parsers generated by the ANTLR 4 tool. It enables Python applications to process structured text, validate input, and build abstract syntax trees (ASTs) for executing or translating languages. The current version is 4.13.2, with releases typically tied to the main ANTLR tool's irregular but generally annual or bi-annual update cycle.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the general workflow for using the ANTLR4 Python runtime. It assumes you have already created a `.g4` grammar file (e.g., `MyGrammar.g4`) and used the ANTLR Java tool to generate `MyGrammarLexer.py`, `MyGrammarParser.py`, and `MyGrammarListener.py` (or `MyGrammarVisitor.py`) files in your project directory. The example then shows how to take an input string, tokenize it with the generated lexer, parse it with the generated parser, and print the resulting parse tree. For actual parsing, replace the dummy classes with your real generated classes and ensure the `startRule()` method matches your grammar's entry point.

import os
from antlr4 import InputStream, CommonTokenStream, ParseTreeWalker

# --- BEGIN: Dummy generated classes for demonstration ---
# In a real scenario, these would be generated by the ANTLR tool:
# java -jar antlr-4.x.x-complete.jar -Dlanguage=Python3 MyGrammar.g4

class MyGrammarLexer:
    def __init__(self, input_stream): pass
    def getAllTokens(self): return []

class MyGrammarParser:
    def __init__(self, token_stream): pass
    def startRule(self): return None # Replace 'startRule' with your grammar's entry rule

class MyGrammarListener:
    def enterEveryRule(self, ctx): pass
    def exitEveryRule(self, ctx): pass
    # ... other enter/exit methods for your grammar rules

# --- END: Dummy generated classes ---

# Assuming MyGrammar.g4 contains:
# grammar MyGrammar;
# startRule: 'hello' ID EOF;
# ID: [a-zA-Z]+;
# WS: [ \t\r\n]+ -> skip;

def parse_input(text):
    input_stream = InputStream(text)
    lexer = MyGrammarLexer(input_stream) # Use your generated Lexer class
    stream = CommonTokenStream(lexer)
    parser = MyGrammarParser(stream)    # Use your generated Parser class
    
    # Optionally, set up error handling
    # parser.removeErrorListeners()
    # parser.addErrorListener(MyCustomErrorListener())
    
    tree = parser.startRule() # Call the entry rule of your grammar
    
    # Optionally, walk the parse tree with a listener or visitor
    # walker = ParseTreeWalker()
    # listener = MyGrammarListener() # Use your generated Listener/Visitor class
    # walker.walk(listener, tree)
    
    print(f"Parse tree: {tree.toStringTree(recog=parser)}")

if __name__ == '__main__':
    # Simulate user input or a file content
    # For a real application, you might read from stdin or a file
    test_input = os.environ.get('ANTLR_TEST_INPUT', 'hello world')
    print(f"Parsing: '{test_input}'")
    parse_input(test_input)
    print("Note: In a real scenario, 'MyGrammarLexer' and 'MyGrammarParser' would be generated .py files from your .g4 grammar using the ANTLR Java tool.")

view raw JSON →