使用 lex 和 Yacc 解析 php
我想创建一个 php 解析器。我正在考虑使用 lex/yacc 来完成该任务。
这是个好主意吗?还是有更好的方法?
我在哪里可以找到已经为 php 语言制作的 lex/yacc 。或者至少是语法?
i want to create a php parser.i was thinking of using lex/yacc for the task.
is that a good idea ? or there are better ways ?
where can i find an already made lex/yacc for php language. or at least the grammer ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅 Zend/zend_language_parser.y 。
See Zend/zend_language_parser.y .
请参阅我们的 PHP 前端。它不是使用传统的 LEX 和 YACC 构建的,但其优点是需要付出巨大的努力来定义强大的解析器,并在这一点上进行数年的严格测试。使用完全详尽的语法,它可以解析完整的 PHP5(这很难,该语言定义不明确),包括获取有关内插字符串值的所有详细信息,自动构建 AST,并且可以将 AST 漂亮地打印回完全有效的 PHP 源文本,并保留布局,包括评论。
总是有超越解析的生命。需要工具来支持分析或修改代码以达到实际目的。 PHP 前端的底层引擎 DMS 软件重新工程工具包提供了 LEX - 我们使用类似 YACC 的机制,此外还提供了按程序导航/检查/修改 AST 的能力,为模式匹配和/或代码转换提供源到源重写规则,支持构建符号表,并提供控制和数据流分析框架。这种机制使您更容易专注于完成工作,而不是在遇到需要时尝试临时实施此类机制。 (DMS 实际上使用前端作为集成手段来处理多种语言)。
See our PHP Front End. This is not built using traditional LEX and YACC, but has the advantage of a huge effort to define a robust parser, and hard knock testing over several years at this point. Using a fully elaborated grammar, it parses full PHP5 (that's hard, the language is poorly defined), including picking up all the details about interpolated string values, automatically builds ASTs, and can prettyprint ASTs back to completely valid PHP source text preserving layout, including comments.
Invariably there is life beyond parsing. and one needs tools to support the analysis or modification of the code to achieve the actual purpose. The engine underlying the PHP Front End, the DMS Software Reengineering Toolkit, provides the LEX- and YACC-like machinery we use and additionally provides the ability to procedurally navigate/inspect/modify the ASTs, provides source-to-source rewrite rules for pattern-matching and/or transformation of code, supports building of symbol tables, and provides a control and data flow analysis framework. This machinery makes it much easier to focus on getting the job done, rather than trying to implement such machinery ad hoc as you encounter the need. (DMS actually handles a wide variety of languages using front ends as means of integration).
你可以在rphp_grammar的github中看到rphp_grammar.y rphp_语法
You can see the rphp_grammar.y in rphp_grammar's github rphp_grammar