Intermediate Code Generator for C-like Programs
An educational compiler that performs lexical analysis, syntax analysis, semantic validation, and three-address code generation for a C-like language.
ThreeAddrGen/
├── backend/ # Python Flask + PLY compiler
│ ├── app.py # Flask API server
│ └── compiler/ # Compiler pipeline modules
│ ├── lexer.py # Tokenizer (PLY)
│ ├── parser.py # LALR parser (PLY yacc)
│ ├── ast_nodes.py # AST node definitions
│ ├── symbol_table.py # Symbol table with scoping
│ ├── semantic.py # Semantic analyzer
│ ├── tac_generator.py # Three-address code gen
│ └── postfix.py # Postfix notation gen
├── frontend/ # React + TypeScript + Vite
│ └── src/
│ ├── components/ # UI components
│ ├── hooks/ # useCompiler hook
│ └── utils/ # API client
└── start.sh # Launch script
# 1. Set up Python virtual environment
cd ThreeAddrGen
python3 -m venv .venv
source .venv/bin/activate
pip install -r backend/requirements.txt
# 2. Install frontend dependencies
cd frontend && npm install && cd ..
# 3. Start both servers
./start.shOpen http://localhost:5173 in your browser.
| Phase | Description |
|---|---|
| Lexical Analysis | Tokenizes source code into keywords, identifiers, numbers, operators, delimiters |
| Syntax Analysis | Builds AST using LALR(1) parsing (PLY yacc) |
| Semantic Validation | Checks for undefined variables, redeclarations, type mismatches |
| TAC Generation | Produces three-address code with proper label/goto control flow, short-circuit boolean evaluation, and backpatching |
| Postfix Generation | Converts AST to postfix notation |
- Types:
int,float,char,bool,void - Control flow:
if/else,while,for - Operators: arithmetic, relational, logical (
&&,||,!) - Functions with parameters
- Nested blocks with scoped variables
- Comments:
//and/* */ - Short-circuit boolean evaluation in TAC
POST /api/compile— Compile source code, returns JSON with tokens, AST, TAC, symbols, errorsGET /api/samples— Returns sample C-like programs