package lrgrep
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=e53de12e4c5cbe6bca00643593266b4f9fa2e3f6a138195eeff7a4329f5c1c75
sha512=7fd7c4d11506fea7cc11c9bbf5aea9142d905643553c0c90e1bb16b794106b0c14a266acf89ae91b6929008dc0ba6515f788642873e2e4ba6c3d49bd45d25127
doc/kernel/Kernel/Sentence_generation/index.html
Module Kernel.Sentence_generationSource
Generating parse sentences from transitions
This module provides functionality to generate concrete parse examples (sentences) from sequences of LR states or LR transitions. It's used for generating counterexamples and debug information.
Core algorithm:
- The algorithm works backwards from the desired parsing outcome to find a valid sequence of transitions that would produce that outcome.
to_transitionsconverts a sequence of LR states to a sequence of transitions that connect those states.
to_cellsmaps transitions to reduction graph cells, using dynamic programming to find the minimum-cost path through the reduction graph.
expand_cellsrecursively expands cells back to the original terminal symbols that would trigger the reductions.
Key data structures:
- Cells: Represent positions in the reduction graph, encoded as a compact triple (node, pre_class, post_class) for efficient storage and lookup. (Pre_class and post_class constrain the lookahead symbols that can precede and follow)
- The algorithm uses dynamic programming to find minimum-cost paths through the reduction graph.
Implementation details:
to_cellsuses a sophisticated dynamic programming approach where at each transition, it considers:- All post_classes of the current node
- For each, all pre_classes that can reach it with finite cost Then it keeps only the minimal cost paths
expand_cellshandles two cases:L tr: A transition node - either shift (return the terminal) or goto (recursively solve the subproblem with minimum cost)R (l, r): An inner node - decompose into left and right subproblems, finding solutions that minimize total cost
- The
Breakexception is used to short-circuit when a minimal-cost solution is found during the exploration of all possible decompositions.
- Nullable reductions need some special care. If a nullable reduction is possible and the lookahead classes allow it, the algorithm takes that path instead of the non-nullable one.
val find_transition :
'g Info.grammar ->
'g Kernel__Info.lr1 Fix.Indexing.index ->
'g Info.Lr1.n Fix.Indexing.index ->
('g Kernel__Info.goto_transition, 'g Kernel__Info.shift_transition)
Fix.Indexing.Sum.n
Fix.Indexing.indexFind the transition from LR state x to LR state y. Returns a goto transition if y is reached via a non-production, or a shift transition if y is reached via a terminal. Raises Invalid_argument if y is an entrypoint. Raises Not_found if there is no transition from x to y.
val to_transitions :
'a Info.grammar ->
'a Info.Lr1.n Fix.Indexing.index list ->
'a Info.Lr1.n Fix.Indexing.index
* ('a Kernel__Info.goto_transition, 'a Kernel__Info.shift_transition)
Fix.Indexing.Sum.n
Fix.Indexing.index
listConvert a sequence of LR states to the initial state and the list of transitions connecting consecutive states. Raises Invalid_argument if the input list is empty.
val to_cells :
'g Info.grammar ->
('g, 'cell) Reachability.t_cell ->
'g Info.transition Fix.Indexing.index list ->
'cell Fix.Indexing.index listMap a list of transitions to reduction graph cells, finding the minimum-cost path through the cost DAG using dynamic programming.
Processes transitions right-to-left. For each transition, iterates all post_classes and pre_classes, selecting the (pre, post) pair that minimizes the total cost: cost(cell) + cost(suffix).
Returns the list of cells along the minimum-cost path.
val expand_cells :
'g Info.grammar ->
('g, 'cell) Reachability.t_cell ->
'cell Fix.Indexing.index list ->
'g Info.terminal Fix.Indexing.index listRecursively expand reduction graph cells back to terminal symbols.
Handles two node types from the cost tree:
L tr: A leaf transition — shifts return the terminal symbol; gotos check for nullable reductions first, then recurse into the minimum-cost non-nullable reduction equation.R (l, r): An inner node — decomposes into left and right sub-problems via the coercion matrix, recursing into both children whose combined cost equals the current cost.
Uses a Break exception to short-circuit once a minimal-cost decomposition is found.
val sentence_of_transitions :
'g Info.grammar ->
'g Reachability.t ->
'g Info.transition Fix.Indexing.index list ->
'g Info.terminal Fix.Indexing.index listGenerate a terminal sentence from a list of transitions. Combines to_cells and expand_cells in a single pipeline.
val sentence_of_stack :
'g Info.grammar ->
'g Reachability.t ->
'g Info.Lr1.n Fix.Indexing.index list ->
'g Info.terminal Fix.Indexing.index listGenerate a terminal sentence from a list of LR states (a parse stack). Combines to_transitions, to_cells, and expand_cells in a single pipeline. The input list must be non-empty (see to_transitions).