classBisonGrammarReader
final class BisonGrammarReader
@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.
$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"]$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())public function read(string $source): GrammarReads a grammar file.
Parameters
$source | string | The text of the `.y` or `.yy` file |
Returns
Grammar The grammar with Bison's precedence policyThrows
SyntaxException When the file is not a grammar Bison acceptsGrammarException When the grammar is inconsistentUnknownSymbolException When a rule uses an undeclared symbolTest cases 4
Calls 8
- method-call Parser::parse() line 72
- new GrammarBuilder line 73
- method-call GrammarBuilder::policy() line 74
- class-const PrecedencePolicy::LastTerminal() line 74
- method-call GrammarBuilder::terminal() line 75
- method-call BisonGrammarReader::declaration() line 77
- method-call BisonGrammarReader::alternative() line 82
- method-call GrammarBuilder::build() line 86
public function declaration(Declaration $declaration, GrammarBuilder $builder): voidRecords what a declaration says about symbols, precedence, the start symbol or expected conflicts.
Parameters
$declaration | Declaration | The declaration |
$builder | GrammarBuilder | Collects the grammar |
Test cases 1
Called from 1
Calls 6
- method-call GrammarBuilder::precedence() line 102
- method-call BisonGrammarReader::associativity() line 102
- class-const SymbolClass::Token() line 103
- method-call GrammarBuilder::terminal() line 105
- method-call GrammarBuilder::start() line 108
- method-call GrammarBuilder::expect() line 110
public function associativity(Associativity $associativity): AssociativityMaps Bison's associativity onto the grammar's.
Parameters
$associativity | Associativity | As declared |
Returns
Associativity As the automaton resolves conflictsTest cases 1
Called from 1
Calls 8
- class-const Associativity::Left() line 124
- class-const Associativity::Left() line 124
- class-const Associativity::Right() line 125
- class-const Associativity::Right() line 125
- class-const Associativity::NonAssoc() line 126
- class-const Associativity::NonAssoc() line 126
- class-const Associativity::Precedence() line 127
- class-const Associativity::Precedence() line 127
public function alternative(
string $lhs,
Alternative $alternative,
GrammarBuilder $builder,
): voidAdds one alternative as a rule, with hidden rules for its mid-rule actions.
Parameters
$lhs | string | The nonterminal being defined |
$alternative | Alternative | The alternative |
$builder | GrammarBuilder | Collects the grammar |
Test cases 1
Called from 1
Calls 4
- method-call GrammarBuilder::terminal() line 145
- method-call BisonGrammarReader::continues() line 148
- method-call BisonGrammarReader::midRule() line 149
- method-call GrammarBuilder::rule() line 154
public function continues(Alternative $alternative, int $index): boolReports whether a symbol, action or predicate follows an item, which makes an action there a mid-rule action.
Parameters
$alternative | Alternative | The alternative |
$index | int | The item's index |
Returns
bool True when the alternative continues after the itemTest cases 2
Called from 1
Calls 1
- function-call
array_sliceline 167
public function midRule(GrammarBuilder $builder): stringAdds the hidden empty rule that stands for a mid-rule action.
Parameters
$builder | GrammarBuilder | Collects the grammar |
Returns
string The `$@n` nonterminalTest cases 1
Called from 1
Calls 1
- method-call GrammarBuilder::rule() line 186
Private surface 2§
Implementation details, listed for orientation only.
private int $midRuleCount = 0private 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
BisonGrammarReaderTest::testAlternativecallsBisonGrammarReaderTest::testAssociativitycallsBisonGrammarReaderTest::testContinuescallsBisonGrammarReaderTest::testContinuesLooksOnlyAtWhatFollowsTheItemcallsBisonGrammarReaderTest::testDeclarationcallsBisonGrammarReaderTest::testMidRulecallsBisonGrammarReaderTest::testReadcallsBisonGrammarReaderTest::testReadNumbersMidRuleActionsBeforeTheirRulecallsBisonGrammarReaderTest::testReadNumbersMidRuleActionsFromOneForEveryFilecallsBisonGrammarReaderTest::testReadRejectsWhatBisonRejectscalls