classGrammar
final class Grammar
A context-free grammar with the precedence declarations that disambiguate it.
The grammar is augmented: rule zero derives the start symbol followed by the end marker. Token classes, fallbacks and the wildcard are the lexical hints a Lemon grammar carries; a Bison grammar declares none of them.
Properties§
public SymbolTable $symbolspublic list<Rule> $rulespublic array<int, Precedence> $precedences = []public PrecedencePolicy $policy = PrecedencePolicy::LastTerminalpublic int|null $expectedConflicts = nullpublic array<int, list<int>> $tokenClasses = []public array<int, int> $fallbacks = []public int|null $wildcard = nullMethods§
public function __construct(
public SymbolTable $symbols,
public list<Rule> $rules,
public array<int, Precedence> $precedences = [],
public PrecedencePolicy $policy = PrecedencePolicy::LastTerminal,
public int|null $expectedConflicts = null,
public array<int, list<int>> $tokenClasses = [],
public array<int, int> $fallbacks = [],
public int|null $wildcard = null,
)Parameters
$symbols | SymbolTable | Every symbol, numbered |
$rules | list<Rule> | Every rule, the augmented start rule first |
$precedences | array<int, Precedence> | Declared precedence by terminal |
$policy | PrecedencePolicy | Which terminal lends an unranked rule its precedence |
$expectedConflicts | int|null | Shift/reduce conflicts the grammar declares as expected |
$tokenClasses | array<int, list<int>> | Member terminals by token-class terminal |
$fallbacks | array<int, int> | Terminal to retry with by the terminal that failed |
$wildcard | int|null | Terminal that stands for any other, if any |
Throws
GrammarException When the rules are not numbered in orderCalls 2
- class-const PrecedencePolicy::LastTerminal() line 39
- new GrammarException line 48
public function rulesOf(int $nonterminal): list<int>Answers the rules that derive one nonterminal.
Parameters
$nonterminal | int | Nonterminal number |
Returns
list<int> Rule indexes in declaration orderTest cases 1
GrammarTest::testRulesOfcalls
Called from 3
- method-call SqlParser\Automaton\ClosureIndex::__construct() packages/sql-parser/src/Automaton/ClosureIndex.php:55
- method-call SqlParser\Automaton\ClosureIndex::reachable() packages/sql-parser/src/Automaton/ClosureIndex.php:85
- method-call SqlParser\Automaton\LookaheadSets::__construct() packages/sql-parser/src/Automaton/LookaheadSets.php:55
public function precedenceOf(int $terminal): Precedence|nullAnswers the precedence declared for a terminal, if any.
Parameters
$terminal | int | Terminal number |
Returns
Precedence|null The declaration, or null when the terminal is unrankedTest cases 1
public function rulePrecedence(Rule $rule): Precedence|nullAnswers the precedence a rule carries into conflict resolution.
A rule that names a terminal takes that terminal's declaration. Otherwise the policy picks a terminal from the right-hand side, and a token class in that position lends the declaration of its first ranked member.
Parameters
$rule | Rule | Rule to rank |
Returns
Precedence|null The declaration, or null when nothing ranks the ruleTest cases 3
Called from 3
- method-call SqlParser\Automaton\ConflictResolver::shiftOrReduce() packages/sql-parser/src/Automaton/ConflictResolver.php:95
- method-call SqlParser\Automaton\ConflictResolver::reduceOrReduce() packages/sql-parser/src/Automaton/ConflictResolver.php:128
- method-call SqlParser\Automaton\ConflictResolver::reduceOrReduce() packages/sql-parser/src/Automaton/ConflictResolver.php:129
Calls 4
- method-call Grammar::precedenceOf() line 93
- method-call SymbolTable::isTerminal() line 97
- method-call Grammar::memberPrecedence() line 100
- class-const PrecedencePolicy::LastTerminal() line 101
public function memberPrecedence(int $terminal): Precedence|nullAnswers the precedence of a terminal, looking through a token class.
Parameters
$terminal | int | Terminal or token-class number |
Returns
Precedence|null The first declaration found, or null when none is rankedTest cases 1
Called from 2
Calls 1
- method-call Grammar::precedenceOf() line 121
public function startSymbol(): intAnswers the nonterminal the augmented start rule derives from.
Returns
int Number of the grammar's own start symbolTest cases 1
Private surface 1§
Implementation details, listed for orientation only.
private array<int, list<int>> $rulesByLhsTest cases 25§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 8
GrammarTest::testMemberPrecedenceLooksThroughATokenClasscallsGrammarTest::testPrecedenceOfcallsGrammarTest::testRulePrecedencePrefersTheNamedTerminalcallsGrammarTest::testRulePrecedenceTakesTheFirstRankedTerminalUnderLemonsPolicycallsGrammarTest::testRulePrecedenceTakesTheLastTerminalUnderBisonsPolicycallsGrammarTest::testRulesMustBeNumberedInOrdercallsGrammarTest::testRulesOfcallsGrammarTest::testStartSymbolcalls
Other tests reaching this symbol 17
ClosureIndexTestcallsConflictResolverTest::testReduceOrReducecallsConflictResolverTest::testResolveCountsAnUnrankedShiftReduceConflictAndShiftscallsConflictResolverTest::testResolveCountsAReduceReduceConflictAndKeepsTheEarlierRulecallsConflictResolverTest::testResolveKeepsPlainShiftsAndReductionscallsConflictResolverTest::testResolveLetsAHigherRankedLaterRuleWinUnderLemonsPolicycallsConflictResolverTest::testShiftOrReducecallsConflictResolverTest::testShiftOrReduceLeavesABareRankUnresolvedcallsLookaheadSetsTestcallsLr0BuilderTestcallsNullableSetTestcallsParseTableBuilderTest::testExpandLookaheadscallsParseTableBuilderTest::testExpandShiftscallsBisonGrammarReaderTestcallsLemonGrammarReaderTestcallsGrammarBuilderTestcallsLrParserTestcalls
Relations§
Instantiated in 1
Method calls 7
- method-call SqlParser\Automaton\ClosureIndex::__construct() packages/sql-parser/src/Automaton/ClosureIndex.php:55
- method-call SqlParser\Automaton\ClosureIndex::reachable() packages/sql-parser/src/Automaton/ClosureIndex.php:85
- method-call SqlParser\Automaton\ConflictResolver::shiftOrReduce() packages/sql-parser/src/Automaton/ConflictResolver.php:94
- method-call SqlParser\Automaton\ConflictResolver::shiftOrReduce() packages/sql-parser/src/Automaton/ConflictResolver.php:95
- method-call SqlParser\Automaton\ConflictResolver::reduceOrReduce() packages/sql-parser/src/Automaton/ConflictResolver.php:128
- method-call SqlParser\Automaton\ConflictResolver::reduceOrReduce() packages/sql-parser/src/Automaton/ConflictResolver.php:129
- method-call SqlParser\Automaton\LookaheadSets::__construct() packages/sql-parser/src/Automaton/LookaheadSets.php:55
Type declarations 12
- type SqlParser\Automaton\ClosureIndex packages/sql-parser/src/Automaton/ClosureIndex.php:41
- type SqlParser\Automaton\ClosureIndex packages/sql-parser/src/Automaton/ClosureIndex.php:79
- type SqlParser\Automaton\ConflictResolver packages/sql-parser/src/Automaton/ConflictResolver.php:29
- type SqlParser\Automaton\LookaheadSets packages/sql-parser/src/Automaton/LookaheadSets.php:35
- type SqlParser\Automaton\Lr0Builder packages/sql-parser/src/Automaton/Lr0Builder.php:28
- type SqlParser\Automaton\NullableSet packages/sql-parser/src/Automaton/NullableSet.php:26
- type SqlParser\Automaton\ParseTableBuilder packages/sql-parser/src/Automaton/ParseTableBuilder.php:43
- type SqlParser\Automaton\ParseTableBuilder packages/sql-parser/src/Automaton/ParseTableBuilder.php:140
- type SqlParser\Automaton\ParseTableBuilder packages/sql-parser/src/Automaton/ParseTableBuilder.php:163
- type SqlParser\Compiler\BisonGrammarReader packages/sql-parser/src/Compiler/BisonGrammarReader.php:70
- type SqlParser\Compiler\LemonGrammarReader packages/sql-parser/src/Compiler/LemonGrammarReader.php:68
- type SqlParser\Grammar\GrammarBuilder packages/sql-parser/src/Grammar/GrammarBuilder.php:218