Skip to content

KaushikBalaji05/Mini-Java-Syntax-Analyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mini Java Compiler Front-End

A Mini Java Compiler Front-End built in Python using PLY (Python Lex-Yacc).

This project performs multiple compiler front-end phases:

Source Code → Lexical Analysis → Syntax Analysis → Abstract Syntax Tree (AST) Generation → Semantic Analysis → Error Reporting

The goal of this project is to understand how real compilers process source code by implementing key compiler-design concepts from scratch.


Features

Lexical Analysis

  • Tokenization using PLY Lex
  • Recognition of keywords, identifiers, literals, operators, and delimiters

Syntax Analysis

  • LALR(1) parser implemented using PLY Yacc
  • Grammar-based validation of Mini Java constructs
  • Error recovery with multiple syntax error reporting

Abstract Syntax Tree (AST)

  • Generates an AST for valid source code
  • Pretty-prints the AST in a tree-like structure
  • Preserves source line information for diagnostics

Semantic Analysis

  • Symbol table implementation with nested scopes
  • Semantic validation after parsing
  • Separate semantic error reporting

Supported Language Constructs

Enums

enum Color {
    RED,
    GREEN,
    BLUE
}

Generic Classes

public class Container<T> {
}

Method Declarations

public int add(int a, int b) {
    return a;
}

Method Overloading Detection

public int add(int a) {
    return a;
}

public int add(int a) {
    return a;
}

Duplicate definitions are detected and reported.

Lambda Expressions

var handler = (int x) -> {
    return;
};

Exception Handling

try {
    count = 10;
}
catch(Exception e) {
    return;
}

Semantic Checks

The semantic analyzer performs:

Duplicate Declaration Detection

  • Duplicate class names
  • Duplicate enum names
  • Duplicate parameter names
  • Duplicate variable declarations

Scope Analysis

  • Global scope
  • Class scope
  • Method scope
  • Lambda scope
  • Try-Catch scope

Undefined Symbol Detection

  • Undeclared variable usage
  • Assignment to undeclared variables
  • Calls to undefined methods

Type Checking

Detects type mismatches such as:

int x;
x = "hello";

Variable Shadowing Warnings

Warns when an inner-scope variable hides an outer-scope declaration.


Compiler Pipeline

Source Code
    ↓
Lexer
    ↓
Parser
    ↓
AST Generation
    ↓
Semantic Analysis
    ↓
Error Summary

Project Structure

Mini-Java-Syntax-Analyzer/
│
├── ast_nodes.py
├── lexer.py
├── parser.py
├── semantic_analyzer.py
├── main.py
│
├── test_input/
│   ├── Container.java
│   ├── ErrorTest.java
│   └── test_input.txt
│
└── README.md

Requirements

Install PLY:

pip install ply

Running the Project

Interactive Mode

python main.py

Enter code and finish with:

END

Parse a File

python main.py test_input/Container.java

Example Output

AST Generation

Program
└── ClassDecl [Test]
    └── MethodDecl [hello]
        ├── ReturnType [void]
        ├── Params
        └── Body
            └── ReturnStmt

Semantic Analysis

✓ Semantic analysis passed — no errors or warnings.

Error Reporting

[E-SEM-007] Assignment to undeclared variable: 'x'
[E-SEM-006] Call to undefined method: 'foo'

Concepts Demonstrated

  • Lexical Analysis
  • Context-Free Grammars
  • LALR(1) Parsing
  • Abstract Syntax Trees
  • Symbol Tables
  • Scope Management
  • Semantic Analysis
  • Compiler Front-End Design
  • Error Recovery Techniques

Future Improvements

  • Local variable declarations
  • Expression parsing
  • Type inference
  • Intermediate Code Generation (Three Address Code)
  • Constant Folding Optimization
  • Graphviz AST Visualization
  • JVM Bytecode Generation

Educational Purpose

This project is designed as a learning-oriented compiler front-end that demonstrates how programming languages are analyzed before execution. It combines concepts from Compiler Design, Automata & Formal Languages, and Programming Language Theory into a working implementation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors