抽象语法树和具体语法树有什么区别? [英] What is the difference between an Abstract Syntax Tree and a Concrete Syntax Tree?

查看:189
本文介绍了抽象语法树和具体语法树有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读一些口译/编译器的工作方式,而令我感到困惑的一个方面是AST和CST之间的区别.我的理解是,解析器生成CST,然后将其交给语义分析器,后者将其转换为AST.但是,我的理解是语义分析器只是确保遵守规则.我真的不明白为什么它会进行任何更改以使其变得抽象而不是具体.

关于语义分析器,我是否缺少某些东西,或者AST和CST之间的区别是人为的?

解决方案

一个具体的语法树完全以解析形式表示源文本.通常,它符合定义源语言的上下文无关语法.

但是,具体的语法和树有很多东西可以使源文本明确地解析,但是对实际含义没有帮助.例如,要实现运算符优先级,您的CFG通常具有多个级别的表达式组件(项,因子等),而运算符将它们连接在不同的级别(您添加项以获取表达式,项由可选倍数的因子组成) , 等等.).但是,要真正解释或编译语言,则不需要此方法.您只需要具有运算符和操作数的Expression节点.抽象语法树是将具体语法树简化为代表程序含义所需的实际操作的结果.该树的定义要简单得多,因此在执行的后期阶段更易于处理.

您通常不需要实际构建具体的语法树. YACC(或Antlr,Menhir或其他任何东西)语法中的动作例程可以直接构建抽象语法树,因此具体语法树仅作为表示源文本的语法结构的概念实体存在. >

I've been reading a bit about how interpreters/compilers work, and one area where I'm getting confused is the difference between an AST and a CST. My understanding is that the parser makes a CST, hands it to the semantic analyzer which turns it into an AST. However, my understanding is that the semantic analyzer simply ensures that rules are followed. I don't really understand why it would actually make any changes to make it abstract rather than concrete.

Is there something that I'm missing about the semantic analyzer, or is the difference between an AST and CST somewhat artificial?

解决方案

A concrete syntax tree represents the source text exactly in parsed form. In general, it conforms to the context-free grammar defining the source language.

However, the concrete grammar and tree have a lot of things that are necessary to make source text unambiguously parseable, but do not contribute to actual meaning. For example, to implement operator precedence, your CFG usually has several levels of expression components (term, factor, etc.), with the operators connecting them at the different levels (you add terms to get expressions, terms are composed of factors optionally multipled, etc.). To actually interpret or compile the language, however, you don't need this; you just need Expression nodes that have operators and operands. The abstract syntax tree is the result of simplifying the concrete syntax tree down to this things actually needed to represent the meaning of the program. This tree has a much simpler definition and is thus easier to process in the later stages of execution.

You usually don't need to actually build a concrete syntax tree. The action routines in your YACC (or Antlr, or Menhir, or whatever...) grammar can directly build the abstract syntax tree, so the concrete syntax tree only exists as a conceptual entity representing the parse structure of your source text.

这篇关于抽象语法树和具体语法树有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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