final class BisonGrammarReader
Public API: explicitly declared with @visibility public.

Turns a Bison grammar file into the grammar the automaton is built from.

The file is read with bison-parser; this class keeps what the automaton needs. %token names terminals, %left, %right, %nonassoc and %precedence rank them, %start and %expect are recorded, and every alternative becomes a rule. A mid-rule action becomes a hidden $@n nonterminal deriving the empty string, numbered before the rule it appears in, exactly as Bison numbers it. Character literals and strings on a right-hand side are terminals named by their value.

Reading a small grammardoctest
$grammar = (new \SqlParser\Compiler\BisonGrammarReader())->read("%token NUM\n%left '+'\n%%\nexpr: expr '+' expr | NUM ;\n");
    [count($grammar->rules), $grammar->symbols->name($grammar->startSymbol())] // => [3, "expr"]
Numbering a mid-rule action as Bison doesdoctest
$grammar = (new \SqlParser\Compiler\BisonGrammarReader())->read("%token A B\n%%\ns: A { act(); } B ;\n");
    array_map(static fn (\SqlParser\Grammar\Rule $rule): string => $grammar->symbols->name($rule->lhs), $grammar->rules) // => ['$accept', '$@1', 's']

Methods§

public function __construct(private Parser $parser = new \BisonParser\Parser())

Parameters

$parserParserReads Bison grammar files
Calls 1
public function read(string $source): Grammar

Reads a grammar file.

Parameters

$sourcestringThe text of the `.y` or `.yy` file

Returns

Grammar The grammar with Bison's precedence policy

Throws

SyntaxException When the file is not a grammar Bison accepts
GrammarException When the grammar is inconsistent
UnknownSymbolException When a rule uses an undeclared symbol
Test cases 4
Calls 8
public function declaration(Declaration $declaration, GrammarBuilder $builder): void

Records what a declaration says about symbols, precedence, the start symbol or expected conflicts.

Parameters

$declarationDeclarationThe declaration
$builderGrammarBuilderCollects the grammar
Test cases 1
Called from 1
Calls 6
public function associativity(Associativity $associativity): Associativity
public function alternative(
    string $lhs,
    Alternative $alternative,
    GrammarBuilder $builder,
): void

Adds one alternative as a rule, with hidden rules for its mid-rule actions.

Parameters

$lhsstringThe nonterminal being defined
$alternativeAlternativeThe alternative
$builderGrammarBuilderCollects the grammar
Test cases 1
Called from 1
Calls 4
public function continues(Alternative $alternative, int $index): bool

Reports whether a symbol, action or predicate follows an item, which makes an action there a mid-rule action.

Parameters

$alternativeAlternativeThe alternative
$indexintThe item's index

Returns

bool True when the alternative continues after the item
Test cases 2
Called from 1
Calls 1
public function midRule(GrammarBuilder $builder): string

Adds the hidden empty rule that stands for a mid-rule action.

Parameters

$builderGrammarBuilderCollects the grammar

Returns

string The `$@n` nonterminal
Test cases 1
Called from 1
Calls 1

Private surface 2§

Implementation details, listed for orientation only.

private int $midRuleCount = 0
private Parser $parser = new \BisonParser\Parser()

Test cases 10§

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

Dedicated tests 10