PHP 中的 Lex 和 Yacc

发布于 2024-08-18 04:49:06 字数 1539 浏览 8 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不回头走下去 2024-08-25 04:49:21

找这种东西有一段时间了。找到这篇文章后,我尝试了 ANTLR PHP 运行时。我可以报告说,它还远未完成。生成的代码中有几个错误,原始 java 运行时类尚未正确转换为 PHP(嵌套类声明,在尝试访问类方法运算符时使用“.”而不是“.”)。

ANTLR框架本身相当强大(无法证明生成代码的效率)。
特别是图形工具 ANTLRWorks 可以轻松创建和调试语法。 PHP 版本太糟糕了。不过,您可以自己推出。最好的解决方案可能是分析生成的 ANTLR 运行时类,弄清楚它是如何工作的,并提出一个轻量级的企业版本。

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.

墨落成白 2024-08-25 04:49:21

廉价的技巧:编写一个递归下降解析器。这将涵盖很多情况。看
是否有可在 8 位嵌入式系统上使用的 Flex/bison 替代方案?

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?

情独悲 2024-08-25 04:49:21

另一个建议:避免 Lex/Yacc 方法,使用 PHP 作为一个好的 string解析器,

这两个选项具有相同的功能(Lex 和 Yacc),并且仅使用内置 PHP 类和函数。

对于复杂的情况,请记住 XML、XSLT 等是 W3C 标准,然后, XML 方言是“标准格式”,XML 工具经过优化且仍在不断发展,XML 数据是可互换的。

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.

九命猫 2024-08-25 04:49:20

有 JLexPHP: https://github.com/wez/JLexPHP/blob/master /jlex.php

我没有使用过它,但是有这个: http://pear .php.net/package/PHP_ParserGenerator ,它从 Lemon 创建 PHP 解析器> 语法。但该项目似乎不活跃。

我还找到了这个项目:http://code.google.com/p/antlrphpruntime/ ,它使用 Antlr。不过又不活跃了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文