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

查看:742
本文介绍了面向对象编程语言的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看起来像?

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 中的函数声明, 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天全站免登陆