# khubaib3737/c-compiler architecture

> Architecture diagram of the GitHub repository khubaib3737/c-compiler, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/khubaib3737/c-compiler
- Repository: https://github.com/khubaib3737/c-compiler

## Overview

##### Project Type and Purpose
- This is a C++ compiler that performs lexical analysis, syntax analysis, and parse tree construction.
- It reads source code from an input file (parser_files/source.txt), tokenizes the code (Scanner), checks syntax using an LL(1) parser (SyntaxAnalyzer with Grammar.h defining rules), and finally builds a parse tree (ParseTree.h).
- The compiler also handles error logging and uses CSV files (transition_table1.csv and transition_table2.csv) for defining state transitions during parsing.

##### Analyzing the File Structure
- Top-Level Files: Main.cpp, Grammar.h, Scanner.h, SyntaxAnalyzer.h, ParseTree.h, and CustomDataTypesLibrary.h form the core code base.
- Output Directory: TokensAndErrorsOutput contains files that record tokens, errors, and grammar productions used.
- Parser Files: The parser_files directory houses configuration/transition tables and a sample source file (keywords.txt, source.txt, transition table CSVs), which facilitate the runtime behavior of the compiler.
- Separation of distinct roles: input configuration, core compiler logic, and output/error reporting.

##### Main Components
a. Input Component:
- File: parser_files/source.txt – the source code file.
- Parser configuration files: keywords.txt and the CSV files (transition_table1.csv, transition_table2.csv).
b. Compiler Core:
i. Main Module (Main.cpp): Acts as the entry point that orchestrates the overall process.
ii. Lexical Analysis Module (Scanner.h): Reads source code, tokenizes it into lexical elements, and saves output tokens.
iii. Syntax Analysis Module (SyntaxAnalyzer.h) using the LL(1) rules defined in Grammar.h: Reads tokens (from token.txt) and verifies syntax.
iv. Parse Tree Construction Module (ParseTree.h): Constructs and maintains the parse tree from a stream of tokens.
v. Custom Data Types Library (CustomDataTypesLibrary.h): Provides supporting data structures (like maps or pairs) that the compiler relies on.
c. Output and Error Logging:
- Tokens and Errors: Tokenized output is stored in TokensAndErrorsOutput/token.txt (and tokensVersion2.txt), while any errors detected are logged in error.txt.
- ProductionsUsed.txt captures which grammar productions were applied during parsing.

##### Relationships and Interactions
- Main.cpp initiates the process:
- It calls the Scanner to perform lexical analysis, converting source code into a stream of tokens.
- The Scanner may rely on the configuration provided in the CSV files and keywords.txt.
- The tokens are then passed to the SyntaxAnalyzer:
- The SyntaxAnalyzer validates the tokens using the LL(1) grammar, referencing Grammar.h.
- It also writes results and errors into corresponding output files.
- If the tokens are syntactically valid, the ParseTree module constructs a parse tree for structural representation.
- Throughout, error handling is performed and logged (error.txt) for any discrepancies found.

##### Architectural Patterns and Design Principles
- Modular Design: Each phase (lexical analysis, syntax analysis, tree creation) is encapsulated within its own module, aiding clarity and maintainability.
- Data Flow: The project employs a clear sequential data flow – from reading the source text, through tokenization, syntax checking, to parse tree creation.
- Configuration-Driven Behavior: The use of CSV files to define transition tables is an important detail that influences the parsing logic.
- Emphasis on Decoupling: By keeping output (tokens and errors) in dedicated directories, the design encourages separation of concerns.

##### Elements
- Each component should be clearly labeled:
- "Main Module (Main.cpp)"
- "Lexical Analyzer (Scanner.h)"
- "Syntax Analyzer (SyntaxAnalyzer.h) with LL(1) Grammar (Grammar.h)"
- "Parse Tree Builder (ParseTree.h)"
- "Custom Data Structures (CustomDataTypesLibrary.h)"
- Input configurations (parser_files directory – source.txt, CSVs, keywords.txt)
- Output directories (TokensAndErrorsOutput)
- From Input File → Scanner → SyntaxAnalyzer → ParseTree.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Input & Configurations"
        src["Source Code (source.txt)"]:::input
        key["Keywords (keywords.txt)"]:::input
        tt1["Transition Table 1 (CSV)"]:::input
        tt2["Transition Table 2 (CSV)"]:::input
    end

    subgraph "Compiler Core"
        main["Main Module (Main.cpp)"]:::process
        scan["Lexical Analyzer (Scanner.h)"]:::process
        syn["Syntax Analyzer (SyntaxAnalyzer.h)"]:::process
        grammar["LL(1) Grammar (Grammar.h)"]:::process
        tree["Parse Tree Builder (ParseTree.h)"]:::process
        util["Custom Data Structures (CustomDataTypesLibrary.h)"]:::util
    end

    subgraph "Output & Logging"
        out["Output & Logging (TokensAndErrorsOutput)"]:::output
    end

    %% Relationships and Data Flow
    main -->|"calls"| scan
    src -->|"reads"| scan
    key -->|"provides"| scan
    tt1 -->|"configures"| scan
    tt2 -->|"configures"| scan
    scan -->|"tokenStream"| syn
    grammar -->|"LL(1)rules"| syn
    syn -->|"validatedTokens"| tree
    syn -->|"errorLogs"| out
    tree -->|"parseTreeOutput"| out
    main -->|"uses"| util

    %% Click Events
    click src "https://github.com/khubaib3737/c-compiler/blob/main/parser_files/source.txt"
    click key "https://github.com/khubaib3737/c-compiler/blob/main/parser_files/keywords.txt"
    click tt1 "https://github.com/khubaib3737/c-compiler/blob/main/parser_files/transition_table1.csv"
    click tt2 "https://github.com/khubaib3737/c-compiler/blob/main/parser_files/transition_table2.csv"
    click main "https://github.com/khubaib3737/c-compiler/blob/main/Main.cpp"
    click scan "https://github.com/khubaib3737/c-compiler/blob/main/Scanner.h"
    click syn "https://github.com/khubaib3737/c-compiler/blob/main/SyntaxAnalyzer.h"
    click grammar "https://github.com/khubaib3737/c-compiler/blob/main/Grammar.h"
    click tree "https://github.com/khubaib3737/c-compiler/blob/main/ParseTree.h"
    click util "https://github.com/khubaib3737/c-compiler/blob/main/CustomDataTypesLibrary.h"
    click out "https://github.com/khubaib3737/c-compiler/tree/main/TokensAndErrorsOutput"

    %% Styles
    classDef input fill:#e0f7fa,stroke:#006064,stroke-width:2px;
    classDef process fill:#ffe082,stroke:#ff8f00,stroke-width:2px;
    classDef output fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef util fill:#d1c4e9,stroke:#5e35b1,stroke-width:2px;
```
