如何在基于抽象语法树的解释器中表示类
我已阅读相关问题,但似乎没有一个问题能直接解决该问题。 我正在编写一个 PHP 脚本解释器。 我让 AST 为除类之外的所有内容生成正确的节点。 处理类与处理函数有点不同,因此我正在寻找如何处理独立的类以及扩展其他类的类。
我看过 ANTLR,但我负担不起开销,因为这是针对嵌入式平台的。 我正在寻找的是 AST 中类背后的概念理论,以便它们可以由解释器的执行器部分执行。 非常感谢包含此问题具体答案的良好链接。
I have read the related questions, but none of them appears to address the question directly. I am working on writing a PHP script interpreter. I have the AST generating proper nodes for everything except classes. Handling classes is a bit different than handling functions, so I am looking for how to handle classes that are standalone, and that extend other classes.
I have looked at ANTLR, but I can't afford the overhead as this is for an embedded platform. What I am looking for is the theory conceptually behind classes in ASTs so they can be executed by the executor portion of the interpreter. Good links with specific answers to this issue are definitely appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ANTLR 或多或少与您的问题无关。
PHP 中的类基本上是从字符串到属性的映射。 每个属性可以是公共的、私有的、受保护的。 每个属性还保存一个值,该值可以是静态变量或方法。 方法是(在 PHP 中)采用隐式 $this 参数的函数。 因此,您可以将类基本上视为 PHP 中的一个奇特的数组对象。
当您创建一个对象时,您给它一个指向 PHP 类对象的指针。 当您调用该对象上的方法时,您可以通过通过该指针获取的类对象查找该方法。
我希望这有帮助。
ANTLR is more or less irrelevant to your problem.
A class in PHP is basically a map from strings to attributes. Each attribute can be public, private, protected. Each attribute also holds a value, which could be a static variable or a method. Methods are functions that (in PHP) take an implicit $this parameter. So you can think of a class as basically a fancy array object in PHP.
When you create an object, you give it a pointer to your PHP class object. When you call a method on that object, you look up the method through the class object which you get through that pointer.
I hope this helps.
也许您需要一些与源语法稍微分离的解释结构? 我对 PHP 了解不多,但听起来您确实在问如何以方便解释的方式构建程序模型。 我认为通过 ANTLR 生成的 AST 与您想要的源有点太接近了。
Perhaps you need some structures for interpretation that are a bit more removed from the source grammar? I don't know much about PHP, but it sound like you're really asking how structure the program model in a way that's convenient for interpretation. I'd regard an AST generated via ANTLR as a bit too close to the source for what you want.
我发现自上而下编译器中的最佳方法是将类视为函数,因为它们只是节点,无需在下降到树中之前评估参数。
区别在于执行器,它必须创建一个执行范围,在其中封装构成类节点的方法和变量。
I have found that the best method in a top down compiler, the best method is to treat the classes generally just like functions, in that they are just nodes without needing to evaluate the arguments before descending into the tree.
The differences are in the executor, which has to create an execution scope in which to encapsulate the methods and variables that comprise the class node.
我建议使用 JavaCC(或 FreeCC 的分支)来解析和构建 AST。 JavaCC 生成一个没有运行时依赖性的解析器。 编写比 JavaCC 生成的代码更小/更快的解析器很困难。
I'd suggest using JavaCC (or the fork FreeCC) to parse and build your AST. JavaCC generates a parser which has no runtime dependencies. It's difficult to write a smaller/faster parser than the code which JavaCC generates.
看一下 phc 抽象语法,它正是这样做的。 (顺便说一句,听起来使用 phc 的前端可能比重新发明轮子更好)。
Take a look at the phc abstract grammar, it does exactly that. (Incidentally, it sounds like using phc's front-end might be better than reinventing the wheel).