Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Library like pyparsing for PHP

Tags:

php

parsing

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.

like image 424
Donovan Avatar asked Sep 15 '11 13:09

Donovan


1 Answers

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

like image 196
NikiC Avatar answered Sep 21 '22 19:09

NikiC