关于编译原理:编译原理AST

42次阅读

共计 1655 个字符,预计需要花费 5 分钟才能阅读完成。

AST:形象语法树(abstract syntax code,AST)是源代码的形象语法结构的树状示意,树上的每个节点都示意源代码中的一种构造,这所以说是形象的,是因为形象语法树并不会示意出实在语法呈现的每一个细节,比如说,嵌套括号被隐含在树的构造中,并没有以节点的模式出现。
咱们将源代码转化为 AST 后,能够对 AST 做很多的操作,包含一些你想不到的操作,这些操作实现了各种各样不拘一格的性能,给你带进一个不一样的世界。懂编译原理的确能够随心所欲。

  • Most compilers break down into three primary stages: Parsing, Transformation, and Code Generation
  1. Parsing is taking raw code and turning it into a more abstract representation of the code.
  2. Transformation takes this abstract representation and manipulates to do whatever the compiler wants it to.
  3. Code Generation takes the transformed representation of the code and turns it into new code.

Parsing
Parsing typically gets broken down into two phases: Lexical Analysis and Syntactic Analysis.

  1. Lexical Analysis takes the raw code and splits it apart into these things called tokens by a thing called a tokenizer (or lexer).
  2. Syntactic Analysis takes the tokens and reformats them into a representation that describes each part of the syntax and their relation to one another. This is known as an intermediate representation or Abstract Syntax Tree.

An Abstract Syntax Tree, or AST for short, is a deeply nested object that represents code in a way that is both easy to work with and tells us a lot of information.

Transformation:
It can manipulate the AST in the same language or it can translate it into an entirely new language.
*

  • Let’s look at how we would transform an AST.

*

  • You might notice that our AST has objects within it that look very similar. Each of these are known as an AST Node. These nodes have defined properties on them that describe one
  • isolated part of the tree.
  • When transforming the AST we can manipulate nodes by
  • adding/removing/replacing properties, we can add new nodes, remove nodes, or
  • we could leave the existing AST alone and create an entirely new one based
  • on it.

*

  • Since we’re targeting a new language, we’re going to focus on creating an
  • entirely new AST that is specific to the target language.
正文完
 0