面向对象编程语言的 AST(抽象语法树)是什么样的? [英] What would an AST (abstract syntax tree) for an object-oriented programming language look like?

查看:25
本文介绍了面向对象编程语言的 AST(抽象语法树)是什么样的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关 AST(抽象语法树)的内容,但我看到的所有示例都使用以下表达式:

I'm reading about AST (abstract syntax trees) but all the samples I see use expressions such as:

a + b * c 

可以用类似 lispy 的语法表示为:

Which could be represented in a lispy like syntax as:

(+ a (* b c) )

相当于:

  +
 / 
a   * 
   / 
  b   c

我的问题是 OOPL 中一个类的 AST 是什么样的?

My question is How an AST for a class in a OOPL would look like?

我的天真尝试是针对此 Java 代码:

My naive attempt is for this Java code:

 class Person { 
     String name;
     int    age;
     public String toString() { 
        return "name";
     }
 }

是:

;Hand written
(classDeclaration Person 
     (varDeclaration String name)
     (varDeclaration int    age )
     (funcDeclaration String toString 
           (return "name")
     )
 )

但我不太确定我离真正的 AST 表示有多近或多远.

But I'm not quite sure how close or far am I to a real AST representation.

这取决于我选择的语言.需要多少细节?那些xyzDeclaraction"是需要的还是可以是:

Does it depends on the language I choose. How much detail is needed? Are those "xyzDeclaraction" needed or could be as:

 (Person (String name) (int age))

我在哪里可以看到实际编程语言的真实"表示以了解更多信息.

Where can I see a "real" representation of an actual programming language to learn more.

推荐答案

AST 是 CST (具体语法树,或解析树).具体的语法树是由用于解析文件的产生式(在语法中)产生的树.所以你的 AST 基本上是从你的语法定义派生出来的,但是已经转换了

AST is an abstraction of the CST (concrete syntax tree, or, parse tree). The concrete syntax tree is the tree resulting from the productions (in the grammar) used to parse the file. So your AST is basically derived from your grammar definition, but has for transformed

                        Exp                    
                      /  |                     
                     /   |                          *
                 Ident BinOp Ident       into       / 
                  /      |                       "x" "y"
                 /       |      
               "x"       *      "y"

总而言之,我认为您帖子中的示例看起来不错.我可能会将变量声明包装在 varDeclList 中,将函数声明包装在 methDeclList 中,并将 return 语句包装在 stmtList 中.(见下文.)

All in all I think the example in your post looks fine. I would probably wrap the variable declarations in a varDeclList and the function declaration in a methDeclList, and the return statement in a stmtList. (See below.)

Apple 在他的Java 中的现代编译器实现"一书中描述了 AST 的一种或多或少的真实"表示.(可以在此处找到资源.)

One more or less "real" representation of an AST is described by Apple in his book "Modern Compiler Implementation in Java". (Resources can be found here.)

使用这些类,您的程序将如下表示:

Using those classes, your program would be represented as follows:

Program
    ClassDeclList
        ClassDecl
            Identifier
                id: Person
            VarDeclList
                VarDecl
                    type: String
                    id: name
                VarDecl
                    type: int
                    id: age
            MethDeclList
                MethodDecl
                    modifiers: public
                    returnType: String
                    id: toString
                    Formals
                        (empty)
                    StmtList
                        returnStmt
                            Identifier
                                id: name

这篇关于面向对象编程语言的 AST(抽象语法树)是什么样的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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