关于如何制作可配置解析器的建议

时间:2022-10-29 13:47:19

I want to build a parser for a C like language. The interesting aspect about it is that I want to build it in such a way that someone who has access to the source can easily modified it to extend the language (a new expression type of instance) with the extensions being runtime configurable (they can be turned on and off).

我想为C语言构建一个解析器。关于它的有趣方面是我希望以这样的方式构建它:有权访问源的人可以轻松地修改它以扩展语言(实例的新表达式类型),其扩展是运行时可配置的(它们可以是打开和关闭)。

My current intent is to build a recursive decent parser as an object. Each production will be a method of an object. The method of extension will be to derive classes from this base replacing methods (and production definitions) as needed. I'm still trying to figure out how to mix and match extensions. One idea is to play games with the v-tbl. Objects would be constructed with a v-tbl that is a copy of the base but with methods replaced from derived classes.

我目前的意图是构建一个递归的正确解析器作为对象。每个生产都是一个对象的方法。扩展方法是根据需要从该基础派生类替换方法(和生产定义)。我还在试图弄清楚如何混合和匹配扩展。一个想法是用v-tbl玩游戏。对象将使用v-tbl构造,v-tbl是基础的副本,但是从派生类替换了方法。

Aside from the bit-twiddling nature of the solution the only issues I have with it is

除了解决方案有点笨拙的性质之外,我唯一的问题是它

  • a reasonable way to do the v-tbl mixup
  • 一个合理的方法来进行v-tbl混音

  • what to do when 2 extensions alter the same productions (as most replacements will end up calling the original having one replacement call the other would work but the mechanics of setting this up are the issue)
  • 当2个扩展改变相同的制作时该怎么办(因为大多数替换最终会调用原来有一个替换调用,另一个会工作但是设置这个的机制是问题)

  • how to allow the extension of extensions (this might end up looking like a standard MI system, but I've never got how they work)
  • 如何允许扩展扩展(这可能最终看起来像一个标准的MI系统,但我从来没有得到它们如何工作)

Another solution (a slightly more mundane version of the same same approach) would be to use static member variables to store function-pointers and call them for the same effect.

另一个解决方案(相同方法的稍微平凡的版本)将使用静态成员变量来存储函数指针并调用它们以获得相同的效果。

Edit: I have already built a system that lets me build productions from BNF definitions. I can alter it to support whatever I decide on.

编辑:我已经构建了一个系统,可以让我从BNF定义构建产品。我可以改变它以支持我决定的任何事情。

5 个解决方案

#1


1  

These are some of the challenges the Perl 6 design effort has faced. You may find it worthwhile looking into some of the solutions they came up with. Or you may find that to be gross overkill.

这些是Perl 6设计工作面临的一些挑战。您可能会发现有必要研究一下他们提出的一些解决方案。或者你可能会发现这是一种严重的过度杀伤力。

#2


1  

I made a configurable parser I uploadei it some time ago at http://code.google.com/p/compparser/ The project there is not up-to-date but is working fine.

我做了一个可配置的解析器我上次在http://code.google.com/p/compparser/ uploadei该项目没有最新但工作正常。

#3


0  

If I recall my university courses correctly, recursive descent parsers have some limitations that might bite you, especially since you're allowing extensions - somebody elses language extension could cause issues.

如果我正确地回忆起我的大学课程,递归下降解析器有一些限制可能会让你感到困惑,特别是因为你允许扩展 - 有些人可能会导致语言扩展问题。

A proper compiler toolkit - such as the open source ANTLR - might make things easier, and might also provide some different approaches for you.

一个合适的编译工具包 - 例如开源ANTLR - 可能会使事情变得更容易,也可能为您提供一些不同的方法。

#4


0  

another option is to express the parsing rules in XML or something, instead of in code; less efficient, but far more dynamically configurable; each language or variant can just use its own (XML) file, and even include/reference other files as 'base' files...

另一种选择是用XML或其他东西来表达解析规则,而不是代码;效率较低,但动态可配置性更强;每种语言或变体都可以使用自己的(XML)文件,甚至包含/引用其他文件作为“基础”文件......

#5


0  

Frankly, I am not even sure I understood everything you wrote... :-)

坦率地说,我甚至不确定我理解你写的一切...... :-)

But when I see parser and flexibility, I think about LPeg - Parsing Expression Grammars For Lua. It might not fit your needs but it is well worth a look... ;-)

但是当我看到解析器和灵活性时,我会想到LPeg - 为Lua解析表达式语法。它可能不适合您的需求,但值得一看...... ;-)

#1


1  

These are some of the challenges the Perl 6 design effort has faced. You may find it worthwhile looking into some of the solutions they came up with. Or you may find that to be gross overkill.

这些是Perl 6设计工作面临的一些挑战。您可能会发现有必要研究一下他们提出的一些解决方案。或者你可能会发现这是一种严重的过度杀伤力。

#2


1  

I made a configurable parser I uploadei it some time ago at http://code.google.com/p/compparser/ The project there is not up-to-date but is working fine.

我做了一个可配置的解析器我上次在http://code.google.com/p/compparser/ uploadei该项目没有最新但工作正常。

#3


0  

If I recall my university courses correctly, recursive descent parsers have some limitations that might bite you, especially since you're allowing extensions - somebody elses language extension could cause issues.

如果我正确地回忆起我的大学课程,递归下降解析器有一些限制可能会让你感到困惑,特别是因为你允许扩展 - 有些人可能会导致语言扩展问题。

A proper compiler toolkit - such as the open source ANTLR - might make things easier, and might also provide some different approaches for you.

一个合适的编译工具包 - 例如开源ANTLR - 可能会使事情变得更容易,也可能为您提供一些不同的方法。

#4


0  

another option is to express the parsing rules in XML or something, instead of in code; less efficient, but far more dynamically configurable; each language or variant can just use its own (XML) file, and even include/reference other files as 'base' files...

另一种选择是用XML或其他东西来表达解析规则,而不是代码;效率较低,但动态可配置性更强;每种语言或变体都可以使用自己的(XML)文件,甚至包含/引用其他文件作为“基础”文件......

#5


0  

Frankly, I am not even sure I understood everything you wrote... :-)

坦率地说,我甚至不确定我理解你写的一切...... :-)

But when I see parser and flexibility, I think about LPeg - Parsing Expression Grammars For Lua. It might not fit your needs but it is well worth a look... ;-)

但是当我看到解析器和灵活性时,我会想到LPeg - 为Lua解析表达式语法。它可能不适合您的需求,但值得一看...... ;-)