c语⾔ast编译原理,关于编译原理:编译原理AST
游戏战歌网
AST:形象语法树(abstract syntax code,AST)是源代码的形象语法结构的树状⽰意,树上的每个节点都⽰意源代码中的⼀种构造,这所以说是形象的,是因为形象语法树并不会⽰意出实在语法呈现的每⼀个细节,⽐如说,嵌套括号被隐含在树的构造中,并没有以节点的模式出现。
一曲新词酒一杯咱们将源代码转化为AST后,能够对AST做很多的操作,包含⼀些你想不到的操作,这些操作实现了各种各样不拘⼀格的性能,给你带进⼀个不⼀样的世界。懂编译原理的确能够随⼼所欲。
永远跟你走Most compilers break down into three primary stages: Parsing, Transformation, and Code Generationchange金泫雅
Parsing is taking raw code and turning it into a more abstract representation of the code.
Transformation takes this abstract representation and manipulates to do whatever the compiler wants it to.
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.
Lexical Analysis takes the raw code and splits it apart into these things called tokens by a thing called a tokenizer (or lexer).
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.