# cantrepro/cparse architecture

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

- Interactive diagram: https://gitdiagram.com/cantrepro/cparse
- Repository: https://github.com/cantrepro/cparse

## Overview

#### Purpose and Public Entry Points
`cparse` is a portable C11 static-library parser generator/runtime for textual grammars. It constructs LR(1) or LALR(1) parsing tables and executes them against tokens supplied by the external `clex` lexer. Public types, status/error contracts, parser construction, parse/accept calls, and memory ownership APIs are exposed in `cparse.h`.

#### Grammar Ingestion and Representation
Grammar source text is parsed into productions, terminal/nonterminal symbols, and associated metadata by `grammar.c`, with internal structures declared in `grammar.h`. This is the boundary between user-authored production syntax and table-generation algorithms. Grammar destruction is part of the public lifecycle.
- `grammar.c`
- `grammar.h`
- `cparse.h`

#### Set Analysis and Validation
Grammar analysis computes First/Follow information used for lookahead propagation and detects malformed or unexpected production conditions. Shared dynamic containers, string handling, and supporting operations live in `util.c`/`util.h`, enabling the implementation to avoid fixed grammar-size limits.
- `grammar.c`
- `util.c`
- `util.h`

#### LR(1) Automaton and Table Construction
The core generator builds LR item sets, closure/goto transitions, and indexed action/goto tables. It emits conflict diagnostics with state/item/action context when grammars are not deterministic under the selected construction.
- `lr1_lalr1.c`
- `lr1_lalr1.h`
- `grammar.h`

#### LALR(1) Reduction
LALR parser construction derives from the LR(1) machinery by merging compatible LR states, yielding a more compact parser while retaining the same runtime API and ownership model. `cparseFreeParser` handles parsers built through either LR(1) or LALR(1) constructors.
- `lr1_lalr1.c`
- `lr1_lalr1.h`
- `cparse.h`

#### Parse Runtime and Error/Tree Output
At runtime, parser actions drive shift/reduce stacks using terminal and nonterminal IDs for direct table lookup. `cparseAccept` validates only; `cparse` additionally creates parse-tree nodes. Terminal tokens and aggregate nonterminal nodes preserve source spans. Failures are retained as structured errors containing position, expected tokens, and offending lexeme.
- `lr1_lalr1.c`
- `cparse.h`
- `grammar.h`

#### Integration, Build, and Consumer Example
The project produces `libcparse.a` through `Makefile`. `clex` is a Git submodule and external lexical boundary: callers configure token kinds, pass a borrowed `clexLexer` to parser creation, and retain responsibility for destroying it. The expression program demonstrates the full flow: configure lexer → parse grammar → build LALR parser → parse input → consume/free tree.
- `Makefile`
- `clex`
- `examples/expr_parser.c`

Main entry points: [`cparse.h`](https://github.com/cantrepro/cparse/blob/master/cparse.h) (cparse API), [`lr1_lalr1.c`](https://github.com/cantrepro/cparse/blob/master/lr1_lalr1.c) (LR(1) automaton), [`lr1_lalr1.h`](https://github.com/cantrepro/cparse/blob/master/lr1_lalr1.h) (Action/goto tables), [`grammar.c`](https://github.com/cantrepro/cparse/blob/master/grammar.c) (Grammar ingestion)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public Surface"]
  node_api["cparse API<br/>public header<br/>[cparse.h]"]
  node_build["Static library build<br/>build target"]
  node_example["Expression example<br/>consumer example<br/>[expr_parser.c]"]
  node_lexer{{"clex lexer<br/>external lexer"}}
end

subgraph group_grammar["Grammar Pipeline"]
  node_grammar_ingest["Grammar ingestion<br/>grammar reader<br/>[grammar.c]"]
  node_grammar_model["Grammar model<br/>internal model<br/>[grammar.h]"]
  node_analysis["First/Follow analysis<br/>grammar analysis<br/>[grammar.c]"]
  node_utilities["Dynamic utilities<br/>shared containers<br/>[util.c]"]
end

subgraph group_generator["Parser Generation"]
  node_lr_automaton["LR&#40;1&#41; automaton<br/>state generator<br/>[lr1_lalr1.c]"]
  node_lalr_reduce["LALR state merge<br/>table reduction<br/>[lr1_lalr1.c]"]
  node_parse_tables[("Action/goto tables<br/>parser tables<br/>[lr1_lalr1.h]")]
end

subgraph group_runtime["Runtime"]
  node_parse_runtime["Shift/reduce runtime<br/>parser executor<br/>[lr1_lalr1.c]"]
end

node_build -->|"packages"| node_api
node_example -->|"uses"| node_api
node_example -->|"configures"| node_lexer
node_api -->|"parses grammar text"| node_grammar_ingest
node_grammar_ingest -->|"creates"| node_grammar_model
node_grammar_ingest -->|"uses"| node_utilities
node_grammar_model -->|"analyzed by"| node_analysis
node_utilities -->|"supports"| node_analysis
node_analysis -->|"supplies lookaheads"| node_lr_automaton
node_grammar_model -->|"defines productions"| node_lr_automaton
node_lr_automaton -->|"optional compaction"| node_lalr_reduce
node_lr_automaton -->|"builds LR&#40;1&#41; tables"| node_parse_tables
node_lalr_reduce -->|"builds LALR&#40;1&#41; tables"| node_parse_tables
node_api -->|"constructs parser"| node_parse_tables
node_parse_tables -->|"drives actions"| node_parse_runtime
node_lexer -->|"supplies tokens"| node_parse_runtime
node_api -->|"accepts or parses"| node_parse_runtime

click node_api "https://github.com/cantrepro/cparse/blob/master/cparse.h"
click node_build "https://github.com/cantrepro/cparse/blob/master/Makefile"
click node_example "https://github.com/cantrepro/cparse/blob/master/examples/expr_parser.c"
click node_lexer "https://github.com/cantrepro/cparse/tree/master/clex"
click node_grammar_ingest "https://github.com/cantrepro/cparse/blob/master/grammar.c"
click node_grammar_model "https://github.com/cantrepro/cparse/blob/master/grammar.h"
click node_analysis "https://github.com/cantrepro/cparse/blob/master/grammar.c"
click node_utilities "https://github.com/cantrepro/cparse/blob/master/util.c"
click node_lr_automaton "https://github.com/cantrepro/cparse/blob/master/lr1_lalr1.c"
click node_lalr_reduce "https://github.com/cantrepro/cparse/blob/master/lr1_lalr1.c"
click node_parse_tables "https://github.com/cantrepro/cparse/blob/master/lr1_lalr1.h"
click node_parse_runtime "https://github.com/cantrepro/cparse/blob/master/lr1_lalr1.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_build,node_example,node_lexer toneBlue
class node_grammar_ingest,node_grammar_model,node_analysis,node_utilities toneAmber
class node_lr_automaton,node_lalr_reduce,node_parse_tables toneMint
class node_parse_runtime toneRose
```

## Components

### Public Surface

- [cparse API](https://github.com/cantrepro/cparse/blob/master/cparse.h): public header · Construction, parsing, errors, trees, and ownership contracts. · `cparse.h`
- [Static library build](https://github.com/cantrepro/cparse/blob/master/Makefile): build target · Produces libcparse.a. · `Makefile`
- [Expression example](https://github.com/cantrepro/cparse/blob/master/examples/expr_parser.c): consumer example · Demonstrates lexer setup through tree cleanup. · `examples/expr_parser.c`
- [clex lexer](https://github.com/cantrepro/cparse/tree/master/clex): external lexer · Borrowed token source owned by the caller. · `clex`

### Grammar Pipeline

- [Grammar ingestion](https://github.com/cantrepro/cparse/blob/master/grammar.c): grammar reader · Converts textual productions into internal grammar data. · `grammar.c`
- [Grammar model](https://github.com/cantrepro/cparse/blob/master/grammar.h): internal model · Symbols, productions, and grammar metadata. · `grammar.h`
- [First/Follow analysis](https://github.com/cantrepro/cparse/blob/master/grammar.c): grammar analysis · Computes lookahead inputs and validates productions. · `grammar.c`
- [Dynamic utilities](https://github.com/cantrepro/cparse/blob/master/util.c): shared containers · Removes fixed grammar-size limits. · `util.c`

### Parser Generation

- [LR(1) automaton](https://github.com/cantrepro/cparse/blob/master/lr1_lalr1.c): state generator · Builds item sets, closure, and goto transitions. · `lr1_lalr1.c`
- [LALR state merge](https://github.com/cantrepro/cparse/blob/master/lr1_lalr1.c): table reduction · Merges compatible LR states for compact parsers. · `lr1_lalr1.c`
- [Action/goto tables](https://github.com/cantrepro/cparse/blob/master/lr1_lalr1.h): parser tables · Indexed terminal and nonterminal parse actions. · `lr1_lalr1.h`

### Runtime

- [Shift/reduce runtime](https://github.com/cantrepro/cparse/blob/master/lr1_lalr1.c): parser executor · Accepts input or builds source-spanned parse trees and structured errors. · `lr1_lalr1.c`

## Connections

- Static library build → cparse API: packages
- Expression example → cparse API: uses
- Expression example → clex lexer: configures
- cparse API → Grammar ingestion: parses grammar text
- Grammar ingestion → Grammar model: creates
- Grammar ingestion → Dynamic utilities: uses
- Grammar model → First/Follow analysis: analyzed by
- Dynamic utilities → First/Follow analysis: supports
- First/Follow analysis → LR(1) automaton: supplies lookaheads
- Grammar model → LR(1) automaton: defines productions
- LR(1) automaton → LALR state merge: optional compaction
- LR(1) automaton → Action/goto tables: builds LR(1) tables
- LALR state merge → Action/goto tables: builds LALR(1) tables
- cparse API → Action/goto tables: constructs parser
- Action/goto tables → Shift/reduce runtime: drives actions
- clex lexer → Shift/reduce runtime: supplies tokens
- cparse API → Shift/reduce runtime: accepts or parses
