How lucasgabriel-cp/my_compiler works
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.