# lucasgabriel-cp/my_compiler architecture

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

- Interactive diagram: https://gitdiagram.com/lucasgabriel-cp/my_compiler
- Repository: https://github.com/lucasgabriel-cp/my_compiler

## Overview

##### Main components and their responsibilities
- Input Module – Represented by the file "input2.cmp" which acts as the source code for compilation.
- Driver/Main Module – The main entry point (main.cpp), which coordinates the various compilation phases.
- Lexical Analysis Module – Implemented in "my_lib/lexer.cpp" and "my_lib/lexer.h". This module uses a finite automaton (as noted in the README) to transform raw source code into tokens. It’s also related to the output in "output_lexer.txt".
- Syntax Analysis Module – Found in "my_lib/parser.cpp" and "my_lib/parser.h". It applies tabular predictive (LL(1)) parsing to take tokens from the lexical phase and build a syntax tree. The resulting AST is stored (output_AST.txt) and the tree itself is handled in "my_lib/util/SyntaxTree.cpp/h".
- Semantic Analysis Module – Implemented in "my_lib/semantic.h" (and potentially other related implementation not fully expanded in the tree). It operates on the syntax tree created in the previous phase to validate and process semantics, as shown by "output_semantic.txt".
- Utility Components – Located in "my_lib/util" which includes modules for constructing and handling tokens ("token.cpp/h"), nodes ("Node.cpp/h"), hash matrix functionalities ("HashMatrix.cpp/h"), and the overall syntax tree. These components support the analysis phases.
- Supplementary Documentation – The "gramatica" directory contains supporting documents (e.g., gramatica.docx, tabela.xlsx) that likely detail language grammar and maybe parsing tables.
- Future Extensions – Although not yet implemented, reserve space for components like Code Optimizer and Code Generator.

##### Relationships and data flows
- The Driver Module (main.cpp) reads the source file (input2.cmp) and channels it to the Lexical Analysis Module.
- The Lexical Analysis Module (using the finite automaton) scans the source code, tokenizes it, and sends the list of tokens to the Syntax Analysis Module.
- The Syntax Analysis Module processes the tokens via LL(1) predictive parsing techniques to construct a syntax tree. This step interacts with utility modules found in "my_lib/util".
- The Semantic Analysis Module then takes the syntax tree as input, performing context checks and validations.
- Each module outputs diagnostic or transformed information (e.g., output_lexer.txt, output_AST.txt, output_parser.txt, output_semantic.txt).

##### Architectural patterns and design principles
- Modularity – Each compilation phase is a distinct module with well-defined input/output relations.
- Layered Processing – There’s a clear, sequential flow: input → lexical analysis → syntax analysis (AST construction) → semantic analysis.
- Separation of Concerns – Utility functions and data structures (tokens, syntax tree nodes) are isolated in the "my_lib/util" directory to serve as building blocks for multiple phases.
- Extensibility – Future phases (code optimizer, code generator) can be integrated into this layered design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Compilation Pipeline
    subgraph "Compilation Pipeline"
        IM("Input Module"):::input
        DM("Driver/Main Module"):::driver
        LA("Lexical Analyzer (Finite Automaton)"):::processing
        SA("Syntax Analyzer (LL(1) Parser)"):::processing
        SE("Semantic Analyzer"):::processing
        FE["Future Extensions"]:::future
            F1("Code Optimizer"):::future
            F2("Code Generator"):::future
    end

    %% Utility Components Subgraph
    subgraph "Utility Components"
        TU("Token Manager"):::utility
        NU("Node Manager"):::utility
        HM("HashMatrix"):::utility
        STU("Syntax Tree Utilities"):::utility
    end

    %% Supplementary Documentation
    DOC("Documentation (Grammar & Parsing Tables)"):::docs

    %% Connections in the Compilation Pipeline
    IM -->|"source_code"| DM
    DM -->|"initiates_compilation"| LA
    LA -->|"token_stream (lexer_output:output_lexer.txt)"| SA
    SA -->|"syntax_tree (AST_output:output_AST.txt, parser_output:output_parser.txt)"| SE
    SE -->|"validated_tree (semantic_output:output_semantic.txt)"| FE
    FE --> F1
    FE --> F2

    %% Interactions with Utility Components
    SA -->|"uses"| TU
    SA -->|"uses"| NU
    SA -->|"uses"| HM
    SA -->|"builds"| STU

    %% Documentation support
    DOC ---|"informs"| SA

    %% Click Events
    click IM "https://github.com/lucasgabriel-cp/my_compiler/blob/main/input2.cmp"
    click DM "https://github.com/lucasgabriel-cp/my_compiler/blob/main/main.cpp"
    click LA "https://github.com/lucasgabriel-cp/my_compiler/blob/main/my_lib/lexer.cpp"
    click SA "https://github.com/lucasgabriel-cp/my_compiler/blob/main/my_lib/parser.cpp"
    click SE "https://github.com/lucasgabriel-cp/my_compiler/blob/main/my_lib/semantic.h"
    click TU "https://github.com/lucasgabriel-cp/my_compiler/blob/main/my_lib/util/token.cpp"
    click NU "https://github.com/lucasgabriel-cp/my_compiler/blob/main/my_lib/util/Node.cpp"
    click HM "https://github.com/lucasgabriel-cp/my_compiler/blob/main/my_lib/util/HashMatrix.cpp"
    click STU "https://github.com/lucasgabriel-cp/my_compiler/blob/main/my_lib/util/SyntaxTree.cpp"
    click DOC "https://github.com/lucasgabriel-cp/my_compiler/tree/main/gramatica"

    %% Styles
    classDef input fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef driver fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef processing fill:#ccffff,stroke:#333,stroke-width:2px;
    classDef utility fill:#ffffcc,stroke:#333,stroke-width:2px;
    classDef docs fill:#ffcc99,stroke:#333,stroke-width:2px;
    classDef future fill:#dddddd,stroke:#333,stroke-width:2px;
```
