MadCompiler is a compiler course design project from the NCHU computer science department.
- Lexical Analysis: Tokenizes the input source code.
- Syntax Analysis: Builds an Abstract Syntax Tree (AST).
- Semantic Analysis: Checks for semantic errors.
- Code Generation: Produces intermediate code.
- Clone the repository:
git clone https://github.com/aqpower/MadCompiler.git
- Build the project using CMake:
cd MadCompiler mkdir build cmake -S . -B build cmake --build build
Run the compiler with the following command:
./build/src/madcompiler <source_code_file> -o <output_file>
Generate the lexical analysis output:
./build/src/madcompiler <source_code_file> -l <output_file>
- Keywords:
if
,if else
,while
; token type 1. - Identifiers: token type 2.
- Constants: Unsigned integers; token type 3.
- Operators:
+
,-
,*
,/
,=
,>
,<
,<=
,!=
; token type 4. - Separators:
,
,;
,{
,}
,(
,)
; token type 5. - Other Tokens: Defined by the following regular expressions:
ID = letter(letter | digit)*
NUM = digit digit*
The start symbol of the grammar is program
. The following production rules are implemented:
program -> block
block -> { stmts }
stmts -> stmt stmts | ε
stmt -> id = expr ;
| if ( bool ) stmt
| if ( bool) stmt else stmt
| while (bool) stmt
| block
bool -> expr < expr
| expr <= expr
| expr > expr
| expr >= expr
| expr
expr -> expr + term
| expr - term
| term
term -> term * factor
| term / factor
| factor
factor -> ( expr ) | ID | NUM
MIT License