Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Aaron-Gp/Mini-C-Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Mini-C Compiler

Rules

Keyword

int, return, main, void, if, else, while, continue, break

Identifier

Identifiers compliant with the C89 standard ([A-Za-z_][0-9A-Za-z_]*)

Constant

Decimal integers, such as 1, 223, 10, etc

Assignment operator

=

Operator

Unary Operator

- ! ~

Binary Operator

+ - * / % < <= > >= == != & | ^ && ||

Punctuation

; { } ( )

Statement

Variable Declaration

int a, b=111, c=1+3;

Assignment Statement

a = (d+b&1)/(e!=3^b/c&d); 
a = b+c;

Return Statement

return a+b; 
return func(a);

Function Call

println_int(a+b);

Conditional Statement

if ( condition ) { ... }
if ( condition ) { ... } else { ...  }

Loop Statement

while ( condition ) { ... }

Loop Control Statement

continue;
break;

Function Definition

Without Arg(s)

int func(){...}
void func() {...}

With Arg(s)

int func(int a, int b){...}
void func(int a, int b){...}

Preset Function

In addition to custom functions, it is also necessary to support calls to preset functions

println_int(int a)

Has the same output as printf("%d\n", a) in C

Need to be able to handle function recursion normally

Get Started

The following command can generate assembly code using gcc

gcc -S -masm=intel -fverbose-asm test.c

To compile with cmake, execute the following command from the command line:

mkdir build
cd build
cmake ..
cmake –build .

The compilation product is called build/CompilerlabX.


If you want to execute the assembly code yourself and debug it

X86:

Execute in Linux end point after inserting the code yourself

gcc -m32 -no-pie <input assembly file> -o <output executable file> ./<output executable file>

The output can be observed.

Note: On some machines, you may need to add i386 architecture packages to perform the above operations correctly. The reference command is as follows

sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install libc6:i386 libstdc++6:i386 gcc-multilib

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published