A custom-built Compiler Frontend and Interpreter for a subset of the C language. This project implements the full pipeline from raw source code to execution, including Lexical Analysis, Parsing (AST), Semantic Validation, and Runtime Interpretation.
- Converts raw source code into a stream of Tokens.
- Handles Keywords (
int,print), Identifiers, Numbers, and Operators. - Robust Error Handling: Detects invalid characters and malformed identifiers.
- Implements a Recursive Descent Parser.
- builds a hierarchical Abstract Syntax Tree (AST).
- Handles Operator Precedence (multiplication
*happens before addition+). - Validates grammar rules (e.g., missing semicolons
T_SEMICOLON).
- Symbol Table: tracks variable declarations and memory state.
- Scope Validation:
- Prevents using undeclared variables.
- Prevents re-declaring existing variables.
- Traverses the AST recursively to execute logic.
- Performs arithmetic calculations.
- Handles Runtime Errors (like Division by Zero).
- Executes
print()statements to standard output.
The project follows the standard compiler pipeline:
graph TD
A[Source Code] -->|Lexer| B[Tokens]
B -->|Parser| C[Abstract Syntax Tree (AST)]
C -->|Semantic Check| D[Validated AST]
D -->|Interpreter| E[Output / Result]
- Language: C (Standard C99)
- Tools: GCC Compiler
- No External Libraries: Built completely from scratch (std libs only).
- Clone the repository
- Write your C code in
inputfile.txt:int x = 10; int y = 5; int result = (x + y) * 2; print(result);
- Compile the Parser:
gcc parser.c -o parser
- Run logic:
Note: You can pass any filename as an argument.
./parser inputfile.txt
<program> ::= <statement>*
<statement> ::= <declaration> | <assignment> | <print_stmt>
<declaration> ::= "int" IDENTIFIER "=" <expression> ";"
<assignment> ::= IDENTIFIER "=" <expression> ";"
<print_stmt> ::= "print" "(" <expression> ")" ";"
<expression> ::= <term> { ("+" | "-") <term> }
<term> ::= <factor> { ("*" | "/") <factor> }
<factor> ::= NUMBER | IDENTIFIER | "(" <expression> ")"