Language parser library written in PHP

2019-02-07 03:14发布

问题:

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.

回答1:

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.



回答2:

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/



回答3:

If you need a simple customizable tokenizer, there is nette\tokenizer.

Do you know PHPStan? Its php doc parser is based on this package.

  • https://github.com/phpstan/phpdoc-parser