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

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 $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

Methods§

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

$symbolsSymbolTableEvery symbol, numbered
$ruleslist<Rule>Every rule, the augmented start rule first
$precedencesarray<int, Precedence>Declared precedence by terminal
$policyPrecedencePolicyWhich terminal lends an unranked rule its precedence
$expectedConflictsint|nullShift/reduce conflicts the grammar declares as expected
$tokenClassesarray<int, list<int>>Member terminals by token-class terminal
$fallbacksarray<int, int>Terminal to retry with by the terminal that failed
$wildcardint|nullTerminal that stands for any other, if any

Throws

GrammarException When the rules are not numbered in order
Calls 2
public function rulesOf(int $nonterminal): list<int>
public function precedenceOf(int $terminal): Precedence|null

Answers the precedence declared for a terminal, if any.

Parameters

$terminalintTerminal number

Returns

Precedence|null The declaration, or null when the terminal is unranked
Test cases 1
Called from 2
public function rulePrecedence(Rule $rule): Precedence|null

Answers 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

$ruleRuleRule to rank

Returns

Precedence|null The declaration, or null when nothing ranks the rule
Test cases 3
Called from 3
Calls 4
public function memberPrecedence(int $terminal): Precedence|null

Answers the precedence of a terminal, looking through a token class.

Parameters

$terminalintTerminal or token-class number

Returns

Precedence|null The first declaration found, or null when none is ranked
Test cases 1
Called from 2
Calls 1
public function startSymbol(): int

Answers the nonterminal the augmented start rule derives from.

Returns

int Number of the grammar's own start symbol
Test cases 1

Private surface 1§

Implementation details, listed for orientation only.

private array<int, list<int>> $rulesByLhs

Test cases 25§

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

Dedicated tests 8
Other tests reaching this symbol 17

Relations§

Instantiated in 1
Method calls 7
Type declarations 12