Skip to content

aarushisingh236/go-syntax-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

11 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Go Language Analyzer (Compiler + Static Analysis Engine)

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.


๐Ÿ“Œ Overview

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.


๐Ÿš€ Features

๐Ÿ”ง Compiler Pipeline

  • Lexical Analysis (Tokenization using PLY)

  • LALR Parsing (PLY Yacc)

  • Abstract Syntax Tree (AST) construction

  • Semantic analysis:

    • Scope resolution
    • Type checking

๐Ÿ”€ Control Flow Graph (CFG)

  • 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

๐Ÿ” Static Analysis

  • Detection of:

    • Undeclared variables
    • Redeclaration errors
    • Type mismatches
    • Dead/unreachable code
    • Unused variables
  • Scope-aware analysis using symbol tables


๐Ÿ“Š Visualization

  • AST Visualization

    • Tree representation of program structure
  • CFG Visualization

    • Graph representation of execution flow

    • Color-coded nodes:

      • ๐ŸŸข Entry/Exit
      • ๐ŸŸ  Conditions
      • ๐ŸŸฃ Switch
      • โšช Merge points
      • ๐Ÿ”ต Statements

๐Ÿ—๏ธ Project Structure

.
โ”œโ”€โ”€ 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

โš™๏ธ Architecture

1. Lexer

  • Implemented using PLY (Lex)
  • Converts source code into tokens

2. Parser

  • Built using LALR parsing (PLY Yacc)
  • Generates AST from grammar rules

3. AST (Abstract Syntax Tree)

  • Hierarchical program representation

  • Node types include:

    • VarDecl, Assign, BinaryOp, If, ForLoop, Switch
  • Used as input for semantic and CFG stages


4. Semantic Analysis

  • Stack-based symbol table

  • Handles:

    • Variable declaration and lookup
    • Nested scopes
    • Type validation

5. CFG Generation

  • Transforms AST โ†’ Control Flow Graph

  • Each node = Basic Block

  • Captures:

    • Execution paths
    • Branching
    • Loop cycles

6. Static Analysis Engine

  • Runs analyses on AST + CFG

  • Enables program-level reasoning:

    • Reachability
    • Usage tracking

๐Ÿงช Example

Input

var x = 2

for x < 5 {
    x = x + 1
}

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

CFG Output (conceptual)

ENTRY โ†’ VarDecl โ†’ LOOP_COND
           โ†“        โ†˜
         BODY โ†โ”€โ”€โ”€โ”€โ”€โ”˜
           โ†“
       AFTER_LOOP โ†’ SWITCH
                    โ†™   โ†˜
                 CASE  DEFAULT
                    โ†“      โ†“
                   MERGE โ†’ EXIT

โ–ถ๏ธ How to Run

Install dependencies

pip install -r requirements.txt

Run AST view

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

Run CFG view

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

Visualize 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 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)

โš ๏ธ Limitations

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

๐Ÿ”ฎ Future Improvements

  • Data-flow analysis (live variable analysis)
  • Intermediate Representation (IR)
  • SSA form
  • Optimization passes
  • Function support
  • Better CFG layout (Graphviz-based)
  • IDE / VS Code extension

๐Ÿ› ๏ธ Tech Stack

  • Python
  • PLY (Lex/Yacc)
  • NetworkX (graph modeling)
  • Matplotlib (visualization)

๐Ÿ† Summary

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.

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages