# kovalmaksym/ifj architecture

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

- Interactive diagram: https://gitdiagram.com/kovalmaksym/ifj
- Repository: https://github.com/kovalmaksym/ifj

## Overview

##### Main Components
- Entry Point and I/O Handling:
- Main Component (main.c): Acts as the driver that accepts the input (source code file) and output file names.
- Lexical Analysis:
- Scanner (scanner.c and scanner.h): Reads the input file character-by-character and produces a stream of tokens.
- Syntax Analysis:
- Parser (parser.c and parser.h): Processes the token stream using the grammar rules defined (see doc/Grammar.txt and doc/GramToLL.txt) and checks the validity of the code.
- Precedence Handling (precedence.h): Likely assists in managing operator precedence during parsing or expression evaluation.
- Expression Evaluation:
- Expression Module (expression.c and expression.h): Works on recognizing, evaluating, or converting expressions.
- Semantic Analysis and State Management:
- Symbol Table (symtable.c and symtable.h): Manages symbol definitions, variable scopes, and type checking.
- Stack (stack.c and stack.h): Provides data structures to support parsing (perhaps during expression evaluation) and managing runtime states.
- Code Generation:
- Code Generation Module (codegen.c and codegen.h): Responsible for generating the output code or intermediary representation based on the parsed input.
- Documentation & Auxiliary Files:
- Documentation (doc/ directory): Includes grammar definitions and tables (doc/tables.txt) which can be referenced for in-depth understanding of the grammar and parsing process.
- Additional files (like Makefile and xkratk17.rar) serve build/deployment or submission-related roles.

##### Relationships and Interactions
- The application starts in Main, which handles input file reading and output file writing.
- Main calls the Scanner to convert the input file into a token stream.
- The Parser receives the token stream. It likely leverages the Precedence definitions to process expressions correctly.
- The Expression module operates within the parser to handle arithmetic or logical expressions.
- During parsing, the Symbol Table is used to store and look up variables/symbols.
- The Stack module is invoked by both the Parser and Expression modules to manage intermediate states or operator precedence as needed.
- Once the source program is parsed and its semantics verified, the Code Generation module constructs the output code or an intermediate representation which is then written to the output file.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Each module (scanner, parser, codegen, etc.) has a distinct responsibility, making the project modular.
- Layered Architecture: The system processes the input in stages (lexical analysis → syntax analysis → semantic analysis → code generation).
- Reusability: Utility libraries such as the stack and symbol table could be designed for reuse in other parts of the project or similar projects.

##### Final Checklist
- Nodes/blocks for each main component: Main, Scanner, Parser (including its reliance on precedence/grammar files), Expression Evaluator, Symbol Table, Stack, and Code Generator.
- Blocks with file names and short descriptions to contextualize their roles.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Input/Output Layer"
        MAIN["Main (main.c)"]:::inputOutput
        EXEC["Main Executable (main)"]:::inputOutput
    end

    subgraph "Lexical Analysis"
        SCANNER["Scanner (scanner.c/h)"]:::lexical
    end

    subgraph "Syntax Analysis"
        PARSER["Parser (parser.c/h)"]:::syntax
        PRECEDENCE["Precedence (precedence.h)"]:::syntax
    end

    subgraph "Expression Evaluation"
        EXPRESSION["Expression (expression.c/h)"]:::expression
    end

    subgraph "Semantic Analysis/State Management"
        SYMTABLE["Symbol Table (symtable.c/h)"]:::semantics
        STACK["Stack (stack.c/h)"]:::semantics
    end

    subgraph "Code Generation"
        CODEGEN["Code Generation (codegen.c/h)"]:::codegen
    end

    subgraph "Documentation"
        DOC["Documentation (doc)"]:::documentation
    end

    MAIN -->|"reads_input"| SCANNER
    SCANNER -->|"produces_tokens"| PARSER
    PARSER -->|"parses_expressions"| EXPRESSION
    PARSER -->|"uses_precedence"| PRECEDENCE
    PARSER -->|"updates_queries_symbol_table"| SYMTABLE
    PARSER -->|"utilizes_stack"| STACK
    EXPRESSION -->|"requests_stack_ops"| STACK
    PARSER -->|"sends_IR"| CODEGEN
    EXPRESSION -->|"sends_partial_IR"| CODEGEN
    CODEGEN -->|"outputs_code"| EXEC
    DOC --- PARSER
    DOC --- EXPRESSION
    DOC --- SYMTABLE
    DOC --- CODEGEN

    click MAIN "https://github.com/kovalmaksym/ifj/blob/main/./main.c"
    click EXEC "https://github.com/kovalmaksym/ifj/tree/main/./main"
    click SCANNER "https://github.com/kovalmaksym/ifj/blob/main/./scanner.c"
    click SCANNER "https://github.com/kovalmaksym/ifj/blob/main/./scanner.h"
    click PARSER "https://github.com/kovalmaksym/ifj/blob/main/./parser.c"
    click PARSER "https://github.com/kovalmaksym/ifj/blob/main/./parser.h"
    click PRECEDENCE "https://github.com/kovalmaksym/ifj/blob/main/./precedence.h"
    click EXPRESSION "https://github.com/kovalmaksym/ifj/blob/main/./expression.c"
    click EXPRESSION "https://github.com/kovalmaksym/ifj/blob/main/./expression.h"
    click SYMTABLE "https://github.com/kovalmaksym/ifj/blob/main/./symtable.c"
    click SYMTABLE "https://github.com/kovalmaksym/ifj/blob/main/./symtable.h"
    click STACK "https://github.com/kovalmaksym/ifj/blob/main/./stack.c"
    click STACK "https://github.com/kovalmaksym/ifj/blob/main/./stack.h"
    click CODEGEN "https://github.com/kovalmaksym/ifj/blob/main/./codegen.c"
    click CODEGEN "https://github.com/kovalmaksym/ifj/blob/main/./codegen.h"
    click DOC "https://github.com/kovalmaksym/ifj/tree/main/./doc"

    classDef inputOutput fill:#f9f,stroke:#333,stroke-width:2px;
    classDef lexical fill:#bbf,stroke:#333,stroke-width:2px;
    classDef syntax fill:#afa,stroke:#333,stroke-width:2px;
    classDef expression fill:#fea,stroke:#333,stroke-width:2px;
    classDef semantics fill:#eef,stroke:#333,stroke-width:2px;
    classDef codegen fill:#fbf,stroke:#333,stroke-width:2px;
    classDef documentation fill:#ddd,stroke:#333,stroke-width:2px;
```
