Skip to content

Jamesha123/Simple_Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Compiler (mC)

A simple compiler for the mC language, a subset of C that supports basic programming constructs including arithmetic expressions, control flow statements, function declarations, and variable management.

Overview

This compiler is built using traditional compiler construction tools:

  • Lex (Flex) for lexical analysis
  • Yacc (Bison) for syntax analysis and parsing
  • C for the runtime and AST implementation

The compiler generates an Abstract Syntax Tree (AST) representation of the input program and can output the parsed structure.

Features

The mC language supports:

Data Types

  • int - Integer values
  • char - Character values
  • void - No return type

Language Constructs

  • Variable Declarations - Global and local variables
  • Function Declarations - With parameters and return types
  • Arithmetic Expressions - Addition, subtraction, multiplication, division
  • Relational Operations - Less than, greater than, equality, etc.
  • Control Flow - If-else statements, while loops
  • Function Calls - With argument lists
  • Return Statements - Function return values
  • Comments - Single-line and multi-line comments

Scope Management

  • Global scope for variables declared outside functions
  • Local scope for variables and parameters within functions

Project Structure

Simple_Compiler/
├── src/                    # Source code
│   ├── driver.c           # Main program entry point
│   ├── parser.y           # Yacc grammar definition
│   ├── scanner.l          # Lex lexical analyzer
│   ├── tree.c/h           # AST implementation
│   └── strtab.c/h         # String table management
├── test/                  # Test files
│   ├── testArithExpr.mC   # Arithmetic expression tests
│   ├── testFunDecl.mC     # Function declaration tests
│   ├── testCondStmt.mC    # Conditional statement tests
│   └── ...                # Additional test cases
├── makefile               # Build configuration
└── README.md              # This file

Building the Compiler

Prerequisites

  • GCC compiler
  • Lex (Flex)
  • Yacc (Bison)
  • Make

Build Instructions

  1. Compile the compiler:

    make
  2. Run tests:

    make test
  3. Clean build artifacts:

    make clean

The build process will create an executable obj/mcc that serves as the compiler.

Usage

Compiling mC Programs

# Compile an mC program
./obj/mcc < input.mC

# Or pipe input
cat input.mC | ./obj/mcc

Example mC Program

int globalVar;

int add(int a, int b) {
    int result;
    result = a + b;
    return result;
}

void main() {
    int x, y;
    x = 5;
    y = 10;
    
    if (x < y) {
        int sum = add(x, y);
    }
    
    while (x > 0) {
        x = x - 1;
    }
}

Test Files

The test/ directory contains various test cases demonstrating different language features:

  • testArithExpr.mC - Arithmetic expressions
  • testFunDecl.mC - Function declarations
  • testCondStmt.mC - Conditional statements
  • testLoopStmt.mC - Loop constructs
  • testAssgnStmt.mC - Assignment statements
  • testGloblVars.mC - Global variables
  • testLocalVars.mC - Local variables
  • testFuncCall.mC - Function calls
  • testRetrnStmt.mC - Return statements
  • testRelOpExpr.mC - Relational operations
  • testAddExpr.mC - Addition expressions
  • testMulExpr.mC - Multiplication expressions

Implementation Details

Lexical Analysis (scanner.l)

  • Tokenizes input into keywords, identifiers, operators, and literals
  • Handles comments, whitespace, and error detection
  • Tracks line and column numbers for error reporting

Syntax Analysis (parser.y)

  • Defines the grammar for the mC language
  • Builds Abstract Syntax Tree during parsing
  • Manages symbol table for scope resolution
  • Handles semantic actions for AST construction

AST Implementation (tree.c/h)

  • Provides data structures for representing parsed programs
  • Includes functions for tree traversal and printing
  • Supports different node types for various language constructs

String Table (strtab.c/h)

  • Manages string storage and lookup
  • Handles identifier and string literal storage
  • Provides scope-aware symbol table functionality

Development

This compiler was developed as part of a compiler construction course, demonstrating the fundamentals of:

  • Lexical analysis and tokenization
  • Context-free grammar definition
  • Abstract syntax tree construction
  • Symbol table management
  • Error handling and reporting

License

This project is part of an academic assignment and is intended for educational purposes.

About

A simple complier that is able to parse and read simple c++ code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published