# onetrueawk/awk architecture

> Architecture diagram of the GitHub repository onetrueawk/awk, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/onetrueawk/awk
- Repository: https://github.com/onetrueawk/awk

## Overview

##### Main Components Structure

a) Input Processing Layer
- UTF-8 input handling
- CSV processing module (new feature)
- Input stream management

b) Lexical Analysis
- Lex.c as the tokenizer
- Connection to UTF-8 character handling

c) Parsing Layer
- Awkgram.y (Yacc/Bison grammar)
- Parse tree generation
- Symbol table management

d) Translation/Intermediate Layer
- Tran.c's role
- Program representation structures

e) Runtime Execution Engine
- Pattern-action processing
- Built-in function implementation
- Regular expression engine with UTF-8 support

##### Component Relationships

**[Input] -> [Lexer] -> [Parser] -> [Translator] -> [Runtime Engine]**
- ^

**[Library Functions] ----------------------------------|**

##### Implementation Details to Highlight
- maketab.c's role in generating parsing tables
- lib.c's built-in function implementation
- run.c's execution engine
- Memory management system

##### Key Files to Reference
**awk.h         - Core definitions**
**awkgram.y     - Grammar definition**
**lex.c         - Lexical analyzer**
**parse.c       - Parsing utilities**
**tran.c        - Translation layer**
**run.c         - Runtime execution**
**lib.c         - Library functions**

##### Testing Infrastructure
A separate section showing:
- Test framework structure
- Regression test system
- Bug tracking system

This design should reflect both the classical AWK interpreter architecture and the modern additions for UTF-8 and CSV processing, making it a comprehensive representation of the current implementation.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Input
        Input[Input Stream]:::input
        UTF8[UTF-8 Processing]:::input
        CSV[CSV Processing]:::input
        Input --> UTF8
        Input --> CSV
    end

    subgraph "Lexical Analysis"
        Lexer["Lexical Analyzer"]:::core
        click Lexer "https://github.com/onetrueawk/awk/blob/master/lex.c"
    end

    subgraph "Parsing System"
        Grammar["Grammar Definition"]:::core
        ParseUtils["Parse Utilities"]:::core
        MakeTab["Table Generator"]:::core
        ParseTree["Parse Tree"]:::core
        
        click Grammar "https://github.com/onetrueawk/awk/blob/master/awkgram.y"
        click ParseUtils "https://github.com/onetrueawk/awk/blob/master/parse.c"
        click MakeTab "https://github.com/onetrueawk/awk/blob/master/maketab.c"
        
        Grammar --> ParseTree
        ParseUtils --> ParseTree
        MakeTab --> Grammar
    end

    subgraph "Translation Layer"
        Translator["Translation Module"]:::core
        SymbolTable["Symbol Table"]:::core
        
        click Translator "https://github.com/onetrueawk/awk/blob/master/tran.c"
    end

    subgraph "Runtime System"
        Runtime["Runtime Engine"]:::core
        LibFuncs["Library Functions"]:::lib
        RegEx["RegEx Engine"]:::core
        
        click Runtime "https://github.com/onetrueawk/awk/blob/master/run.c"
        click LibFuncs "https://github.com/onetrueawk/awk/blob/master/lib.c"
        
        Runtime --> RegEx
        LibFuncs --> Runtime
    end

    subgraph "Core Infrastructure"
        CoreDefs["Core Definitions"]:::core
        Proto["Protocol Definitions"]:::core
        MainEntry["Main Program"]:::core
        
        click CoreDefs "https://github.com/onetrueawk/awk/blob/master/awk.h"
        click Proto "https://github.com/onetrueawk/awk/blob/master/proto.h"
        click MainEntry "https://github.com/onetrueawk/awk/blob/master/main.c"
    end

    subgraph "Testing Framework"
        Tests["Test Suite"]:::test
        Regression["Regression Tests"]:::test
        BugTracker["Bug Tracking"]:::test
        
        click Tests "https://github.com/onetrueawk/awk/tree/master/testdir/"
        click Regression "https://github.com/onetrueawk/awk/tree/master/REGRESS"
        click BugTracker "https://github.com/onetrueawk/awk/tree/master/bugs-fixed/"
        
        Tests --> Regression
        Tests --> BugTracker
    end

    UTF8 --> Lexer
    CSV --> Lexer
    Lexer --> Grammar
    ParseTree --> Translator
    Translator --> Runtime
    CoreDefs --> Lexer
    CoreDefs --> Grammar
    CoreDefs --> Translator
    CoreDefs --> Runtime
    Proto --> Runtime
    MainEntry --> Runtime

    classDef core fill:#2374f7,stroke:#333,stroke-width:2px
    classDef input fill:#4CAF50,stroke:#333,stroke-width:2px
    classDef lib fill:#FFD700,stroke:#333,stroke-width:2px
    classDef test fill:#FF5733,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Input Processing"]:::input
        L3["Library Functions"]:::lib
        L4["Testing Components"]:::test
    end
```
