A compiler front-end for a subset of the Go programming language, extended with control flow graph (CFG) generation, static analysis, and program visualization.
The project implements the complete front-end compilation pipeline—from lexical analysis to semantic analysis—and provides tools for understanding program structure and execution flow.
The compiler processes Go-like source code through multiple stages:
- Lexical Analysis
- Parsing
- Abstract Syntax Tree (AST) construction
- Semantic Analysis
- Control Flow Graph (CFG) generation
- Static Analysis
- AST and CFG Visualization
The modular architecture separates each compilation stage, making the project easy to extend and experiment with.
- Lexical analysis using PLY (Lex)
- LALR parsing using PLY (Yacc)
- Abstract Syntax Tree (AST) construction
- Semantic analysis with:
- Scope resolution
- Symbol table management
- Static type checking
Generates CFGs directly from the AST with support for:
- Sequential execution
- Conditional branching (
if / else) - Loops (
for) - Multi-way branching (
switch) - Entry and exit nodes
- Merge points and loop back edges
Performs compile-time analysis including:
- Undeclared variable detection
- Redeclaration detection
- Type mismatch detection
- Unused variable analysis
- Dead / unreachable code detection
Provides graphical representations of:
- Abstract Syntax Tree (AST)
- Control Flow Graph (CFG)
CFG nodes are categorized for improved readability:
- Entry / Exit
- Statements
- Conditions
- Switch nodes
- Merge points
Source Code
│
▼
Lexer
│
▼
Parser
│
▼
AST
│
┌────┴────┐
▼ ▼
Semantic CFG
Analysis Generation
│ │
└────┬────┘
▼
Static Analysis
│
▼
Visualization
.
├── samples/
├── src/
│ ├── lexer.py
│ ├── parser.py
│ ├── ast_nodes.py
│ ├── semantic.py
│ ├── cfg.py
│ ├── analysis.py
│ ├── visualize.py
│ └── main.py
├── README.md
└── requirements.txt
var x = 2
for x < 5 {
x = x + 1
}
switch x {
case 5:
x = x + 2
default:
x = x + 3
}ENTRY
│
VarDecl
│
Loop Condition
├─────────────┐
▼ │
Loop Body──────┘
│
Switch
├──────┐
▼ ▼
Case Default
└──┬──┘
▼
EXIT
Install dependencies
pip install -r requirements.txtGenerate the AST
python src/main.py samples/sample.go --astGenerate the CFG
python src/main.py samples/sample.go --cfgVisualize the CFG
python src/main.py samples/sample.go --viz-cfgRun static analysis
python src/main.py samples/sample.go --analyze- PLY (Lex/Yacc) for explicit lexer and parser implementation.
- AST-first architecture to decouple parsing from later compilation stages.
- CFG generation to enable program-level reasoning.
- Modular components to simplify future extensions.
- Supports a subset of the Go language.
- Limited type system.
- No functions or user-defined types.
- No intermediate representation (IR).
- No optimization or code generation.
- Function support
- Intermediate Representation (IR)
- Static Single Assignment (SSA)
- Data-flow analysis
- Optimization passes
- Graphviz-based visualization
- IDE integration
- Python
- PLY (Lex/Yacc)
- NetworkX
- Matplotlib
This project helped me understand:
- Compiler front-end design
- Parsing and semantic analysis
- Symbol table implementation
- Control Flow Graph construction
- Static program analysis
- Graph-based program visualization