-
Install Dependencies
pip install -r requirements.txt
-
Run the Application
python app.py
-
Access the Web Interface Open your browser and navigate to:
http://localhost:5000
The compiler accepts arithmetic expressions with the following syntax:
- Variables: Letters (a-z, A-Z), e.g.,
x,result,myVar - Numbers: Integer or decimal, e.g.,
5,3.14,60 - Operators:
+,-,*,/ - Parentheses:
(and)for grouping - Assignment:
variable = expression
-
Simple Assignment
x = a + b * 60 -
Parentheses for Precedence
result = (a + b) * (c - d) -
Constant Folding Demo
y = 5 + 3 * 2This will show optimization:
3 * 2becomes6in Phase 5 -
Complex Expression
result = (10 + 20) * 3 - 5This will show:
(10 + 20)optimized to30
Shows all variables and temporary variables used:
- Variable: Original variable name
- Identifier: Mapped identifier (id1, id2, etc.)
- Type: Either "variable" (user-defined) or "temporary" (compiler-generated)
Breaks input into tokens:
- Each token shows: TYPE, value, and position in input
Shows the Abstract Syntax Tree:
- Tree structure represents operator precedence
- Follows standard precedence:
*and/before+and- - Parentheses override precedence
Validates the expression:
- Checks for undefined variables
- Reports errors and warnings
- Shows success/failure status
Generates 3-address code:
- Each instruction has at most 3 addresses
- Format:
result = operand1 operator operand2 - Uses temporary variables (temp1, temp2, etc.)
Applies constant folding:
- Evaluates constant expressions at compile-time
- Example:
10 + 20becomes30 - Reduces runtime computation
Generates x86-64 style assembly:
- MOV: Move data between registers
- ADD, SUB, MUL, DIV: Arithmetic operations
- Register allocation shown (R0, R1, etc.)
Optimizes assembly code:
- Removes redundant MOV instructions
- Eliminates dead stores
- Peephole optimization
To run in debug mode for development:
FLASK_DEBUG=true python app.pyFor production (default):
python app.pyThe compiler can also be used via REST API:
curl -X POST http://localhost:5000/compile \
-H "Content-Type: application/json" \
-d '{"expression": "x = a + b * 60"}'Response includes all 7 phases and the symbol table in JSON format.
- Precedence: Remember that
*and/have higher precedence than+and- - Parentheses: Use parentheses to control evaluation order
- Optimization: Try expressions with constants to see optimization in action
- Symbol Table: Watch how variables are mapped to identifiers (id1, id2, etc.)
- Temporary Variables: Notice how complex expressions generate temporary variables
If port 5000 is busy, modify app.py line 118 to use a different port:
app.run(debug=debug_mode, host='0.0.0.0', port=8080)Make sure your expression follows the supported syntax:
- Variables must start with a letter
- Balanced parentheses
- Valid operators only
The application works best in modern browsers:
- Chrome/Edge (recommended)
- Firefox
- Safari
The application is fully offline:
- No internet connection required
- No CDN dependencies
- All assets are local
- Can be used on air-gapped systems