final class BisonGrammarCompiler
Public API: explicitly declared with @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.

Compiling a small grammardoctest
$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())

Parameters

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

Compiles a grammar file.

Parameters

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

Returns

Grammar The grammar

Throws

SyntaxException When the file is not a grammar Bison accepts, which includes a file without rules
UnknownSymbolException When a rule uses a name that is neither a rule nor a declared token
Test cases 4
Calls 8
public function declaredTokens(GrammarFile $file): array<string, true>

Collects the names %token declares.

Parameters

$fileGrammarFileThe tree

Returns

array<string, true> The names
Test cases 1
Called from 1
Calls 2
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

$alternativeAlternativeThe alternative
$definedarray<string, true>Names rules define
$declaredarray<string, true>Names `%token` declares

Returns

list<Symbol> The symbols in order, string literals left out

Throws

UnknownSymbolException When a name is neither defined nor declared
Test cases 2
Called from 1
Calls 6
public function startSymbol(GrammarFile $file): string

Finds the start symbol: what %start names, or else the first rule.

Parameters

$fileGrammarFileThe tree, with at least one rule

Returns

string The start symbol
Test cases 1
Called from 1
Calls 2

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