k-kinzal / lemon-parser
Parser for Lemon 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:15 UTC
README
Lemon Parser reads grammar files of the Lemon parser generator into a syntax tree that keeps everything the file says, with the position of each part. It reads the language the Lemon manual defines, so a file Lemon accepts is read as Lemon reads it and a file Lemon rejects raises an error with Lemon's message. 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 Lemon grammars.
Requirements
- PHP 8.1+
Installation
composer require k-kinzal/lemon-parser
Usage
%ifdef regions are settled with the names passed to parse(), as Lemon's -D option does.
use LemonParser\Parser; $file = (new Parser())->parse(file_get_contents('parse.y'), ['SQLITE_OMIT_WINDOWFUNC']); foreach ($file->rules() as $rule) { echo $rule->lhs->name, ' ::='; foreach ($rule->items as $item) { echo ' ', implode('|', array_map(fn ($symbol) => $symbol->name, $item->symbols)); } echo $rule->precedence === null ? '' : " [{$rule->precedence->name}]", "\n"; } foreach ($file->declarations() as $declaration) { echo $declaration::class, ' at ', $declaration->location(), "\n"; }
A tree prints back as a grammar file, and printing is stable, which is how a tree can be checked:
use LemonParser\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.