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.
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.
The mC language supports:
int
- Integer valueschar
- Character valuesvoid
- No return type
- 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
- Global scope for variables declared outside functions
- Local scope for variables and parameters within functions
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
- GCC compiler
- Lex (Flex)
- Yacc (Bison)
- Make
-
Compile the compiler:
make
-
Run tests:
make test
-
Clean build artifacts:
make clean
The build process will create an executable obj/mcc
that serves as the compiler.
# Compile an mC program
./obj/mcc < input.mC
# Or pipe input
cat input.mC | ./obj/mcc
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;
}
}
The test/
directory contains various test cases demonstrating different language features:
testArithExpr.mC
- Arithmetic expressionstestFunDecl.mC
- Function declarationstestCondStmt.mC
- Conditional statementstestLoopStmt.mC
- Loop constructstestAssgnStmt.mC
- Assignment statementstestGloblVars.mC
- Global variablestestLocalVars.mC
- Local variablestestFuncCall.mC
- Function callstestRetrnStmt.mC
- Return statementstestRelOpExpr.mC
- Relational operationstestAddExpr.mC
- Addition expressionstestMulExpr.mC
- Multiplication expressions
- Tokenizes input into keywords, identifiers, operators, and literals
- Handles comments, whitespace, and error detection
- Tracks line and column numbers for error reporting
- 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
- Provides data structures for representing parsed programs
- Includes functions for tree traversal and printing
- Supports different node types for various language constructs
- Manages string storage and lookup
- Handles identifier and string literal storage
- Provides scope-aware symbol table functionality
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
This project is part of an academic assignment and is intended for educational purposes.