Any Decent PHP Parser Written in PHP

Any decent PHP parser written in PHP?

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

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)
  • Converting an AST into JSON and back.
  • Resolution of namespaced names (aliases etc.)

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

Language parser library written in PHP

This is not a complete list, if you're looking for PHP runtime lexer/parsers, one exceptional project is Phlexy by NikiC.

You can find a use-case inside PHP-Parser as well written by him. That is a parser for the PHP language with an abstract syntax tree (AST), partially generated from a grammar file.

I never managed it to get that far yet, from my own research over the years, there are not many such projects in PHP userspace, and these two libraries from NikiC are really a very good example.

If you're looking for a lexer that follows more the flex rules, I have written one in XDOM that lexes CSS selector syntax, it's also with a parser but the parser is not based on a grammar file even though it exists in the CSS specs. The lexer is based on a .lex file.

how to create a language in php

Basically, "making a language" involves several steps. First, you need a "lexer" which splits your input into substrings belonging to different symbol classes (like "identifier", "number", "operator" etc). Second, you write down a grammar of your language, usually using some kind of BNF. Then you eat the banana use a program called "parser generator" which turns your grammar into actual parser code and finally you combine lexer and parser to get an actual complier.

Normally, this kind of things is being done with C or Java, I've never heard of working compliers written in php. Still, you can use php tokenizer for the first part (the lexer) - assuming your language has syntax similar to php - and try http://pear.php.net/package/PHP_ParserGenerator to generate the parser.

Sorry if this sounds a bit complicated, but so it is.

PHP parsing technique

PHP uses bison/yacc to generate a parser. Have a look at the grammar here: http://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_language_parser.y?view=markup

Generate AST of a PHP source file

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.

Can anyone suggest a good PHP parsing class?

Leave it to PHP to have a native function to do just that: http://php.net/token_get_all

Use php to scan a .php file for all of its methods and classes

http://www.php.net/manual/en/function.show-source.php may help you within php.


Not the same thing but;

also see http://xdebug.org/

and maybe http://pecl.php.net/package/xhprof

They might help you too.

Edit: By the way, phpDoc also shows the code.



Related Topics



Leave a reply



Submit