Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good parser generator for php?

I need to parse a small 'mini language' which users can type on my site. I was wondering what the counterparts of lex and jacc or antlr are for the world of php.

like image 861
Toad Avatar asked Sep 15 '10 17:09

Toad


People also ask

What is the best parser generator?

Java Compiler Compiler (JavaCC) is the most popular parser generator for use with Java applications. A parser generator is a tool that reads a grammar specification and converts it to a Java program that can recognize matches to the grammar.

Should you use a parser generator?

A parser generator is a good tool that you should make part of your toolbox. A parser generator takes a grammar as input and automatically generates source code that can parse streams of characters using the grammar.

What is a parser in PHP?

PHP Parser is a library that takes a source code written in PHP, passes it through a lexical analyzer, and creates its respective syntax tree. This is very useful for static code analysis, where we want to check our own code not only for syntactic errors but also for satisfying certain quality criteria.


2 Answers

I used LIME Parser generator for PHP a couple of years ago, and it was already mature and stable.

The parser generator itself is written in PHP, which doesn't really matter in any technical sense - as we require only that the generated parser be in PHP - but I like this detail nonetheless. It makes me feel less apologetic about writing software in PHP ;-)

EDIT:

I should add:

  1. Where I wrote "used" it would be more accurate to say that I "played with". I haven't written any production code using lime, yet. But I see no reason not to do so.

  2. The "calculator example" provided with lime uses a tokenize() method which is very far from a real substitute for the power of lex. But if you need a real tokenizer it ought to be possible to use lex on the "front end" to feed tokens to lime on the "back end".

like image 188
Peter Avatar answered Oct 17 '22 01:10

Peter


http://pear.php.net/package/PHP_ParserGenerator

http://wezfurlong.org/blog/2006/nov/parser-and-lexer-generators-for-php

like image 28
ts. Avatar answered Oct 17 '22 02:10

ts.