This project is a simple Arithmetic Compiler written in C, designed to tokenize, parse, and generate assembly-like instructions for basic arithmetic expressions. The compiler can handle addition, subtraction, multiplication, and division operations on integers.
The project is organized as follows:
ArithmeticCompiler/
├── CMakeLists.txt # Build configuration file for CMake
├── include/
│ ├── tokenizer.h # Header file for the tokenizer
│ ├── parser.h # Header file for the parser
│ └── assembly.h # Header file for the assembly generator
├── src/
│ ├── main.c # Main entry point of the program
│ ├── tokenizer.c # Implementation of the tokenizer
│ ├── parser.c # Implementation of the parser
│ └── assembly.c # Implementation of the assembly generator
└── README.md # Project documentation
- Tokenization: Breaks down an arithmetic expression into individual tokens.
- Parsing: Parses tokens in a structured way to calculate a result.
- Assembly Code Generation: Generates a simple assembly-like code to represent the computation steps.
To compile and run the project, you’ll need:
- GCC or any C compiler
- CMake version 3.10 or higher
Clone the repository and navigate to the project directory:
git clone <repository-url>
cd ArithmeticCompiler
-
Create a build directory:
mkdir build cd build
-
Run CMake to configure the project:
cmake ..
-
Compile the project:
make
-
After compilation, an executable named
ArithmeticCompiler
will be generated in thebuild
directory.
To use the compiler:
-
Run the executable:
./ArithmeticCompiler
-
Enter an arithmetic expression (e.g.,
3 + 4 * 2
), and the compiler will display the parsed result and generated assembly-like instructions.
Here’s an example of using the compiler:
$ ./ArithmeticCompiler
Enter an arithmetic expression (e.g., 3 + 4 - 2): 3 + 4 * 2
Parsed result: 11
Assembly Code:
LOAD 3
ADD 4
MUL 2
The code is divided into three main modules: Tokenization, Parsing, and Assembly Generation. Here’s a quick overview of each module:
-
The tokenizer processes an input string and breaks it down into tokens representing numbers, operators (
+
,-
,*
,/
), and the end of the expression. Each token is defined by aTokenType
and may hold a numerical value.typedef enum { TOKEN_NUMBER, TOKEN_PLUS, TOKEN_MINUS, TOKEN_MULTIPLICATION, TOKEN_DIVISION, TOKEN_END } TokenType; typedef struct { TokenType type; int value; } Token; Token *tokenize(const char *input);
-
The parser processes the list of tokens to compute the result of the arithmetic expression. It interprets operators and numbers and returns the final result.
int parse(Token *tokens, int *result);
-
The assembly generator creates simple assembly-like instructions for each arithmetic operation. These instructions simulate loading a value and applying operations (addition, subtraction, etc.).
void generate_assembly(Token *tokens);
-
The main file initializes the program, takes user input, calls the tokenizer, parser, and assembly generator, and then displays the results to the user.
int main() { // User input handling, calling modules, and displaying results }
Contributions are welcome! If you'd like to improve this compiler, feel free to fork the repository, make your changes, and submit a pull request. Whether it's optimizing the code, adding new features, or improving documentation, all contributions are valued.
If you find any issues or have suggestions, please open an issue to help me improve.