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

查看:31
本文介绍了用 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 运行时词法分析器/解析器,一个特殊的项目是 Phlexy 来自 NikiC.

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天全站免登陆