We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 9 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
找这种东西有一段时间了。找到这篇文章后,我尝试了 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.
廉价的技巧:编写一个递归下降解析器。这将涵盖很多情况。看
是否有可在 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?
另一个建议:避免 Lex/Yacc 方法,使用 PHP 作为一个好的 string解析器,
用于简单任务和简单翻译器:使用 perl 正则表达式 (PCRE),以及 PHP
preg_*
函数。该回调具有与 Awk 或 Yacc 规则相同的功能,但使用 PHP 代码(!)。对于复杂任务:将您的语言翻译(使用 PHP 字符串或 PCRE 翻译器或其他翻译器)为 XML 方言,使用 DOM 进行处理 和/或 XSLT。 XSLT 与 Yacc 一样是“面向规则”(se xsl:template)。通过 XSLT,您还可以使用 registerphpfunctions() 访问 PHP 函数。如果需要返回非 XML 语言或 I/O 复杂格式,请处理输出 (保存的 XML 或 XSLT 输出)再次使用 PCRE 和字符串函数。
这两个选项具有相同的功能(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.
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.
有 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.