解析和打印PHP代码 [英] Parsing and Printing PHP Code

查看:79
本文介绍了解析和打印PHP代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

优选地,我想要一个允许我从PHP解析PHP 的解决方案,但是欢迎任何解决方案. (作为我正在寻找的示例,Ruby具有-ruby_parser gem.-

Preferably I'd like a solution which allows me to parse PHP from PHP, but any solution is welcome. (As an example of what I'm looking for, Ruby has the - amongst others - ruby_parser gem.)

我正在寻找从PHP代码生成某种抽象语法树的方法. (并且除非我弄错了,否则我完全了解eval函数的存在和行为.)

I am looking to generate abstract syntax trees of some kind, from PHP code. (And unless I am mistaken, I am fully aware of the existence and behaviour of the eval function.)

将PHP代码解析为某种抽象语法树的选项(如果有)是什么?将这些语法树重新转换为(可读的)PHP代码有哪些选择?

What are the options (if any) for parsing PHP code to some sort of abstract syntax tree? And what are the options for turning these syntax trees back into (readable) PHP code?

推荐答案

令牌生成器与解析器不同.令牌生成器仅生成令牌,而不以树格式生成令牌.对于实际上希望为PHP生成AST的用户,类似于ruby_parser对Ruby所做的操作,请使用PHP-Parser项目(

A tokenizer is not the same as a parser. The tokenizer just produces tokens and not in a tree format. For those who are actually looking to produce an AST for PHP similar to what ruby_parser does for Ruby, use the PHP-Parser project (https://github.com/nikic/PHP-Parser).

PHP-Parser项目还带有一个漂亮的打印机,可以将AST转换回PHP.

The PHP-Parser project also comes with a pretty printer that turns your AST back to PHP.

这篇关于解析和打印PHP代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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