What's the difference between parse trees and abstract syntax trees?

前端 未结 5 1727
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-22 18:58

I found the two terms in a compiler design book, and I\'d like to know what each stands for, and how they are different.

I searched on the internet and found that pa

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-22 19:57

    An AST describes the source code conceptually, it doesn't need to contain all the syntactical elements required to parse some source code (curly braces, keywords, parenthesis etc.).

    A Parse tree represents the source code more closely.

    In an AST the node for an IF statement could contain just three children:

    • Condition
    • If Case
    • Else Case

    For a C-like language the Parse Tree would need to contain nodes for the 'if' keyword, parenthesis, curly braces also.

提交回复
热议问题