7

I would like to know: is there something like pyparsing (a recursive descent parser) for PHP?
I already looked for it, but it seems no one did it yet. I hope I am wrong.

Thank you in advance.

Donovan
  • 6,002
  • 5
  • 41
  • 55
  • Custom nested expressions with different enclosing characters. Not a simple thing with Regex :-( – Donovan Sep 17 '11 at 22:07
  • @stereofrog: that project seems to be dead, but I'll have a look. Thank you. – Donovan Sep 17 '11 at 22:08
  • @Alberteddu: Could you give an example / a formal grammar? Depending on what exactly it is, it might be better to use a recursive regex (you can do recursion in regex and it's easy) or a hand written regex lexer and parser or a parser generator. Depends on the complexity of the grammar. – NikiC Sep 18 '11 at 09:09

3 Answers3

6

I don't know any maintained parser generators written in PHP. But there are parser generators written in other languages with PHP as a target language. One I have personally used is kmyacc. There is a PHP and Windows compatible fork of it. The grammar for it is written in yacc format and can be compiled to PHP using this command:

kmyacc -l -m %PARSER_PROTOTYPE_FILE% -p %NAME% %GRAMMAR_FILE%

Kmyacc already comes with a procedural parser prototype file for PHP, but I personally use a modified version of an OOP based prototype.

As an example: This grammar get's compiled into this parser. (Note that the grammar is huge, that's why the generated parser has two and a half thousand lines. A "normal" grammar would obviously be far smaller.)

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • Thi is very interesting. I'm going to take a look. Thanks a lot (obviously if that's what I need I'll check as answered). – Donovan Sep 18 '11 at 10:46
2

If all you need to parse are "custom expressions", you can probably code a recursive descent parser by hand fairly easily, if you have already written down your grammar.

See this SO answer for details: Is there an alternative for flex/bison that is usable on 8-bit embedded systems?

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
0

You can try this:

http://pyparsing.wikispaces.com/message/view/home/41772107

dbers
  • 636
  • 5
  • 16