Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lex and Yacc in PHP [closed]

Is there an implementation of Lex and Yacc in PHP?

If not, can anyone suggest a lexical analyser and parser generator (ie, anything like Lex and Yacc) that will create PHP code. I'm not too worried about the performance of the resulting parser.

I am sick of using regex to parse things that really shouldn't be parsed with regex...

like image 631
Rik Heywood Avatar asked Jan 19 '10 11:01

Rik Heywood


People also ask

Is Lex and Yacc still used?

Yes, these tools are worth learning if you ever need to create or modify code that parses a grammar. For many years the de facto tool for generating code to parse a grammar was yacc, or its GNU cousin, bison.

Is Yacc open source?

The IEEE POSIX P1003. 2 standard defines the functionality and requirements for both Lex and Yacc. Some versions of AT&T Yacc have become open source. For example, source code is available with the standard distributions of Plan 9.

How does Lex and YACC work together?

lex and yacc often work well together for developing compilers. As noted, a program uses the lex-generated scanner by repeatedly calling the function yylex() . This name is convenient because a yacc-generated parser calls its lexical analyzer with this name.


3 Answers

There's JLexPHP: https://github.com/wez/JLexPHP/blob/master/jlex.php

I've not used it, but there's this: http://pear.php.net/package/PHP_ParserGenerator , which creates a PHP Parser from a Lemon grammar. The project seems to be inactive though.

I also found this project: http://code.google.com/p/antlrphpruntime/ , which uses Antlr. Again inactive though.

like image 96
John Carter Avatar answered Oct 23 '22 02:10

John Carter


Been for looking for this kind of thing for a while. After finding this post, I've tried the ANTLR PHP runtime. I can report that it's far from being finished. There are several errors in the generated code, where the original java runtime classes has not been properly translated to PHP (nested class declarations, using '.' instead of '.' when trying to access class methods operator).

The ANTLR framework itself is quite powerful (can't attest to the efficiency of the generated code). Especially the graphical tool ANTLRWorks makes it easy to create and debug grammas. Just too bad about the PHP version. It's possible to roll your own though. The best solution may be to analyse the generated ANTLR runtime class, figure out how it's works, and come up with a light weight less enterprisey version thereof.

like image 37
jens_profile Avatar answered Oct 23 '22 02:10

jens_profile


Cheap trick: code a recursive descent parser. This will cover a lot of cases. See Is there an alternative for flex/bison that is usable on 8-bit embedded systems?

like image 25
Ira Baxter Avatar answered Oct 23 '22 02:10

Ira Baxter