A command-line static analysis tool for a subset of the Go programming language. This project implements a compiler front-end pipeline extended with control-flow modeling, program analysis, and visualization.
This project goes beyond a traditional compiler assignment by combining:
- Compiler construction (lexing โ parsing โ AST)
- Semantic analysis (type + scope checking)
- Control Flow Graph (CFG) generation
- Static analysis on program structure
- Graph-based visualization of programs
The system processes Go-like source code through multiple stages and enables both structural and analytical understanding of programs.
-
Lexical Analysis (Tokenization using PLY)
-
LALR Parsing (PLY Yacc)
-
Abstract Syntax Tree (AST) construction
-
Semantic analysis:
- Scope resolution
- Type checking
-
Builds CFG from AST
-
Supports:
- Sequential flow
- Conditional branching (
if / else) - Loops (
for) - Multi-branch control (
switch-case-default)
-
Explicit representation of:
- Entry and exit nodes
- Loop back edges
- Merge points
-
Detection of:
- Undeclared variables
- Redeclaration errors
- Type mismatches
- Dead/unreachable code
- Unused variables
-
Scope-aware analysis using symbol tables
-
AST Visualization
- Tree representation of program structure
-
CFG Visualization
-
Graph representation of execution flow
-
Color-coded nodes:
- ๐ข Entry/Exit
- ๐ Conditions
- ๐ฃ Switch
- โช Merge points
- ๐ต Statements
-
.
โโโ samples/
โ โโโ sample.go
โโโ src/
โ โโโ ast_nodes.py
โ โโโ lexer.py
โ โโโ parser.py
โ โโโ semantic.py
โ โโโ cfg.py
โ โโโ analysis.py
โ โโโ visualize.py
โ โโโ main.py
โ โโโ parsetab.py
โโโ README.md
โโโ requirements.txt
- Implemented using PLY (Lex)
- Converts source code into tokens
- Built using LALR parsing (PLY Yacc)
- Generates AST from grammar rules
-
Hierarchical program representation
-
Node types include:
VarDecl,Assign,BinaryOp,If,ForLoop,Switch
-
Used as input for semantic and CFG stages
-
Stack-based symbol table
-
Handles:
- Variable declaration and lookup
- Nested scopes
- Type validation
-
Transforms AST โ Control Flow Graph
-
Each node = Basic Block
-
Captures:
- Execution paths
- Branching
- Loop cycles
-
Runs analyses on AST + CFG
-
Enables program-level reasoning:
- Reachability
- Usage tracking
var x = 2
for x < 5 {
x = x + 1
}
switch x {
case 5:
x = x + 2
default:
x = x + 3
}ENTRY โ VarDecl โ LOOP_COND
โ โ
BODY โโโโโโโ
โ
AFTER_LOOP โ SWITCH
โ โ
CASE DEFAULT
โ โ
MERGE โ EXIT
pip install -r requirements.txtpython src/main.py samples/sample.go --astpython src/main.py samples/sample.go --cfgpython src/main.py samples/sample.go --viz-cfgpython src/main.py samples/sample.go --analyze- PLY (Lex/Yacc) for explicit grammar control and learning parsing internals
- AST-first architecture for separation of concerns
- CFG-based analysis to enable graph-level reasoning
- Modular design (lexer, parser, semantic, cfg, analysis, visualize)
- Supports only a subset of Go
- No functions, structs, or advanced types
- Basic type system (primarily integers)
- No optimization or code generation
- Visualization uses force-directed layout (not hierarchical)
- Data-flow analysis (live variable analysis)
- Intermediate Representation (IR)
- SSA form
- Optimization passes
- Function support
- Better CFG layout (Graphviz-based)
- IDE / VS Code extension
- Python
- PLY (Lex/Yacc)
- NetworkX (graph modeling)
- Matplotlib (visualization)
This project evolves a traditional compiler pipeline into a graph-based static analysis system, combining:
- Compiler fundamentals
- Graph theory
- Program analysis
๐ Suitable for systems, compilers, and program analysis exploration.