PHP Lexer and Parser Generator? [closed]
Asked Answered
F

4

12

I know question Lex and Yacc in PHP was asked before but 1 year ago.

Is there any new mature PHP parser generator now? My searches drove me to the following ones, what do you think about them, any others?

  • code.google.com/p/antlrphpruntime/ : The ANTLR PHP version but it seems to be very beta version and I think there is a lot of work to do. The advantage is that I can write the grammar in the ANTLR Works tool.
  • pear.php.net/package/PHP_ParserGenerator/docs/0.1.7/ : I tried but it seems very complicated, to be used with the PHP_LexerGenerator.
  • sourceforge.net/projects/lime-php/ : I didn't try
  • bitbucket.org/wez/lemon-php/downloads : I didn't try
  • bitbucket.org/wez/jlexphp/downloads : I didn't try

[Answer :] Somebody gave me this wonderful link to an old question : Is there an alternative for flex/bison that is usable on 8-bit embedded systems?

This posts allowed me to understand most of the ANTLR code generated. So my choice keeps being the same : ANTLR although the project seems dead. I hope I wont have to alter the code as I would like to keep it maintainable.

I will wait a bit to see if there is another answer otherwise I'll accept your advise to keep using ANTLR

Forth answered 3/11, 2011 at 22:18 Comment(7)
If you have the attributed grammar for PHP, try CoCo/R (ssw.jku.at/coco/). I'm using it right now for a project at work and it works rather well.Tope
I wouldn't use code.google.com/p/antlrphpruntime: it's (AFAIK) not actively being developed, and it only supports the bare minimum of what most of the other ANTLR-targets support. That's why it isn't included by default in the ANTLR tool. The same goes for PHP_ParserGenerator: I wouldn't go for it (not much documentation, and it's an abandoned project). The other 3 tools are unfamiliar to me as well: I suggest you give them a try.Mg
@xbonez : I'm not 100% sure but I dont think you understood my needs. I need to generate a parser written in PHP to parse a specific grammar (not to parse the php language). CoCo/R looks nice but I can't find a php generator.Forth
@BartKiers : I will give a chance to ANTLR because of the grammar writing tool. I now understand the generated code, there is some gaps like unsupported UTF-8 streams but I can manage a Latin 1 conversion before calling the lexer.Forth
@NicolasThery, it's not just a few missing IO-streams. If memory serves me well, there's no predicates whatsoever, no AST rewriting stuff, no StringTemplate options: compared to properly supported targets, you'd be seriously lacking the larger part of ANTLR's powers! But, hey, if it's sufficient for you, of course, you should go ahead and give it a try! It just looks like you're asking a question you already had your own answer to.Mg
@NicolasThery: you're right, I did get you wrong. Coco/R can generate a parser FOR any language (provided you have the grammar file) and IN a lot of languages (C#, C++, python, F# etc.). Unfortunately, PHP is not one of them.Tope
For those of you coming in late to this question the updated address for ANTLR: antlr.org. And there are a bunch of different updated grammars github.com/antlr/grammars-v4Verdieverdigris
C
6

I'd propose to give ANTLR a try. ANTLRWorks might be helpful.

I wrote an LL(1) parser generator myself in pure PHP, since I wasn't aware of other PHP-based solutions.

Costumer answered 3/11, 2011 at 23:30 Comment(3)
I'm trying the ANTLR at the moment but there is a lots of errors in the generated code. For exemple, some exceptions are missing and the parser rises unexpected exceptions. Did you use that component ? code.google.com/p/antlrphpruntimeForth
Hm, actually not. I tried the ActionScript runtime recently - which is currently broken. But I remember, that I checked played around with ANTLR and the PHP runtime. At that time, things seemed to work.Costumer
The link above is dead. It can now be found here: antlr3.org/worksVerdieverdigris
R
6

I've ported Jison, a Bison clone in javascript, to php. The results are a killer parser, able to handle very simple and very complex lexing/parsing. It is now part of Jison, but there are a few updates in my fork.

The files are here. See the readme in that page, you create a javascript and php parser at the same time that are capable of doing the same or different things.

It is stable, fast, easy and fun to use.

Recension answered 24/5, 2012 at 12:23 Comment(4)
I Will Check That as soon as possible, it reads great.Forth
dead link on ported PHP :/Lituus
Can you update with a link to your PHP parser? ThanksAppellee
Now it's at github.com/zaach/jison/tree/master/ports/phpCuticle
S
2

There is PEG for php https://github.com/hafriedlander/php-peg

Spieler answered 18/1, 2012 at 23:55 Comment(3)
Thank you. Although I'm now quite happy with the ANTLR generated Parser, I'm still interested with others because antlr looks very slow. Did you try that PEG, does it have good performances.Forth
By the way, I think this project PEG is nearly DEAD.... Last commit 03/2011, 10 bugs declared in August 2011.Forth
I don't know about performance, but I like PEG and this is, I think, the only PEG implementation in php.Spieler
C
0

There's latest grammar file for PHP 7.4: https://github.com/php/php-src/blob/af12aa8124232b8fb6e96736e1232f6e0d2554e8/Zend/zend_language_parser.y

PHP using re2c parser generator, but as I know its syntax compatible with ANTLR.

Cuticle answered 23/5, 2019 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.