views:

152

answers:

1

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!

+3  A: 

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
+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
"...I prefer PHP for its simplicity ..." you mean PEG?
Ira Baxter
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 <a href="semanticdesigns.com/Products/FrontEnds/PHPFrontEnd.html">PHP Front End</a>
Ira Baxter
+1 Thanks. So u know all my wishes :))
Viet