k-kinzal / bison-parser
Parser for GNU Bison grammar files, producing a lossless syntax tree
Requires
- php: ^8.1
Requires (Dev)
- behat/behat: ^3.14
- brianium/paratest: ^7.3
- deptrac/deptrac: ^3.0
- friendsofphp/php-cs-fixer: ^3.95.25
- k-kinzal/php-ai-toolkit: dev-main
- phpbench/phpbench: ^1.4
- phpcompatibility/php-compatibility: ^10.0.0-alpha2
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^4.0.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-28 02:32:35 UTC
README
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.