This project is a basic compiler written in C++, designed to demonstrate the core components of a compiler such as lexical analysis, parsing, and intermediate code generation. The project is structured to allow flexibility and extensibility, making it a great foundation for learning or extending into a more complex compiler.
The project is organized into several key components:
- Lexer: Responsible for lexical analysis, converting source code into tokens.
- Parser: Converts the tokens generated by the Lexer into an Abstract Syntax Tree (AST).
- IRGenerator: Generates Intermediate Representation (IR) code from the AST.
- Compiler: Orchestrates the entire compilation process by utilizing the Lexer, Parser, and IRGenerator.
- main.cpp: The entry point of the application.
- Compiler.cpp / Compiler.hpp: Manages the overall compilation process.
- Lexer.cpp / Lexer.hpp: Implements the lexical analysis.
- Parser.cpp / Parser.hpp: Implements parsing and AST generation.
- IRGenerator.cpp / IRGenerator.hpp: Implements the generation of intermediate code.
- Token.hpp: Defines the structure of tokens used by the Lexer.
- AST.hpp: Defines the structure of the Abstract Syntax Tree.
- SymbolTable.hpp: Manages symbols and their bindings in the scope of the program.
- C++17 or later: This project utilizes modern C++ features, so you'll need a compiler that supports C++17 or later.
- CMake: Used to manage the build process.
-
Clone the Repository
Clone the project repository to your local machine using Git:git clone https://github.com/merttozer/cpp-compiler.git cd cpp-compiler
-
Build the Project
Use CMake to configure and build the project:mkdir build cd build cmake .. make
This will generate the executable for the cpp-compiler.
-
Run the Application
Once the build is complete, you can run the decoder using the following command:./cpp_compiler <your_source_code_file>
Replace <your_source_code_file> with the path to your .cpp file and with the desired output file name like below. This will parse the provided .pcap file, decode the SIMBA protocol data, and save the results to the specified JSON file.
This project utilizes dependency injection for better flexibility and testability. Each component (Lexer, Parser, IRGenerator) is injected into the Compiler class, allowing you to easily swap out components or extend functionality.
auto lexer = std::make_shared<Lexer>();
auto parser = std::make_shared<Parser>(lexer);
auto irGenerator = std::make_shared<IRGenerator>(parser);
Compiler compiler(lexer, parser, irGenerator);
compiler.compile(sourceCode);
The Lexer class reads the source code and converts it into a series of tokens. These tokens are then passed to the Parser for further processing.
The Parser class takes tokens from the Lexer and generates an Abstract Syntax Tree (AST). This tree represents the hierarchical structure of the source code and is used for further processing.
The IRGenerator class converts the AST into an Intermediate Representation (IR) code. This code is a simplified version of the original source code and can be used for optimization or further compilation stages.
Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.