packagek-kinzal/lemon-parser

Parser for Lemon grammar files, producing a lossless syntax tree

Namespaces§

LemonParser 2 classes
LemonParser\Ast 6 classes
LemonParser\Ast\Declaration 1 interface 8 classes 3 enums
LemonParser\Preprocessor 3 classes
LemonParser\Printer 3 classes
LemonParser\Scanner 4 classes 1 enum
LemonParser\Syntax 6 classes

Documents 1§

Lemon ParserREADME.md

README§

Lemon Parser

License: MIT Docs PHP Version Ask DeepWiki

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.