[ class tree: PHP_ParserGenerator ] [ index: PHP_ParserGenerator ] [ all elements ]

PHP_ParserGenerator Manual

Introduction to PHP_ParserGenerator

by Gregory Beaver

Using PHP_ParserGenerator

PHP_LexerGenerator is a parser generator for PHP 5 based on the Lemon parser generator ( 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:

    1. <?php
    2. $lex = new lexer(file_get_contents($lexerfile));
    3. $parser = new parser($lex);
    4. while ($lex->yylex()) {
    5.     $parser->doParse($lex->token$lex->value);
    6. }
    7. $this->parser->doParse(00);
    8. ?>

    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 Direct download link here:

    Documentation generated on Mon, 11 Mar 2019 15:40:54 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.