How khubaib3737/c-compiler works
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.