Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Language parser library written in PHP

I am looking for a language parser written in PHP.

The goal is to read a custom language, not read PHP code.

Basically, I want to specify a language syntax, give a code snippet and get back a structure representing it. Then I can traverse that structure to execute the code snippet. I believe the structure will be an AST, but I don't know if this is the only option (I am not intimate with parsers and their vocabulary).

I had a look at the Doctrine DQL parser but it doesn't seem like a generic language parser.

like image 821
Matthieu Napoli Avatar asked May 05 '13 17:05

Matthieu Napoli


1 Answers

This is not a complete list, if you're looking for PHP runtime lexer/parsers, one exceptional project is Phlexy by NikiC.

You can find a use-case inside PHP-Parser as well written by him. That is a parser for the PHP language with an abstract syntax tree (AST), partially generated from a grammar file.

I never managed it to get that far yet, from my own research over the years, there are not many such projects in PHP userspace, and these two libraries from NikiC are really a very good example.

If you're looking for a lexer that follows more the flex rules, I have written one in XDOM that lexes CSS selector syntax, it's also with a parser but the parser is not based on a grammar file even though it exists in the CSS specs. The lexer is based on a .lex file.

like image 191
hakre Avatar answered Nov 02 '22 07:11

hakre