-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This is Parse::Yapphp, a yacc-like parser compiler that generates fully reentrant LALR(1) PHP parser classes.
Please refer to the project's README.md and the POD sections in the respective source packages for in-depth (legacy) documentation.
Compilation will always create three files: the parser, the driver and a lexer interface.
yapphp -m My\\Project\\Parser\\Parser -d my/project/parser/ Parser.py
This will create Parser.php
, ParserDriver.php
and LexerInterface.php
in the directory my/project/parser
from the grammar file Parser.py
. The three PHP classes will reside in the My\Project\Parser
namespace.
This section will not dive deeply into how to write yacc-like parser grammar; it will highlight the differences introduced by this module though and how to access rule values in PHP.
# Arbitrary rule
statements : statements ';' statement
# This is the semantic action in PHP
{ return new StatementNode($_[1], $_[3]) }
| statement
# Second rule's semantic action
{ return new Statement($_[1]) }
;
Semantic actions are obviously written in PHP; they are included verbatim into the generated parser's rule array as anonymous functions. You MAY omit the terminating semicolon ';' for the final statement of the semantic action.
The argument to any semantic action is the array $_
that contains the semantic values of the rule starting at the index one (1). In the above example, the array $_
contains:
[
1 => object, // Either `Statement` or `StatementNode`
2 => ';',
3 => object // Of type `Statement`
]
Parse::Yapphp
is a port of Francois Desarmenien's CPAN package Parse::Yapp
. This project borrows heavily from it; merely the parser output has been modified by the author to create PHP output.