Another Tool for Language Recognition - Python Edition
A Python implementation of the next generation ANTLR parser generator tool. This project ports the TypeScript antlr-ng tool to Python, providing a native Python experience while maintaining full compatibility with the original implementation.
- Full ANTLR4 Grammar Support - Process any ANTLR4 grammar file
- Multiple Target Languages - Generate parsers for TypeScript, Java, C++, C#, Go, Python3, Dart, Swift, JavaScript, PHP
- Advanced Parsing - Adaptive LL(*) parsing algorithms
- Grammar Analysis - Built-in grammar validation and optimization
- Template-based Code Generation - Flexible output generation
- Python-Native - Idiomatic Python implementation with type hints
# Install from PyPI (when published)
pip install antlr-ng-python
# Install from source
git clone https://github.com/djdax/antlr-ng-python.git
cd antlr-ng-python
pip install -e .# Basic usage
antlr-ng MyGrammar.g4
# Generate Python3 target
antlr-ng -Dlanguage=Python3 MyGrammar.g4
# Specify output directory
antlr-ng -o ./generated MyGrammar.g4
# Generate with listener and visitor
antlr-ng --generate-listener --generate-visitor MyGrammar.g4
# Show help
antlr-ng --helpfrom antlr_ng import Tool
# Create and configure tool
tool = Tool()
# Load and process grammar
grammar = tool.load_grammar("MyGrammar.g4")
tool.process(grammar, gen_code=True)
# Access generated files in output directory# Test grammar with TestRig
testrig MyGrammar startRule input.txt
# Interactive interpreter
interpreter MyGrammar.g4 startRuleThis is a work-in-progress Python port of the antlr-ng tool. Current status:
- Project structure and packaging
- CLI interface with click
- Basic Tool class architecture
- Parameter parsing and validation
- Test framework setup
- Grammar parsing with ANTLR4 Python runtime
- AST construction and manipulation
- Code generation pipeline
- Target language templates
- Error reporting system
- Full feature parity with TypeScript version
- Performance optimizations
- Comprehensive test suite
- Documentation and examples
# Clone the repository
git clone https://github.com/djdax/antlr-ng-python.git
cd antlr-ng-python
# Set up development environment
python setup_dev.py
# Run tests
pytest
# Format code
black src/ tests/
# Type checking
mypy src/Check out the examples/ directory for sample grammars and usage patterns:
simple_grammar.g4- Basic arithmetic grammarusage_example.py- Python API demonstration
Contributions are welcome! This project aims to:
- Maintain API compatibility with the original TypeScript version
- Follow Python best practices and conventions
- Provide comprehensive type hints and documentation
- Ensure cross-platform compatibility
MIT License - see LICENSE.txt for details.
- antlr-ng - Original TypeScript implementation
- ANTLR4 - The original ANTLR parser generator
- antlr4-python3-runtime - ANTLR4 Python runtime
This Python implementation is part of the ANTLR Next Generation Project, which modernizes the ANTLR toolchain by:
- Separating the tool from target runtimes
- Improving maintainability and development velocity
- Adding new features while preserving compatibility
- Supporting modern development workflows
Note: This is an independent Python port and is not officially affiliated with the ANTLR project, though it aims to maintain compatibility and contribute to the ANTLR ecosystem.