Skip to content

A recursive descent LL(1) parser in Python that validates & evaluates expressions with +, -, *, /, parentheses, and unary minus. Handles precedence, associativity, division by zero. Includes tokenizer, error handling, unit tests, and demo. Ideal for compiler design mini-project.

Notifications You must be signed in to change notification settings

ShafTheStud/predictive-parser-for-arithmetic-expression

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Predictive Parser for Arithmetic Expressions

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.


Grammar (LL(1) Compatible)

Expression → Term { ("+" | "-") Term }* Term → Factor { ("" | "/") Factor } Factor → "(" Expression ")" | "-" Factor | Number

  • No left recursion
  • One-token lookahead (predictive)
  • Handles division by zero

Features

  • Tokenization using regex
  • Recursive descent parsing with lookahead
  • On-the-fly evaluation
  • Comprehensive error reporting
  • Unit tests with unittest
  • Sample input file & demo

Project Structure

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


How to Run

# Run demo
python parser.py

# Run tests
python -m unittest test_parser.py -v

About

A recursive descent LL(1) parser in Python that validates & evaluates expressions with +, -, *, /, parentheses, and unary minus. Handles precedence, associativity, division by zero. Includes tokenizer, error handling, unit tests, and demo. Ideal for compiler design mini-project.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages