Language parser library written in PHP

不打扰是莪最后的温柔 提交于 2019-11-29 09:25:31

问题


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/



来源:https://stackoverflow.com/questions/16387277/language-parser-library-written-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!