3

The question is very straightforward: Is it possible to parse PHP using PEG? I want to use a PEG parser-generator to parse PHP. Please kindly advise. Thank you!

Viet
  • 17,944
  • 33
  • 103
  • 135
  • 1
    I'm not very familiar with PEGs, but I would guess that it is possible. Why don't you give it a try (possibly starting with a simplified version of the PHP grammar to see if it will work at all). – bcat Dec 09 '09 at 00:13

1 Answers1

4

You can make most parser technologies parse most languages, with sufficient effort.

Whether PEG will parse PHP without a lot of effort is a different question.

AFAIK, PHP itself uses Bison (LALR) so I assume PEG will likely handle the grammar supplied by the PHP distribution or something similar. And if you're going to use the PHP distribution, why not just use their supplied parser?

If you don't use the PHP distribution, your problems with parsing PHP are likely to be hard because you'll have to guess the language syntax from the online reference manual, (I've been there, and done that) which is frankly one of the worst ways to define a langauge I've ever seen: all it is is a bunch of examples.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • +1 Hi Ira. Thanks very much for your reasoning. Yes, you are right, using the parser distributed by PHP is the best option. However, I prefer PHP for its simplicity. I will try first and see how far I can go. Thanks. – Viet Dec 09 '09 at 06:25
  • 1
    "...I prefer PHP for its simplicity ..." you mean PEG? – Ira Baxter Dec 09 '09 at 06:48
  • 1
    Are you still trying to convert PHP to C++ (stackoverflow.com/questions/1090124/…)? If so, you might consider using a tool that already has a full PHP parser, and can apply source-to-source pattern rules to carry out such a translation. See PHP Front End – Ira Baxter Dec 09 '09 at 06:55
  • Have you seen PHP-Parser? – CMCDragonkai Nov 05 '14 at 12:57
  • @CMCDragonkai: No link, so I don't precisely what you are referring to. I know of github.com/nikic/PHP-Parser based on this answer: http://stackoverflow.com/a/5834775/120163, but I have no idea how complete it really is. – Ira Baxter Nov 05 '14 at 16:02