2

Preferably I'd like a solution which allows me to parse PHP from PHP, but any solution is welcome. (As an example of what I'm looking for, Ruby has the - amongst others - ruby_parser gem.)


Edit:

I am looking to generate abstract syntax trees of some kind, from PHP code. (And unless I am mistaken, I am fully aware of the existence and behaviour of the eval function.)

What are the options (if any) for parsing PHP code to some sort of abstract syntax tree? And what are the options for turning these syntax trees back into (readable) PHP code?

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
wen
  • 3,782
  • 9
  • 34
  • 54
  • Just wondering what was/is your goal with the parser? – Tomas Votruba May 04 '19 at 05:55
  • @tomáš-votruba: refactoring a large codebase of PHP – wen May 05 '19 at 09:57
  • I see. Are you still in PHP? Do you have any output post/package/tips on that? – Tomas Votruba May 05 '19 at 10:06
  • Write a good amount of tests to run before/after, and ideally list and review the changes you make. PHP is a wild language, so small systematic changes may huge unintended consequences. – wen May 08 '19 at 09:43
  • 1
    Great! Then I'm going in the right direction with Rector - https://github.com/rectorphp/rector. For each rule I have 30 % more code in tests (1 MB of refactoring code, 1,3 MB tests that cover it). – Tomas Votruba May 08 '19 at 11:30

3 Answers3

2

A tokenizer is not the same as a parser. The tokenizer just produces tokens and not in a tree format. For those who are actually looking to produce an AST for PHP similar to what ruby_parser does for Ruby, use the PHP-Parser project (https://github.com/nikic/PHP-Parser).

The PHP-Parser project also comes with a pretty printer that turns your AST back to PHP.

Douglas
  • 36
  • 1
  • I believe this is what I actually ended up using all those years ago, so yeah, I'll accept your answer since PHP-Parser is indeed a great project. :) – wen Sep 26 '14 at 10:54
2

Edit: this answer was written before @pepjin edited their question and changed the requirements. See comments for context.


eval() to execute PHP code from within PHP.

To analyse PHP code for your own micro language etc you can use the PHP Tokenizer. List of parser tokens: http://www.php.net/manual/en/tokens.php

Community
  • 1
  • 1
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • Thanks, the PHP Tokenizer was quite possibly what I was looking for. However, is there a method of transforming the token-arrays back into PHP code? – wen Feb 21 '11 at 15:57
  • I am not sure. I have never had to do that. Seems to be outside the scope of this question. Perhaps you might consider asking another question specifically asking if you can convert tokens back into executable code. – Treffynnon Feb 21 '11 at 16:01
  • I've edited my question to include the conversions of tokens back into executable code, as the reverse process is rather tied to the solution for parsing (as the format of the syntax trees isn't necessarily shared). Maybe I'll start a question specifically for PHP Tokenizer. Thanks, anyway. :) – wen Feb 21 '11 at 16:04
  • I've accepted this answer, for now, as it was indeed a perfect answer to my original question. – wen Feb 21 '11 at 16:06
  • @pepijn: How is a tokeninzer "a perfect answer" to your request to build an AST? – Ira Baxter May 01 '14 at 02:45
  • @IraBaxter read the question edit history and ye shall be enlightened: http://stackoverflow.com/revisions/5068007/1 – Treffynnon May 02 '14 at 14:40
1

You are looking for the evil of eval() I believe.

http://php.net/manual/es/function.eval.php

Tom Gruner
  • 9,635
  • 1
  • 20
  • 26
  • I am looking for a way to generate abstract syntax trees from PHP code. The `eval` function simply allows my to evaluate PHP code - and nothing else. I'll edit my question to reflect this. – wen Feb 21 '11 at 15:53
  • eval definately does have its uses. The comment was a bit tongue in cheek and I hope you don't take offense. – Tom Gruner Feb 21 '11 at 15:57