Using PHP_ParserGenerator
PHP_LexerGenerator is a parser generator for PHP 5 based on the Lemon
parser generator (http://www.hwaci.com/sw/lemon/). The simplest
way to use the parser generator is to use the command-line tool:
$ phplemon /path/to/Parser.y
This will generate files /path/to/Parser.php
and the
state information file /path/to/Parser.out
.
Information on the format of y
files is found in the
documentation for the Lemon Parser Generator.
Specific changes made to the grammar file are as follows:
%extra_argument is removed, as class constructor can be used to
pass in extra information
%token_type and company are irrelevant in PHP, and so are removed
%declare_class is added to define the parser class name and any
implements/extends information
%include_class is added to allow insertion of extra class information
such as constants, a class constructor, etc.
Other changes make the parser more robust, and also make reporting
syntax errors simpler. Detection of expected tokens eliminates some
problematic edge cases where an unexpected token could cause the parser
to simply accept input.
Otherwise, the file format is identical to the Lemon parser generator.
Generated parsers need a lexer that scans input for tokens such as a lexer
generated by PHP_LexerGenerator.
Usage of a generated parser should be something like this:
<?php
$parser = new parser($lex);
while ($lex->yylex()) {
$parser->doParse($lex->token
, $lex->value
);
}
?>
Especially important is the final passing in of 0 which tells the parser that
we have reached the end of the input file.
Until accepted, the package can be installed with
pear install http://pear.chiaraquartet.net/PHP_ParserGenerator/PHP_ParserGenerator-0.1.0.tgz
.
Direct download link here: http://pear.chiaraquartet.net/PHP_ParserGenerator/PHP_ParserGenerator-0.1.0.tgz