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?

5
  • Just wondering what was/is your goal with the parser? Commented May 4, 2019 at 5:55
  • @tomáš-votruba: refactoring a large codebase of PHP Commented May 5, 2019 at 9:57
  • I see. Are you still in PHP? Do you have any output post/package/tips on that? Commented May 5, 2019 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. Commented May 8, 2019 at 9:43
  • 1
    Great! Then I'm going in the right direction with Rector - 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). Commented May 8, 2019 at 11:30

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

6 Comments

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?
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.
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. :)
I've accepted this answer, for now, as it was indeed a perfect answer to my original question.
@pepijn: How is a tokeninzer "a perfect answer" to your request to build an AST?
|
1

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

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

2 Comments

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.
eval definately does have its uses. The comment was a bit tongue in cheek and I hope you don't take offense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.