用PHP编写的语言解析器库 [英] Language parser library written in PHP

查看:75
本文介绍了用PHP编写的语言解析器库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找用PHP编写的语言解析器.

目标是阅读自定义语言,而不是阅读PHP代码.

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

基本上,我想指定一种语言语法,给出一个代码片段,并获取一个表示它的结构.然后,我可以遍历该结构以执行代码片段.我相信该结构将是 AST ,但是我不知道这是否是唯一的选择(我对解析器及其词汇并不熟悉).

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).

我查看了 Doctrine DQL解析器,但它似乎不是通用语言解析器.

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

推荐答案

这不是完整的列表,如果您要查找PHP运行时词法分析器/解析器,则一个特殊的项目是

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

您可以在他写的 PHP-Parser 中找到一个用例.这是PHP语言的解析器,带有部分语法文件生成的抽象语法树(AST).

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.

根据我多年来的研究,我从未设法做到如此远,PHP用户空间中的此类项目并不多,而NikiC的这两个库确实是一个很好的例子.

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.

如果您正在寻找一个遵循更多flex规则的词法分析器,我已经在 XDOM 中编写了一个词法分析CSS选择器语法,它也与解析器一起使用,但是该解析器不是基于语法文件,即使它存在于CSS规范中.该词法分析器基于.lex文件.

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.

这篇关于用PHP编写的语言解析器库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆