Lex and Yacc in PHP [closed]

五迷三道 提交于 2019-12-17 15:36:13

问题


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...


回答1:


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.




回答2:


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.




回答3:


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?




回答4:


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.



来源:https://stackoverflow.com/questions/2093228/lex-and-yacc-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!