Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Subset Compiler & Static Analysis Engine

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.


Overview

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.


Features

Compiler Pipeline

  • 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

Control Flow Graph Generation

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

Static Analysis

Performs compile-time analysis including:

  • Undeclared variable detection
  • Redeclaration detection
  • Type mismatch detection
  • Unused variable analysis
  • Dead / unreachable code detection

Visualization

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

Architecture

Source Code
      │
      ▼
 Lexer
      │
      ▼
 Parser
      │
      ▼
     AST
      │
 ┌────┴────┐
 ▼         ▼
Semantic   CFG
Analysis   Generation
 │         │
 └────┬────┘
      ▼
 Static Analysis
      │
      ▼
 Visualization

Project Structure

.
├── samples/
├── src/
│   ├── lexer.py
│   ├── parser.py
│   ├── ast_nodes.py
│   ├── semantic.py
│   ├── cfg.py
│   ├── analysis.py
│   ├── visualize.py
│   └── main.py
├── README.md
└── requirements.txt

Example

Input

var x = 2

for x < 5 {
    x = x + 1
}

switch x {
case 5:
    x = x + 2
default:
    x = x + 3
}

Control Flow Graph

ENTRY
  │
VarDecl
  │
Loop Condition
 ├─────────────┐
 ▼             │
Loop Body──────┘
  │
Switch
 ├──────┐
 ▼      ▼
Case  Default
  └──┬──┘
     ▼
   EXIT

Running the Project

Install dependencies

pip install -r requirements.txt

Generate the AST

python src/main.py samples/sample.go --ast

Generate the CFG

python src/main.py samples/sample.go --cfg

Visualize the CFG

python src/main.py samples/sample.go --viz-cfg

Run static analysis

python src/main.py samples/sample.go --analyze

Design Decisions

  • 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.

Current Limitations

  • 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.

Future Work

  • Function support
  • Intermediate Representation (IR)
  • Static Single Assignment (SSA)
  • Data-flow analysis
  • Optimization passes
  • Graphviz-based visualization
  • IDE integration

Tech Stack

  • Python
  • PLY (Lex/Yacc)
  • NetworkX
  • Matplotlib

Learning Outcomes

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

About

Compiler front-end + CFG-based static analysis tool for a subset of Go with visualization

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages