Skip to content

A comprehensive C++ compiler framework that includes lexical analysis, parsing, and intermediate code generation.

License

Notifications You must be signed in to change notification settings

merttozer/cpp-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++ Compiler Project

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.

Project Structure

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.

Files

  • 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.

Getting Started

Prerequisites

  • 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.

Installation

  1. 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
  2. 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.

  3. 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.

How It Works

Dependency Injection

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);

Lexical Analysis (Lexer)

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.

Parsing (Parser)

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.

Intermediate Code Generation (IRGenerator)

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.

Contributing

Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

A comprehensive C++ compiler framework that includes lexical analysis, parsing, and intermediate code generation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published