compiler-construction - 解析树和抽象语法树(AST)有什么区别?

标签 compiler-construction terminology compiler-theory abstract-syntax-tree parse-tree

它们是由编译过程的不同阶段生成的吗?或者它们只是同一事物的不同名称?

最佳答案

这是基于 Expression Evaluator泰伦斯·帕尔的语法。

此示例的语法:

grammar Expr002;

options 
{
    output=AST;
    ASTLabelType=CommonTree; // type of $stat.tree ref etc...
}

prog    :   ( stat )+ ;

stat    :   expr NEWLINE        -> expr
        |   ID '=' expr NEWLINE -> ^('=' ID expr)
        |   NEWLINE             ->
        ;

expr    :   multExpr (( '+'^ | '-'^ ) multExpr)*
        ; 

multExpr
        :   atom ('*'^ atom)*
        ; 

atom    :   INT 
        |   ID
        |   '('! expr ')'!
        ;

ID      : ('a'..'z' | 'A'..'Z' )+ ;
INT     : '0'..'9'+ ;
NEWLINE : '\r'? '\n' ;
WS      : ( ' ' | '\t' )+ { skip(); } ;

输入

x=1
y=2
3*(x+y)

解析树

解析树是输入的具体表示。解析树保留输入的所有信息。空框代表空白,即行尾。

Parse Tree

AST

AST 是输入的抽象表示。请注意,AST 中不存在括号,因为关联可以从树结构中导出。

AST

有关更详细的解释,请参阅 Compilers and Compiler Generators第 页23
Abstract Syntax Trees上页。 21 在 Syntax and Semantics of Programming Languages

关于compiler-construction - 解析树和抽象语法树(AST)有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5026517/

相关文章:

linq - EF 和 Linq 中的术语

performance - "less/greater than than"的性能是否优于 "less/greater than or equal to"

compiler-construction - 变体转换系统

c++ - hdf5.h编译错误

c++ - 将指针转换为模板参数 : Comeau & MSVC compile, GCC 失败

c# - 解析没有花括号的代码块的技巧

language-agnostic - 编程上下文中的术语 "clause"

parameters - 参数和参数有什么区别?

javascript - 计算机科学中函数闭包的正式定义

android - 为什么同一段(简单的)Java 代码在不同的 Android 设备上表现非常不同?