How cantrepro/cparse works
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 cparse API · lr1_lalr1.c LR(1) automaton · lr1_lalr1.h Action/goto tables · grammar.c Grammar ingestion
Components 12
Public Surface
- cparse API · Construction, parsing, errors, trees, and ownership contracts.
cparse.h - Static library build · Produces libcparse.a.
Makefile - Expression example · Demonstrates lexer setup through tree cleanup.
examples/expr_parser.c - clex lexer · Borrowed token source owned by the caller.
clex
Grammar Pipeline
- Grammar ingestion · Converts textual productions into internal grammar data.
grammar.c - Grammar model · Symbols, productions, and grammar metadata.
grammar.h - First/Follow analysis · Computes lookahead inputs and validates productions.
grammar.c - Dynamic utilities · Removes fixed grammar-size limits.
util.c
Parser Generation
- LR(1) automaton · Builds item sets, closure, and goto transitions.
lr1_lalr1.c - LALR state merge · Merges compatible LR states for compact parsers.
lr1_lalr1.c - Action/goto tables · Indexed terminal and nonterminal parse actions.
lr1_lalr1.h
Runtime
- Shift/reduce runtime · Accepts input or builds source-spanned parse trees and structured errors.
lr1_lalr1.c
Connections 17
- 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