package lrgrep

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Kernel.CodegenSource

Code generation for the LR matching machine

This module generates the OCaml code for the LR matching machine from the compiled specification and the generated automaton. The "machine" is translated to a sparse transition table, a bytecode program. An OCaml wrapper is generated to interpret the bytecode and invoke the appropriate semantic actions.

Main components:

  • The spec type holds global configuration including the parser name and lexer definition.
  • The output_header, output_trailer functions generate the outer module structure wrapping the generated code.
  • The output_rule function is the core code generation function that:
  • Outputs the bytecode and transition tables for the abstract machine
  • Outputs semantic actions for each branch
  • Outputs wrapper functions glueing the interpreter to user actions

Implementation details:

  • The output_table function formats and outputs the already-compacted bytecode and transition table as an OCaml Lrgrep_runtime.program record. The actual compaction is performed by Lrgrep_support.compact inside output_rule before output_table is called.
  • The output_execute_function generates a case analysis matching on:
  • The clause number
  • The lookahead token
  • For each branch that accepts the clause, outputs a case that: 1. Binds registers to captured variables 2. Executes the semantic action 3. Returns the result
  • The bind_capture function handles different capture types:
  • Value: Regular captured values with full type information
  • Start_loc, End_loc: Start/end position captures
  • It handles optional vs required captures based on whether they can be undefined
  • The lookahead_constraint function generates pattern matching for branches with lookahead constraints, ensuring only the right tokens trigger each branch.
  • The output_rule function proceeds in three steps: 1. Compacts the state machine via Lrgrep_support.compact and outputs the bytecode and transition tables 2. Generates semantic actions with proper variable binding, including type recovery for captured values 3. Outputs a wrapper function to glue the interpreter to user actions
Sourcetype priority = int
Sourcetype spec = {
  1. parser_name : string;
  2. lexer_definition : Syntax.lexer_definition;
}
Sourceval print_literal_code : Utils.Code_printer.t -> (Stdlib.Lexing.position * string) -> unit
Sourceval output_table : Utils.Code_printer.t -> Syntax.rule -> ('g, 'r, 'a, 'b) Automata.Machine.t -> (string * Lrgrep_support_packer.table * int array) -> unit
Sourceval output_wrapper : Utils.Code_printer.t -> Syntax.rule -> unit
Sourcetype printer = Utils.Code_printer.t option -> unit
Sourceval grammar_parameters : 'a Info.grammar -> string list
Sourceval output_header : 'g Info.grammar -> spec -> printer
Sourceval output_trailer : 'g Info.grammar -> spec -> printer
Sourceval output_rule : 'g Info.grammar -> spec -> Syntax.rule -> ('g, 'r) Spec.clauses -> ('g, 'r) Spec.branches -> ('g, 'r, 'a, 'b) Automata.Machine.t -> printer