Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

MiniLang Compiler

A lightweight educational compiler written in Python. It implements the core phases of a simple compiler pipeline: lexical analysis, parsing, semantic analysis, intermediate code generation, and target code generation.

Description

MiniLang Compiler takes a small Pascal-like source program and produces compiler reports for each stage of compilation. It is designed for learning compiler construction concepts and demonstrating how source code moves through tokens, syntax trees, semantic checks, intermediate representation, and target-like instructions.

Features

  • Lexical analysis for reserved words, identifiers, numbers, strings, symbols, and compound operators
  • Parser for a simple program structure and basic statements
  • Semantic analysis for symbol tracking and use-before-assignment checks
  • Intermediate code generation
  • Simple target code generation
  • Compilation reports written to the reports/ directory

Supported Language Snapshot

The current compiler supports a small subset of a Pascal-like language:

  • Program declaration using program <name>;
  • Input statement: read(variable)
  • Output statement: write(variable)
  • Assignment statement: variable := value
  • Comments beginning with !

Example input:

program demo;
read(x)
y := 10
write(x)
write(y)

Project Structure

.
├── compiler.py
├── README.md
└── blahblahblah/
    ├── lexer.py
    ├── parser.py
    ├── semantic.py
    ├── intermediate.py
    └── codegen.py

Requirements

  • Python 3.8 or newer

This project uses only the Python standard library.

Usage

Create a source file, for example sample.bbb:

program demo;
read(x)
y := 10
write(x)
write(y)

Run the compiler:

python compiler.py sample.bbb

After compilation, generated reports are written to the reports/ directory.

Generated Reports

The compiler can generate the following files:

Report Description
reports/lexer_report.txt Tokens, identifiers, and lexical errors
reports/parser_report.txt Parsed syntax tree and parser errors
reports/semantic_report.txt Symbol table and semantic errors
reports/listing.txt Source listing with errors attached to line numbers
reports/intermediate_report.txt Generated intermediate representation
reports/target_report.txt Generated target-like instructions

If lexical, parser, or semantic errors are found, compilation stops before intermediate and target code generation.

Compiler Pipeline

Source Code
    ↓
Lexer
    ↓
Parser
    ↓
Semantic Analyzer
    ↓
Intermediate Code Generator
    ↓
Target Code Generator

Example Output

For this source:

program demo;
read(x)
y := 10
write(y)

The intermediate report may contain:

READ x
y = 10
WRITE y

The target report may contain:

IN x
LOAD 10
STORE y
OUT y

Notes

  • Identifiers are normalized to lowercase by the lexer.
  • Identifier tokens are limited to the first 32 characters.
  • Text after ! on a line is treated as a comment.
  • The parser currently supports a compact statement format and does not require semicolons after every statement.

Future Improvements

  • Add support for expressions such as x + y or a * b
  • Add block syntax with begin and end
  • Add variable declarations and type checking
  • Improve parser error recovery
  • Add automated tests
  • Package the compiler as a command-line tool

License

No license has been specified yet. Add a license before distributing or publishing this project.

About

A lightweight educational compiler written in Python that demonstrates lexical analysis, parsing, semantic analysis, intermediate code generation, and target code generation for a small Pascal-like language

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages