What Is a Good Parser Generator for PHP

PHP Lexer and Parser Generator?

I'd propose to give ANTLR a try. ANTLRWorks might be helpful.

I wrote an LL(1) parser generator myself in pure PHP, since I wasn't aware of other PHP-based solutions.

Language parser library written in PHP

This is not a complete list, if you're looking for PHP runtime lexer/parsers, one exceptional project is Phlexy by NikiC.

You can find a use-case inside PHP-Parser as well written by him. That is a parser for the PHP language with an abstract syntax tree (AST), partially generated from a grammar file.

I never managed it to get that far yet, from my own research over the years, there are not many such projects in PHP userspace, and these two libraries from NikiC are really a very good example.

If you're looking for a lexer that follows more the flex rules, I have written one in XDOM that lexes CSS selector syntax, it's also with a parser but the parser is not based on a grammar file even though it exists in the CSS specs. The lexer is based on a .lex file.

Flex/Bison-like functionality within PHP

LIME Parser Generator for PHP:

Complete LALR(1) parser generator and
engine (like BISON or YACC) but it's
all done in PHP, and the input grammar
is easier and more maintainable. Write
your actions in PHP. Generate PHP
output code. Drive your parser with
PHP. Wanna make a language?


update:

Since I wrote the above, I see that there are some other tools for parser generation, announced here:

http://wezfurlong.org/blog/2006/nov/parser-and-lexer-generators-for-php/

Not sure if these are any better maintained now in 2014, but I know Wez Furlong, he was the original developer of PDO, and he is a very good developer.

Lemon Parser-Generator: Are nonterminals not evaluated?

Do you not want something like:

expr(A) ::= VALUE(B). {A = B; echo "got a value:".B.PHP_EOL;}

Library like pyparsing for PHP

I don't know any maintained parser generators written in PHP. But there are parser generators written in other languages with PHP as a target language. One I have personally used is kmyacc. There is a PHP and Windows compatible fork of it. The grammar for it is written in yacc format and can be compiled to PHP using this command:

kmyacc -l -m %PARSER_PROTOTYPE_FILE% -p %NAME% %GRAMMAR_FILE%

Kmyacc already comes with a procedural parser prototype file for PHP, but I personally use a modified version of an OOP based prototype.

As an example: This grammar get's compiled into this parser. (Note that the grammar is huge, that's why the generated parser has two and a half thousand lines. A "normal" grammar would obviously be far smaller.)



Related Topics



Leave a reply



Submit