A compiler of C--
, a C
-like programming language introduced for the compiler lesson in Nanjing University.
- This is the source codes of my programming assignment of Principles and Techniques of Compiler courses (2020 Spring).
- Each push (or pull request) will be tested, goto Actions for details.
- There are still some bugs, but won't be fixed for now.
The main workflow is
CMM source -> Lexical -> Syntax -> Semantics -> IR -> Assembly
File | Description |
---|---|
main.c |
Entry point |
lexical.h, lexical.l, _lexical.c |
Lexical |
syntax.h, syntax.y, _syntax.c |
Syntax |
semantics.h, semantics.c |
Semantics |
ir.h, ir.c |
IR code |
asm.h, asm.c |
Assembly code |
There are some helper functions, macros and structs in other files.
File | Description |
---|---|
common.h |
Shared header |
debug.h |
Debugging |
hash.h, hash.c |
Hasher |
object.h, object.c |
Object creating and destroying, wrapper for malloc/free |
list.h, list.c |
Linked-list |
type.h, type.c |
Types in CMM language |
symbol.h, symbol.c |
Symbol and symbol table |
ast.h, ast.c |
Syntax tree and IR code |
optimize.h, optimize.c |
Optimizer for IR code |
Environment:
- OS: Ubuntu 16.04 or 18.04
- Compiler: gcc 7.5
- Flex: 2.6
- Bison: 3.0
- Spim
Instructions:
- Install dependencies:
apt-get install bison flex build-essential spim
- Build project:
cd src && make
Check lexical analysis.
./src/ncc a.cmm --lexcial
Parse source codes into syntax tree.
./src/ncc a.cmm --syntax
Check semantics analysis.
./src/ncc a.cmm --semantics
Translate source codes to intermediate codes.
# output to stdout
./src/ncc a.cmm --ir
# output to file
./src/ncc a.cmm a.ir --ir
Generate MIPS-32 assembly codes.
# output to stdout
./src/ncc a.cmm
# output to file
./src/ncc a.cmm a.s
cd test && ./testall.sh