6

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.

user187291
  • 53,363
  • 19
  • 95
  • 127

3 Answers3

2

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.

Matthias
  • 3,458
  • 4
  • 27
  • 46
user187291
  • 53,363
  • 19
  • 95
  • 127
1

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.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
0

phpParseTree

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

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • This appears to produce only a stream of tokens wrapped in XML tags. It does not look like a parse tree in spite of what the target web site says. – Ira Baxter Aug 31 '10 at 13:03