classParseTableBuilder
final class ParseTableBuilder
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
$lr0 | Lr0Builder | Builds the LR(0) automaton |
Calls 1
- new Lr0Builder line 32
public function build(Grammar $grammar): BuildResultBuilds the table.
Parameters
$grammar | Grammar | Grammar to build for |
Returns
BuildResult The table and the conflicts settled by defaultTest cases 11
ParseTableBuilderTest::testBuildCountsConflictsItSettlesByDefaultcallsParseTableBuilderTest::testBuildKeepsReductionsExplicitWhereTheWildcardActscallsParseTableBuilderTest::testBuildMakesANonAssociativeOperatorAnErrorcallsParseTableBuilderTest::testBuildSettlesAnAmbiguousGrammarWithPrecedencecallsParseTableBuilderTest::testBuildSpellsTokenClassesOutToTheirMemberscallsLrParserTest::testExpectedcallsLrParserTest::testParseBuildsATreeNamedAfterTheRulescallsLrParserTest::testParseGivesTheTreeTheTextThatFollowsTheLastTokencallsLrParserTest::testParseLeavesMidRuleActionsOutOfTheTreecallsLrParserTest::testParseRejectsAnUnexpectedTokencallsLrParserTest::testParseSuppliesTheEndMarkerWhenTheStreamLacksItcalls
Calls 20
- method-call Lr0Builder::build() line 45
- new LookaheadSets line 46
- new NullableSet line 46
- new ConflictResolver line 47
- static-call ActionCode::shift() line 60
- method-call ConflictResolver::resolve() line 63
- method-call ParseTableBuilder::expandShifts() line 63
- method-call ParseTableBuilder::expandLookaheads() line 63
- method-call LookaheadSets::of() line 63
- method-call ParseTableBuilder::defaultRule() line 67
- function-call
in_arrayline 67 - class-const ActionCode::ERROR() line 68
- static-call ActionCode::reduce() line 68
- method-call ParseTableBuilder::withoutDefault() line 69
- function-call
array_mapline 71 - new TableRule line 72
- new ParseTable line 75
- new ArrayRows line 75
- new BuildResult line 77
- new ConflictSummary line 77
public function defaultRule(
ResolvedState $resolved,
bool $consistent,
bool $accepting,
): int|nullChooses the rule a state reduces by when no explicit action applies.
Parameters
$resolved | ResolvedState | The state's settled actions |
$consistent | bool | Whether the state shifts nothing |
$accepting | bool | Whether the state completes the augmented start rule |
Returns
int|null The rule, or null when the state has nothing to reduceTest cases 1
Called from 1
public function withoutDefault(array<int, int> $actions, int|null $default): array<int, int>Drops the explicit reductions the default already covers.
Parameters
$actions | array<int, int> | Action code by terminal |
$default | int|null | Rule the state reduces by default |
Returns
array<int, int> The remaining explicit actionsTest cases 1
Called from 1
Calls 2
- static-call ActionCode::reduce() line 124
- function-call
array_filterline 126
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
$shifts | array<int, int> | Target state by terminal or token class |
$grammar | Grammar | Grammar declaring the classes |
Returns
array<int, int> Target state by terminalTest 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
$lookaheads | array<int, array<int, int>> | Lookahead bitset by completed rule |
$grammar | Grammar | Grammar declaring the classes |
Returns
array<int, array<int, int>> Lookaheads over terminals onlyTest cases 1
Called from 1
Calls 2
- static-call Bitset::has() line 170
- static-call Bitset::add() line 174
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
ParseTableBuilderTest::testBuildCountsConflictsItSettlesByDefaultcallsParseTableBuilderTest::testBuildKeepsReductionsExplicitWhereTheWildcardActscallsParseTableBuilderTest::testBuildMakesANonAssociativeOperatorAnErrorcallsParseTableBuilderTest::testBuildSettlesAnAmbiguousGrammarWithPrecedencecallsParseTableBuilderTest::testBuildSpellsTokenClassesOutToTheirMemberscallsParseTableBuilderTest::testDefaultRulecallsParseTableBuilderTest::testExpandLookaheadscallsParseTableBuilderTest::testExpandShiftscallsParseTableBuilderTest::testWithoutDefaultcalls
Other tests reaching this symbol 6
LrParserTest::testExpectedcallsLrParserTest::testParseBuildsATreeNamedAfterTheRulescallsLrParserTest::testParseGivesTheTreeTheTextThatFollowsTheLastTokencallsLrParserTest::testParseLeavesMidRuleActionsOutOfTheTreecallsLrParserTest::testParseRejectsAnUnexpectedTokencallsLrParserTest::testParseSuppliesTheEndMarkerWhenTheStreamLacksItcalls