...menustart
- Compiler Note
- Introduction
...menuend
2 parts of compiler : analysis / synthesis
- tokens and symbol table
- syntax describes the proper form of its programs
- use context-free grammars to specify the syntax of a language
- CFG itself is specified by listing their productions.
- Parser的任务是 find a parse tree for a given string of terminals
-
derivation 的过程
- root -- start symbol
- interior node -- nonterminal
- correspond to a production with its children
- leaf -- terminal or eps
- from left to right, the leaves form the yield of the tree
-
CST 其实就是 parse tree。
-
CST只是概念上的语法术,大部分编译器都使用AST。
- interior -- an operator
- children of the node -- the operands of the operator
- AST 是一种数据结构
- Idea to any number n of precedence levels
- We need n+1 nonterminals.
- The first,like factor , can never be torn apart.
- Then, for each precedence level, there is one nonterminal representing expressions that can be torn apart only by operators at that level or higher
- 将输入的字符串翻译成一些列具有语义的动作
- SDT is done by attaching rules or program fragments to productions in a grammar.
- SDD是特殊的CFG , 特殊之处在于:
- We associate attributes with nonterminals and terminals.
- Then, we attach rules to the productions of the grammar;
| PRODUCTION | SEMANTIC RULES |
|---|---|
| expr → expr₁ + term | expr.t = expr₁.t ‖ term.t ‖ '+' |
- SDTS 也是一种特殊的CFG
- it attachs program fragments to productions in a grammar.
- SDTS 和 SDD 相似,SDD指定语义动作,SDTS实现了这些语义动作。 ??
expr → expr₁ + term {print('+')}
expr → expr₁ - term {print('-')}
expr → term
term → 0 {print('0')}
- Parsing is the process of determining how a string of terminals can be generated by a grammar.
- "recursive descent" method can be used both to parse and to implement syntax-directed translators.
- Yacc is a alternative tool
- to generate a translator directly from a translation scheme
- 可以较容易地手工构造出高效的语法分析器
- can handle a larger class of grammars and translation schemes
- software tools for generating parsers directly from grammars often use bottom-up methods.
- a simple form of recursive-descent parsing
- the lookahead symbol unambiguously determines the flow of control
- is a program consisting of a procedure for every nonterminal
- FIRST(α)
- semantic define what its programs mean
- gather type info
- type checking
- coercions
- AST -> three-address code
--- analysis end ---