standalone grammar and parser for php

2019-06-20 15:00发布

i'm looking for a ready-made grammar and parser for php (at least 5.2), ideally an utility/library that can parse php code into a readable AST, e.g. xml. The parser itself doesn't have to be written in php, the source language doesn't matter much.

3条回答
贪生不怕死
2楼-- · 2019-06-20 15:22

To answer my own question I've managed to compile phc on my OSX box, the parser part seems to work well

 phc --dump-xml=ast foo.php > bar.xml

creates an xml representation of the AST.

查看更多
欢心
3楼-- · 2019-06-20 15:34

phpParseTree

The Parse_Tree extension generates an XML parse tree from a php code.

查看更多
Rolldiameter
4楼-- · 2019-06-20 15:35

Our DMS Software Reengineering Toolkit is generalized compiler technology used to parse/analyzer/transform arbitrary computer langauges. It parses to ASTs, and has support for building symbol tables, and various types of flow graphs.

It has a PHP Front End that is fully PHP 5.x compliant, automatically builds full ASTs, using DMS as a foundation. It can export XML, but our experience (and the design of DMS) says you get a lot more milage by staying "inside" DMS with the AST data structure, doing your work there, with DMS's huge library of AST manipulation and pattern matching facilities, and then generating your result, rather than trying to handle the huge amounts of XML that you will get.

This front end has been used in a number of production tools.

EDIT: October 2016: Now handles PHP 5.6 and PHP 7.

查看更多
登录 后发表回答