Generate AST of a PHP source file

强颜欢笑 提交于 2019-11-29 16:48:27

问题


I want to parse a PHP source file, into an AST (preferably as a nested array of instructions).

I basically want to convert things like

f($a, $b + 1)

into something like

array( 'function_call',
    array(
        array( 'var', '$a' ),
        array( 'expression',
            array(
                array( 'binary_operation',
                    '+',
                    array ('var', '$b'),
                    array( 'int', '1' )
                )
            )
        )
    )
)

Are there any inbuilt PHP library or third party libraries (preferably in PHP) that would let me do this?


回答1:


I have implemented a PHP Parser after I figured out that there was no existing parser. It parses the PHP code into a node tree.




回答2:


HipHop

You can use Facebook's HHVM to dump the AST.

apt-get install hhvm

# parse specified file and dump the AST
hhvm --parse arg  

This worked for HipHop (the old PHP to C++ compiler) - back in the days of 2013!


HHVM

Update 2015

--parse is not supported.

You will get an error: HHVM The 'parse' command line option is not supported.

See https://github.com/facebook/hhvm/blob/c494c3a145008f65d349611eb2d09d0c33f1ab23/hphp/runtime/base/program_functions.cpp#L1111

Feature Request to support the CLI option again: https://github.com/facebook/hhvm/issues/4615


PHP 7

PHP 7 will have an AST, see the related RFC.

There are two extensions, which provide access and expose the AST generated by PHP 7:

  • https://github.com/nikic/php-ast
  • https://github.com/sgolemon/astkit



回答3:


Pfff is an OCaml library for parsing and manipulating PHP code. See the manual of Pfff for more details.




回答4:


No, there is no such feature built-in. But you can use the Tokenizer to create it.




回答5:


Well, you can look at the answers from Parsing and Printing PHP Code and Generating PHP code (from Parser Tokens): basically PEAR's PHP_Beautifier package at http://pear.php.net/package/PHP_Beautifier can be extended to do what you want, but it sounds like it requires some heavy lifting.

And if you're not constrained to PHP then http://www.eclipse.org/pdt/articles/ast/PHP_AST.html walks you through using the Eclipse PHP module's AST parser.



来源:https://stackoverflow.com/questions/6153634/generate-ast-of-a-php-source-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!