documentBison Parser
Bison Parser
License: MIT Docs PHP Version Ask DeepWiki
Bison Parser reads GNU Bison grammar files (.y, .yy) into a syntax tree that keeps everything the file says, with the position of each part. It reads the language the GNU Bison manual defines, so a file Bison accepts is read as Bison reads it and a file Bison rejects raises an error at the same place. The manual is the specification, and spec/features states it clause by clause as scenarios that run the parser.
It reads grammars; it does not generate parsers. The tree is for tools that analyse, transform, document or generate from Bison grammars.
Requirements
- PHP 8.1+
Installation
composer require k-kinzal/bison-parser
Usage
use BisonParser\Parser;
$file = (new Parser())->parse(file_get_contents('grammar.y'));
foreach ($file->declarations as $declaration) {
echo $declaration::class, ' at ', $declaration->location(), "\n";
}
foreach ($file->rules() as $rule) {
echo $rule->name->value, ': ', count($rule->alternatives), " alternatives\n";
foreach ($rule->alternatives as $alternative) {
foreach ($alternative->symbols() as $symbol) {
echo ' ', $symbol->kind->value, ' ', $symbol->value, "\n";
}
}
}
A tree prints back as a grammar file, and printing is stable, which is how a tree can be checked:
use BisonParser\Printer\Printer;
$text = (new Printer())->print($file);
Every node of the tree is described in the API documentation.
License
MIT License. See LICENSE for details.