Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any decent PHP parser written in PHP? [closed]

Tags:

php

parsing

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).

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.

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.)

like image 725
NikiC Avatar asked Apr 07 '11 19:04

NikiC


People also ask

What is a parser in PHP?

PHP Parser is a library that takes a source code written in PHP, passes it through a lexical analyzer, and creates its respective syntax tree. This is very useful for static code analysis, where we want to check our own code not only for syntactic errors but also for satisfying certain quality criteria.

What is the importance of parser in PHP?

Tree-based parsers holds the entire document in Memory and transforms the XML document into a Tree structure. It analyzes the whole document, and provides access to the Tree elements (DOM).


1 Answers

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

PHP-Parser: A PHP parser written in 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:

  • Compilation of the AST back to PHP ("pretty printing")
  • Infrastructure for traversing and changing the AST
  • Serialization to and from XML (as well as dumping in a human readable form)
  • Resolution of namespaced names (aliases etc.)

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

like image 179
NikiC Avatar answered Sep 27 '22 16:09

NikiC