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

Community
  • 1
  • 1
Nicolas Thery
  • 2,319
  • 4
  • 26
  • 36
  • 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. – Ayush Nov 04 '11 at 03:12
  • 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. – Bart Kiers Nov 04 '11 at 08:05
  • @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. – Nicolas Thery Nov 04 '11 at 12:36
  • @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. – Nicolas Thery Nov 04 '11 at 12:39
  • 1
    @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. – Bart Kiers Nov 04 '11 at 12:44
  • @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. – Ayush Nov 04 '11 at 14:14
  • For those of you coming in late to this question the updated address for ANTLR: http://antlr.org/. And there are a bunch of different updated grammars https://github.com/antlr/grammars-v4 – Max Worg Jun 12 '16 at 03:11

4 Answers4

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
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.

Dennis
  • 7,907
  • 11
  • 65
  • 115
SteAp
  • 11,853
  • 10
  • 53
  • 88
  • 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 ? http://code.google.com/p/antlrphpruntime/ – Nicolas Thery Nov 03 '11 at 23:45
  • 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. – SteAp Nov 03 '11 at 23:47
  • The link above is dead. It can now be found here: http://www.antlr3.org/works/ – Max Worg Jun 13 '16 at 01:43
2

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

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • 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. – Nicolas Thery Jan 20 '12 at 00:04
  • By the way, I think this project PEG is nearly DEAD.... Last commit 03/2011, 10 bugs declared in August 2011. – Nicolas Thery Jan 20 '12 at 00:05
  • I don't know about performance, but I like PEG and this is, I think, the only PEG implementation in php. – jcubic Jan 21 '12 at 00:37
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.

Acuna
  • 1,741
  • 17
  • 20