ircmaxell/php-yacc
PHP port of kmyacc: a YACC/LALR(1) parser generator that takes a YACC grammar (plus a parser template) and generates a PHP parser. Useful for building fast parsers for structured/unstructured languages; generation is resource-heavy, parsing is fast.
Begin by installing via Composer: composer require --dev ircmaxell/php-yacc. Examine the examples/ directory — especially examples/php/grammar.y and template.php — to grasp YACC syntax and required parser template structure. The minimal workflow is:
simple.y) with terminals, non-terminals, and rulestemplate.php for your needs (handles parser class skeleton and action code injection)vendor/bin/phpyacc -f simple.y -t template.php -o SimpleParser.phpphpyacc in CI/pre-commit hooks; the parser class becomes static code. Avoid any runtime generation.SimpleParser::T_NUMBER) as lexer emission tokens. Ensure your lexer yields arrays like [token_id, value, line] for compatibility.-n flag (v0.0.7+) to name semantic values, e.g.:
%name expr
expr: expr '+' expr { $$ = new AddNode($expr_1, $expr_2); }
Avoids fragile $1, $2 references.#if defined('PHP7')) to support multiple language variants, inspired by PHP-Parser’s dual PHP5/7 support.T_NUM but grammar expects T_NUMBER) cause non-descriptive errors — log lexer output during debugging.$$, $1, $2, etc. — avoid <?php $var = $$ ... ?> in templates; use <?= sparingly and always with explicit defaults (<?= $action ?? '' ?>).phpyacc -v to generate detailed conflict logs — inspect them early in grammar design.%union, %type, and %left/%right associativity are not supported. Semantic type annotations like $<expr>{...} won’t work — rely on naming (-n) and convention.YACC_DEBUG=1 env var (check generated parser for YACC_DEBUG usage) or inspect debug() calls in the parser. Enable verbose output with phpyacc -v.ircmaxell/php-yacc:~0.0.7 and audit generated code in CI. For production-critical parsing, consider forking or migrating to nikic/php-parser (which uses kmyacc directly).How can I help you explore Laravel packages today?