A complete implementation of a compiler for a simple arithmetic expression language, demonstrating lexical analysis, syntactic analysis, and formal language concepts.
- Overview
- Features
- Project Structure
- Installation
- Usage
- Language Specification
- Compiler Architecture
- Examples
- Technical Details
- Contributing
- License
This project implements a mini compiler for an arithmetic expression language as part of a formal language theory demonstration. It covers the fundamental stages of compiler front-end design including:
- Lexical Analysis: Token identification using Deterministic Finite Automata (DFA)
- Syntactic Analysis: Recursive descent parsing with Context-Free Grammar (CFG)
- Grammar Analysis: Left recursion elimination, FIRST and FOLLOW sets
- Semantic Analysis: Expression validation and error detection
The project demonstrates how formal language theory provides the theoretical foundation for modern compilers and interpreters.
- β Tokenization: Identifies identifiers, numbers, operators, and parentheses
- β DFA Implementation: Deterministic Finite Automata for identifiers and numbers
- β Grammar Analysis: Original grammar with left recursion elimination
- β Recursive Descent Parser: Predictive parsing using grammar after left recursion removal
- β Parse Tree Generation: Visual representation of syntactic structure
- β Expression Validation: Accepts/rejects expressions based on language rules
- β Error Detection: Clear error messages for syntax and semantic errors
- Identifiers:
a,b,x,total1,num2 - Numbers:
12,50,1000 - Operators:
+,-,*,/ - Parentheses:
(,)
- Demonstration Mode: Shows complete compilation pipeline with examples
- Interactive Mode: Enter expressions to compile in real-time
- Command-Line Mode: Compile specific expressions directly
mini_compiler/
βββ main.py # Entry point and demonstration
βββ lexer/
β βββ **init**.py
β βββ token.py # Token classes and enums
β βββ lexer.py # Lexical analyzer with DFA
βββ parser/
β βββ **init**.py
β βββ ast.py # Abstract Syntax Tree nodes
β βββ parser.py # Recursive descent parser
βββ analyzer/
β βββ **init**.py
β βββ grammar.py # Grammar analysis utilities
β βββ validator.py # Expression validation
βββ utils/
βββ **init**.py
βββ printer.py # Pretty printing utilities
- Python 3.6 or higher
- No external dependencies required
- Clone the repository:
git clone link
cd mini-compiler- Verify installation:
python main.py --helppython main.pyThis runs a comprehensive demonstration with:
- All test expressions
- Grammar analysis
- DFA diagrams
- Parse tree visualization
python main.py "a+b*c"
python main.py "(a+b)*(c+d)"
python main.py "100+25*2"python main.py
# Then type 'y' when prompted for interactive mode
# Enter expressions one by onefrom lexer import Lexer
from parser import Parser
# Lexical analysis
lexer = Lexer("a+b*c")
tokens = lexer.tokenize()
# Syntactic analysis
parser = Parser(tokens)
ast = parser.parse()E -> T E'
E' -> + T E' | - T E' | Ξ΅
T -> F T'
T' -> * F T' | / F T' | Ξ΅
F -> (E) | id | num
| Token Type | Pattern | Example |
|---|---|---|
| IDENTIFIER | letter(letter|digit)* | a, b, x1 |
| NUMBER | digit+ | 12, 50, 1000 |
| PLUS | + | + |
| MINUS | - | - |
| MULT | * | * |
| DIV | / | / |
| LPAREN | ( | ( |
| RPAREN | ) | ) |
a+ba*b+c(a+b)*cx+y*z(a+b)*(c+d)100+25num1*value2
+a(starts with operator)a+(ends with operator)a**(invalid operator sequence)(a+b(unbalanced parentheses)a+b)(unbalanced parentheses)*/ab(invalid operator sequence)a+*b(invalid operator sequence)
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β SOURCE PROGRAM β
β (a+b*c, x+y*z, etc.) β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β LEXICAL ANALYZER β
β (Scanner/Tokenizer) β
β Identifies: id, num, +, -, *, /, (, ) β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β TOKEN STREAM β
β [id, +, id, *, id, EOF] β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β SYNTAX ANALYZER β
β (Parser) β
β Recursive Descent Parsing β
β Grammar: E -> T E' β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β PARSE TREE β
β (AST) β
β Represents the syntactic structure β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β SEMANTIC ANALYSIS β
β (Optional) β
β Type checking, validation β
βββββββββββββββββββββββββββββββββββββββββββββββββββ
Token Stream:
Token(IDENTIFIER, 'a', line=1, col=1)
Token(PLUS, '+', line=1, col=2)
Token(IDENTIFIER, 'b', line=1, col=3)
Token(MULT, '*', line=1, col=4)
Token(IDENTIFIER, 'c', line=1, col=5)
Token(EOF, 'EOF', line=1, col=6)
Parse Tree:
E
βββ T
β βββ F
β β βββ a
β βββ T'
β βββ Ξ΅
βββ E'
βββ '+'
βββ T
β βββ F
β β βββ b
β βββ T'
β βββ '*'
β βββ F
β β βββ c
β βββ T'
β βββ Ξ΅
βββ E'
βββ Ξ΅
E
=> T E'
=> F T' E'
=> id T' E'
=> a E'
=> a + T E'
=> a + F T' E'
=> a + b T' E'
=> a + b * F T' E'
=> a + b * c
E
=> T E'
=> T + T E'
=> T + T
=> T + F T'
=> T + b T'
=> T + b * F
=> T + b * c
=> a + b * c
State q0 = Start
State q1 = Accept
q0 --letter--> q1
q1 --letter/digit--> q1
State q0 = Start
State q1 = Accept
q0 --digit--> q1
q1 --digit--> q1
| Symbol | FIRST | FOLLOW |
|---|---|---|
| E | {(, id, num} | {), $} |
| T | {(, id, num} | {+, -, ), $} |
| F | {(, id, num} | {*, /, +, -, ), $} |
| E' | {+, -, Ξ΅} | {), $} |
| T' | {*, /, Ξ΅} | {+, -, ), $} |
- Highest: Parentheses
( ) - Medium: Multiplication
*, Division/ - Lowest: Addition
+, Subtraction-
The project includes a comprehensive test suite with both accepted and rejected expressions:
Accepted:
test_expressions = [
"a+b",
"a*b+c",
"(a+b)*c",
"x+y*z",
"(a+b)*(c+d)",
"100+25",
"num1*value2"
]Rejected:
test_expressions = [
"+a", # Starts with operator
"a+", # Ends with operator
"a**", # Invalid operator sequence
"(a+b", # Unbalanced parentheses
"a+b)", # Unbalanced parentheses
"*/ab", # Invalid operator sequence
"a+*b" # Invalid operator sequence
]# Run all tests
python main.py
# Test specific expression
python main.py "a+b*c"This project demonstrates:
- Formal Language Theory in practice
- Compiler Design fundamentals
- Automata Theory (DFA implementation)
- Grammar Theory (left recursion elimination)
- Parsing Techniques (recursive descent)
- Software Engineering principles (modular design)
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Add support for more operators (e.g.,
%,^) - Implement semantic analysis (type checking)
- Add code generation (e.g., generate Python bytecode)
- Implement error recovery in the parser
- Add unit tests
- Create a web interface
This project is licensed under the MIT License - see the LICENSE file for details.
- Based on the project requirements from the Faculty of Engineering and Technology, Department of Computer Engineering
- Inspired by the formal language theory concepts from:
- "Compilers: Principles, Techniques and Tools" by Aho, Lam, Sethi, and Ullman
- "Introduction to Automata Theory, Languages and Computation" by Hopcroft, Motwani, and Ullman
For questions or feedback, please contact:
- Project Lead: [SIAHA TOUKO AUBIN]
- Email: [[email protected]]
- Department: Department of Computer Engineering
- Faculty: Faculty of Engineering and Technology
- A. V. Aho, M. Lam, R. Sethi, and J. Ullman, Compilers: Principles, Techniques and Tools, 2nd Edition.
- J. Hopcroft, R. Motwani, and J. Ullman, Introduction to Automata Theory, Languages and Computation.
- K. Louden, Compiler Construction: Principles and Practice.
- M. Sipser, Introduction to the Theory of Computation.
# Clone the repository
git clone [Repo_link]
cd mini-compiler
# Run the demonstration
python main.py
# Compile an expression
python main.py "a+b*c"
