質問

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.

役に立ちましたか?

解決 2

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.

他のヒント

Supposedly php has a library to do a lexer/parser: http://php.net/manual/en/parle.examples.php For Windows, looks like you can grab a pre-compiled binary. http://php.net/manual/en/parle.installation.php says versions available here: http://windows.php.net/downloads/pecl/releases/parle/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top