# pablomlago/multifunction_bison_calculator architecture

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

- Interactive diagram: https://gitdiagram.com/pablomlago/multifunction_bison_calculator
- Repository: https://github.com/pablomlago/multifunction_bison_calculator

## Overview

##### Project Purpose and Type
- This is a compiler/interpreter project with a command-line interface. It reads and evaluates mathematical expressions, implements a symbol table using a binary tree, and supports additional commands (e.g., "clean", "echo", "help", "load", "read", and "workspace").

##### Main Components
a. Lexical Analyzer:
- Files: mfcalc.l, lex.yy.c, lex.yy.h.
- Role: Tokenizes input from the command line or files.

b. Parser:
- Files: mfcalc.y, mfcalc.tab.c, mfcalc.tab.h, mfcalc.output.
- Role: Uses Bison to analyze the syntax based on the mathematical expression grammar.

c. Symbol Table:
- Files: abb.c, abb.h.
- Role: Implements a binary tree to store variables, constants, and function information.

d. Command Module:
- Files: commands.c, commands.h.
- Role: Extends basic calculator functionality with additional commands.

e. Error Handling Module:
- Files: errors.c, errors.h.
- Role: Defines and manages error messages across the application.

f. Initialization Module:
- Files: initialization.c, initialization.h.
- Role: Pre-populates the symbol table with predefined functions, commands, and constants.

g. Parameter Extraction:
- Files: params.c, params.h.
- Role: Extracts and validates parameters for commands.

h. Stack Implementation:
- Files: pila.c, pila.h.
- Role: Implements a stack that may be used internally for parsing or expression evaluation.

i. Dynamic Function Library Loader:
- Files: sampleLibrary.c, sampleLibrary.h.
- Role: Demonstrates dynamic loading of external functions (via the "load" command).

j. Utility & Execution:
- Files: launch.sh, instructions1, instructions2.
- Role: The shell script builds the executable; instruction files provide sample usage.

##### Relationships and Interactions
- User Input (CLI) feeds the raw commands and expressions.
- The Lexical Analyzer (Flex-generated from mfcalc.l) processes this input and sends tokens to the Parser.
- The Parser (Bison-generated from mfcalc.y) builds the parsed structure (or possibly an abstract syntax tree) and then interacts with:
- The Symbol Table (for storing variables, constants, or function references).
- The Commands Module (to detect and execute commands such as load, workspace, etc.).
- The Error Handling Module (to manage and signal parsing or evaluation errors).
- The Initialization Module pre-loads the symbol table at startup.
- The Parameter Extraction module assists the Commands Module in processing user command parameters.
- The Stack module might be used internally within parsing/evaluation or when processing nested expressions.
- When a command like "load" is executed, the Dynamic Function Library Loader is invoked to load an external library function.
- Finally, the results (or errors) are output back to the CLI.

##### Architectural Patterns and Design Principles
- Modularity: Each major function is encapsulated in its own module (e.g., parsers, symbol table, error handling).
- Separation of Concerns: Lexical analysis, parsing, error management, command processing, and dynamic loading are clearly separated.
- Layered Design: There’s a clear data flow from user input → lexical analysis → parsing → execution/evaluation with support modules.

- From CLI to Lexical Analyzer.
- From Lexical Analyzer to Parser.
- Between Parser and the Symbol Table, Commands Module, and Error Handler.
- From Commands Module to Dynamic Loader when executing a "load" command.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Front End"
        UI["User Interface (CLI)"]:::cli
        Lexer["Lexical Analyzer (Flex)&#10;[mfcalc.l, lex.yy.c, lex.yy.h]"]:::lexer
        Parser["Parser (Bison)&#10;[mfcalc.y, mfcalc.tab.c, mfcalc.tab.h, mfcalc.output]"]:::parser
    end

    subgraph "Execution Modules"
        SymbolTable["Symbol Table (Binary Tree)&#10;[abb.c, abb.h]"]:::backend
        Commands["Commands Module&#10;[commands.c, commands.h]"]:::backend
        Errors["Error Handling Module&#10;[errors.c, errors.h]"]:::backend
        Init["Initialization Module&#10;[initialization.c, initialization.h]"]:::backend
        Params["Parameter Extraction&#10;[params.c, params.h]"]:::backend
        Stack["Stack Implementation&#10;[pila.c, pila.h]"]:::backend
        DynLoader["Dynamic Function Library Loader&#10;[sampleLibrary.c, sampleLibrary.h]"]:::backend
    end

    Utility["Utility & Execution (Build Script and Usage)&#10;[launch.sh, instructions1, instructions2]"]:::utility

    %% Flow Connections
    UI -->|"provides input"| Lexer
    Lexer -->|"generates tokens"| Parser
    Parser -->|"updates/lookups"| SymbolTable
    Parser -->|"handles commands"| Commands
    Parser -->|"signals errors"| Errors
    Parser -->|"uses stack"| Stack
    Init -->|"preloads"| SymbolTable
    Commands -->|"parameter extraction"| Params
    Commands -->|"invokes"| DynLoader
    UI -->|"runs build/instructions"| Utility

    %% Click Events
    click Lexer "https://github.com/pablomlago/multifunction_bison_calculator/blob/main/mfcalc.l"
    click Parser "https://github.com/pablomlago/multifunction_bison_calculator/blob/main/mfcalc.y"
    click SymbolTable "https://github.com/pablomlago/multifunction_bison_calculator/blob/main/abb.c"
    click Commands "https://github.com/pablomlago/multifunction_bison_calculator/blob/main/commands.c"
    click Errors "https://github.com/pablomlago/multifunction_bison_calculator/blob/main/errors.c"
    click Init "https://github.com/pablomlago/multifunction_bison_calculator/blob/main/initialization.c"
    click Params "https://github.com/pablomlago/multifunction_bison_calculator/blob/main/params.c"
    click Stack "https://github.com/pablomlago/multifunction_bison_calculator/blob/main/pila.c"
    click DynLoader "https://github.com/pablomlago/multifunction_bison_calculator/blob/main/sampleLibrary.c"
    click Utility "https://github.com/pablomlago/multifunction_bison_calculator/blob/main/launch.sh"

    %% Styles
    classDef cli fill:#f8bd7f,stroke:#333,stroke-width:2px;
    classDef lexer fill:#a6cee3,stroke:#333,stroke-width:2px;
    classDef parser fill:#1f78b4,stroke:#fff,stroke-width:2px;
    classDef backend fill:#b2df8a,stroke:#333,stroke-width:2px;
    classDef utility fill:#ffcc80,stroke:#333,stroke-width:2px;
```
