任何用 PHP 编写的体面的 PHP 解析器? [英] Any decent PHP parser written in PHP?

查看:31
本文介绍了任何用 PHP 编写的体面的 PHP 解析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了很多操作和分析 PHP 代码的工作.通常我只使用 Tokenizer 来做到这一点.对于大多数应用程序,这已经足够了.但有时使用词法分析器进行解析不够可靠(显然).

I do lots of work manipulating and analyzing PHP code. Normally I just use the Tokenizer to do this. For most applications this is sufficient. But sometimes parsing using a lexer just isn't reliable enough (obviously).

因此我正在寻找一些用 PHP 编写的 PHP 解析器.我找到了 hnw/PhpParserkumatch/stagehand-php-parser.两者都是通过将 zend_language_parser.y 自动转换为使用 PHP 而不是的 .y 文件创建的C(然后编译为 LALR(1) 解析器).但是这种自动转换无法使用.

Thus I am looking for some PHP parser written in PHP. I found hnw/PhpParser and kumatch/stagehand-php-parser. Both are created by an automated conversion of zend_language_parser.y to a .y file with PHP instead of C (and then compiled to a LALR(1) parser). But this automated conversion just can't be worked with.

那么,有没有用 PHP 编写的像样的 PHP 解析器?(我需要一个用于 PHP 5.2 和一个用于 5.3.但其中之一也是一个很好的起点.)

So, is there any decent PHP parser written in PHP? (I need one for PHP 5.2 and one for 5.3. But just one of them would be a good starting point, too.)

推荐答案

在这里找不到完整稳定的解析器后,我决定自己编写一个.结果如下:

After no complete and stable parser was found here I decided to write one myself. Here is the result:

PHP-Parser:用 PHP 编写的 PHP 解析器

PHP-Parser: A PHP parser written in PHP

该项目支持解析为 PHP 5.2 和 PHP 8.0 之间的任何 PHP 版本编写的代码.

The project supports parsing code written for any PHP version between PHP 5.2 and PHP 8.0.

除了解析器本身之外,该库还提供了一些相关组件:

Apart from the parser itself the library provides some related components:

  • 将 AST 编译回 PHP(漂亮的打印")
  • 用于遍历和更改 AST 的基础设施
  • 与 XML 之间的序列化(以及以人类可读的形式转储)
  • 命名空间名称(别名等)的解析

有关用法概述,请参阅"基本的用法文档的组件"部分.

For an usage overview see the "Usage of basic components" section of the documentation.

这篇关于任何用 PHP 编写的体面的 PHP 解析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆