classBisonGrammarCompiler
final class BisonGrammarCompiler
@visibility public.Turns a Bison grammar file into the grammar the generators walk.
The file is read with bison-parser; this class keeps what generation needs: every alternative of every rule as a sequence of terminals and nonterminals. A name is a nonterminal when a rule defines it and a terminal when %token declares it; a character literal is a terminal named by its character; actions, precedence marks, tags, aliases and string literals are left out. Alternatives of a rule defined in several places are joined in file order.
$grammar = (new \SqlFaker\Compiler\Bison\BisonGrammarCompiler())->compile("%token NUM\n%start expr\n%%\nexpr: expr '+' term { \$\$ = \$1 + \$3; } | term ;\nterm: NUM ;\n");
[$grammar->startSymbol, array_keys($grammar->ruleMap), count($grammar->ruleMap['expr']->alternatives)] // => ['expr', ['expr', 'term'], 2]Methods§
public function __construct(private Parser $parser = new \BisonParser\Parser())public function compile(string $source): GrammarCompiles a grammar file.
Parameters
$source | string | The text of the `.y` or `.yy` file |
Returns
Grammar The grammarThrows
SyntaxException When the file is not a grammar Bison accepts, which includes a file without rulesUnknownSymbolException When a rule uses a name that is neither a rule nor a declared tokenTest cases 4
Calls 8
- method-call Parser::parse() line 61
- method-call BisonGrammarCompiler::declaredTokens() line 67
- new Production line 72
- method-call BisonGrammarCompiler::symbols() line 72
- new ProductionRule line 76
- function-call
array_mergeline 76 - new Grammar line 79
- method-call BisonGrammarCompiler::startSymbol() line 79
public function declaredTokens(GrammarFile $file): array<string, true>Collects the names %token declares.
Parameters
$file | GrammarFile | The tree |
Returns
array<string, true> The namesTest cases 1
Called from 1
Calls 2
- method-call GrammarFile::allDeclarations() line 92
- class-const SymbolClass::Token() line 93
public function symbols(
Alternative $alternative,
array<string, true> $defined,
array<string, true> $declared,
): list<Symbol>Turns the symbols of an alternative into terminals and nonterminals.
Parameters
$alternative | Alternative | The alternative |
$defined | array<string, true> | Names rules define |
$declared | array<string, true> | Names `%token` declares |
Returns
list<Symbol> The symbols in order, string literals left outThrows
UnknownSymbolException When a name is neither defined nor declaredTest cases 2
Called from 1
Calls 6
- method-call Alternative::symbols() line 120
- class-const SymbolKind::CharLiteral() line 121
- new Terminal line 122
- class-const SymbolKind::String() line 123
- new NonTerminal line 126
- new UnknownSymbolException line 130
public function startSymbol(GrammarFile $file): stringFinds the start symbol: what %start names, or else the first rule.
Parameters
$file | GrammarFile | The tree, with at least one rule |
Returns
string The start symbolTest cases 1
Called from 1
Calls 2
- method-call GrammarFile::allDeclarations() line 146
- method-call GrammarFile::rules() line 152
Private surface 1§
Implementation details, listed for orientation only.
private Parser $parser = new \BisonParser\Parser()Test cases 8§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 8
BisonGrammarCompilerTest::testCompilecallsBisonGrammarCompilerTest::testCompileRejectsAFileWithoutRulescallsBisonGrammarCompilerTest::testCompileRejectsAnUndeclaredNamecallsBisonGrammarCompilerTest::testCompileTakesTheFirstRuleAsStartSymbolWithoutStartcallsBisonGrammarCompilerTest::testDeclaredTokenscallsBisonGrammarCompilerTest::testStartSymbolcallsBisonGrammarCompilerTest::testSymbolscallsBisonGrammarCompilerTest::testSymbolsRejectsAnUnknownNamecalls