2 exp exp op expexpnumber op 34 exp
2
简单整型算术表达式文法: exp → exp op exp|(exp)|number op → +|-|* 符号串 3+4的最左推导: exp op exp number + number 7
1 exp 2 exp 3 op 4 exp number + number 3+4的分析树 8
简单整型算术表达式文法: exp → exp op exp|(exp)|number op → +|-|* exp exp op number exp + number 3+4的最右推导 9
1 exp 4 op 3 number + exp 2 number 3+4的分析树 10
Typedef enum {Plus, Minus, Times} Op. Kind; Typedef enum {Op. Kind, Const. Kind} Exp. Kind; Typedef struct streenode { Exp. Kind kind; Op. Kind op; struct streenode *lchild, *rchild; int val; }Stree. Node Typedef Stree. Node *Syntax. Tree; 14
cabd 变量token存储当前扫描的下一个单词 void match(expected. Token); { if token=expected. Token token = get. Token(); else error; } 24
void S(void) { match(c); A(); match(d); } void A(void) { match(a); match(b); } 25
识别S的递归下降函数的伪代码: void S(void) void A 1(void) { match(c); { match(a); A 1(); //记住当前的token位置 } match(d); void A 2(void) if(error) then { match(a); { A 2(); match(b); match(d); } } } 27
例4 -4:一个简化了的if文法规则: if-stmt→if (exp) statement |if (exp) statement else statement 写出识别if-stmt的递归下降函数的伪代码 33
viod if. Stmt(void) { match(if); match((); exp(); match()); statement(); if token = else then { match(else); statement(); } } 35
2. 重复情况: 简单整型算术表达式文法: exp→exp addop term|term addop→+|term→term mulop factor|factor mulop→* factor→(exp)|number 写出识别表达式exp的递归下降函数的伪代码 36
viod exp(void) { term( ); while token=+ or token={ match(token); term( ); } } 40
简单整型算术表达式文法: exp→exp addop term|term addop→+|term→term mulop factor|factor mulop→* factor→(exp)|number 写出识别表达式exp的完整的递归下降函数的伪代码? 41
if语句的抽象语法树结构: if-stmt if exp then stmt-sequence[else stmt-sequence]end 49
repeat 语句 repeat-stmt repeat stmt-sequence until exp 50
assign 语句 assign_stmt identifier : = exp 51
write语句 write_stmt write exp read语句 read_stmt read identifier 52
typedef enum{Stmt. K, Exp. K}Node. Kind; typedef enum{If. K, Repeat. K, Assign. K, Read. K, Write. K}Stmt. Kind; typedef enum{Op. K, Const. K, Id. K}Exp. Kind; typedef enum{Void, Integer, Boolean}Exp. Type; #define MAXCHILDREN 3 54
typedef struct tree. Node {struct tree. Node* child[MAXCHILDREN]; struct tree. Node* sibling; int lineno; Node. Kind nodekind; union{ Stmt. Kind stmt; Exp. Kind exp; } kind; union{ Token. Type op; int val; char * name; } attr; Exp. Type type; } Tree. Node; 55
Tiny语言的递归下降分析函数如下:PARSE. C 一个输出其输入阶乘的TINY语言程序 read x; if x>0 then fact: =1; repeat fact: =fact* x; x: =x-1 until x=0; write fact end 56
57
一个输出其输入阶乘的TINY语言程序 read x; if x>0 then fact: =1; repeat fact: =fact* x; x: =x-1 until x=0; write fact end 58
59
例4 -3:考虑下述简单整型表达式文法,求 各非终结符的First()集合; (1) exp→exp addop term (2) exp→term (3) addop→+ (4) addop→(5) term→term mulop factor (6) term → factor (7) mulop→* (8) factor→(exp) (9) factor→number 75
First(addop)= {+, -} First(mulop)= {*} First(term)= {(, number} First(factor)= {(, number} First(exp)= {(, number} 76
例4 -5:考虑下述简单整型表达式文法,求 各非终结符的Follow()集合; (1) exp→exp addop term (2) exp→term (3) addop→+ (4) addop→(5) term→term mulop factor (6) term → factor (7) mulop→* (8) factor→(exp) (9) factor→number 81
Follow(exp)= {$, +, -, )} Follow(addop)= {(, number} Follow(term)= {$, +, -, *, )} Follow(mulop)= {(, number} Follow(factor)= {$, +, -, *, )} 82
Follow(T’) Follow(T) Follow(F) First(T’) Follow(T) 85
a. 所求得的各非终结符的First集合和 Follow集合如下: First(declaration)={int, float} First(type) ={int, float} First(var-list) ={identifier} First(list) ={, , } 90
Follow(declaration)={$} Follow(type)={identifier} Follow(var-list)={$} Follow(list) ={$} 91
对各产生式进行编号: r 1: declaration→type var-list r 2: r 3: r 4: r 5: type→int type→ float var-list→identifier list → , var-list r 6: list → b. 构造该文法的LL(1)分析表。 92
M[N, T] declaration type var-list int r 1 r 2 float r 1 r 3 identifier , $ r 5 r 6 r 4 c. 由于文法G相关的LL(1)分析表的每个项目中至 多只有一个产生式,故该文法是LL(1)文法。 93
- Slides: 110