final class ParseTableBuilder
Restricted visibility: declared "@visibility root". Code outside that scope must not name this declaration.

Builds the LALR(1) parse table of a grammar.

Each state keeps its shifts, its gotos and the reductions that could not become its default. The default is the reduction with the most lookahead terminals, and a state with one reduction and nothing to shift reduces without looking at all. A state that acts on the wildcard keeps every reduction explicit, as Lemon does, so the wildcard cannot swallow a token the state would rather reduce on. Token classes are spelled out to their members before conflicts are settled, as Lemon does, so a lexer never has to know about them.

Methods§

public function __construct(private Lr0Builder $lr0 = new \SqlParser\Automaton\Lr0Builder())

Parameters

$lr0Lr0BuilderBuilds the LR(0) automaton
Calls 1
public function build(Grammar $grammar): BuildResult

Builds the table.

Parameters

$grammarGrammarGrammar to build for

Returns

BuildResult The table and the conflicts settled by default
Test cases 11
Calls 20
public function defaultRule(
    ResolvedState $resolved,
    bool $consistent,
    bool $accepting,
): int|null

Chooses the rule a state reduces by when no explicit action applies.

Parameters

$resolvedResolvedStateThe state's settled actions
$consistentboolWhether the state shifts nothing
$acceptingboolWhether the state completes the augmented start rule

Returns

int|null The rule, or null when the state has nothing to reduce
Test cases 1
Called from 1
Calls 2
public function withoutDefault(array<int, int> $actions, int|null $default): array<int, int>

Drops the explicit reductions the default already covers.

Parameters

$actionsarray<int, int>Action code by terminal
$defaultint|nullRule the state reduces by default

Returns

array<int, int> The remaining explicit actions
Test cases 1
Called from 1
Calls 2
public function expandShifts(array<int, int> $shifts, Grammar $grammar): array<int, int>

Replaces a shift on a token class by the same shift on every member.

Lemon adds one shift per member, so a member shifted by two classes is a shift/shift conflict it would reject; the first class keeps the member.

Parameters

$shiftsarray<int, int>Target state by terminal or token class
$grammarGrammarGrammar declaring the classes

Returns

array<int, int> Target state by terminal
Test cases 1
Called from 1
public function expandLookaheads(
    array<int, array<int, int>> $lookaheads,
    Grammar $grammar,
): array<int, array<int, int>>

Replaces a token class in a lookahead set by its members.

Parameters

$lookaheadsarray<int, array<int, int>>Lookahead bitset by completed rule
$grammarGrammarGrammar declaring the classes

Returns

array<int, array<int, int>> Lookaheads over terminals only
Test cases 1
Called from 1
Calls 2

Private surface 1§

Implementation details, listed for orientation only.

private Lr0Builder $lr0 = new \SqlParser\Automaton\Lr0Builder()

Test cases 15§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 9
Other tests reaching this symbol 6