Home » Php » Lex and Yacc in PHP

Lex and Yacc in PHP

Posted by: admin April 23, 2020 Leave a comment


Is there an implementation of Lex and Yacc in PHP?

If not, can anyone suggest a lexical analyser and parser generator (ie, anything like Lex and Yacc) that will create PHP code. I’m not too worried about the performance of the resulting parser.

I am sick of using regex to parse things that really shouldn’t be parsed with regex…

How to&Answers:

There’s JLexPHP: https://github.com/wez/JLexPHP/blob/master/jlex.php

I’ve not used it, but there’s this: http://pear.php.net/package/PHP_ParserGenerator , which creates a PHP Parser from a Lemon grammar. The project seems to be inactive though.

I also found this project: http://code.google.com/p/antlrphpruntime/ , which uses Antlr. Again inactive though.


Been for looking for this kind of thing for a while. After finding this post, I’ve tried the ANTLR PHP runtime. I can report that it’s far from being finished. There are several errors in the generated code, where the original java runtime classes has not been properly translated to PHP (nested class declarations, using ‘.’ instead of ‘.’ when trying to access class methods operator).

The ANTLR framework itself is quite powerful (can’t attest to the efficiency of the generated code).
Especially the graphical tool ANTLRWorks makes it easy to create and debug grammas. Just too bad about the PHP version. It’s possible to roll your own though. The best solution may be to analyse the generated ANTLR runtime class, figure out how it’s works, and come up with a light weight less enterprisey version thereof.


Cheap trick: code a recursive descent parser. This will cover a lot of cases. See
Is there an alternative for flex/bison that is usable on 8-bit embedded systems?


Another sugestion: avoid Lex/Yacc approach, use PHP as a good string parser,

  • for simple tasks and simple translators: use perl-regular expressions (PCRE), with PHP preg_* functions. The callback have the same power of Awk or Yacc rules, but with PHP code (!).

  • for complex tasks: translate (with a PHP string or PCRE translator or another translator) your language to a XML dialect, process with DOM and/or XSLT. XSLT is “rule oriented” (se xsl:template) like Yacc. With XSLT you have also access to PHP functions with registerphpfunctions(). If need back to a non-XML language or a I/O complex format, process de output (a saved XML or a XSLT-output) again with PCRE and string functions.

    • PS: for more rich and complex languages, the “translation to XML” task is possible (see xSugar theory), but not always easy. You can use PHP-PEG to translate with PHP, or you can translate with a external tool, for cache the XML, or for use a permanent-xml-translated version of your specific-language-scripts.

These two options have the same (Lex and Yacc) power, and use only build-in PHP classes and functions.

For the complex cases, remember that XML, XSLT, etc. are W3C standards, then, XML-dialects are “standard formats”, XML-tools are optimized and still evolving, and XML-data are interchangeable.