A recursive descent predictive parser (top-down, LL(1)) for arithmetic expressions in Python. Supports +, -, *, /, parentheses, unary minus, integers/floats, and proper operator precedence & left-associativity. Evaluates valid expressions and raises clear errors for invalid input.
Expression → Term { ("+" | "-") Term }* Term → Factor { ("" | "/") Factor } Factor → "(" Expression ")" | "-" Factor | Number
- No left recursion
- One-token lookahead (predictive)
- Handles division by zero
- Tokenization using regex
- Recursive descent parsing with lookahead
- On-the-fly evaluation
- Comprehensive error reporting
- Unit tests with
unittest - Sample input file & demo
predictive-parser-for-arithmetic-expression/ │ ├── parser.py # Main parser + evaluator ├── test_parser.py # Unit tests ├── expressions.txt # Sample expressions (valid & invalid) └── README.md # This file
# Run demo
python parser.py
# Run tests
python -m unittest test_parser.py -v