【文章內容簡介】
[ E (1) S→L:=E { if =null then /*L是簡單變量 */ emit( ‘:=’ ) else emit( ‘ [’ ‘]’ ‘:=’ )} (2) E→E 1 +E2 { :=newtemp。 emit( ‘:=’ E ‘+’ E )} (3) E→(E 1) {:=} (4) E→L { if =null then := else begin :=newtemp。 emit( ‘:=’ ‘[’ ‘]’) end } A[i1,i2,…,i k] ((… i1 n2+i2)n3+i3)…)n k+ik) w + base((…((low 1 n2+low2)n3+low3)…)n k+lowk) w (8) Elist→id [ E { :=。 :=1。 := } A[i1,i2,…,i k] ((… i1 n2+i2)n3+i3)…)n k+ik) w + base((…((low 1 n2+low2)n3+low3)…)n k+lowk) w (7) Elist→ Elist 1, E { t:=newtemp。 m:=+1。 emit(t ‘:=’ ‘*’ limit(,m))。 emit(t ‘:=’ t ‘+’ )。 := 。 :=t。 :=m } A[i1,i2,…,i k] ((… i1 n2+i2)n3+i3)…)n k+ik) w + base((…((low 1 n2+low2)n3+low3)…)n k+lowk) w (5) L→Elist ] { :=newtemp。 emit( ‘:=’ ‘- ’ C)。 :=newtemp。 emit( ‘:=’ w ‘*’ ) } (6) L→id { :=。 :=null } 例 : 賦值語句 : x:=A[y,z] ? T1:=y*20 ? T1:=T1+z ? T2:=A84 ? T3:=4*T1 ? T4:=T2[T3] ? x:=T4 類型轉換 ? 用 E的類型屬性 ? 對應產生式 E?E1 op E2的語義動作中關于: { if =integer a =integer :=integer else :=real } ? 算符區(qū)分為整型算符 int op和實型算符 real op, ? x:=y+ i*j 其中 x、 y為實型; i、 j為整型 。 這個賦值句產生的三地址代碼為: T1:=i int* j T3:=inttoreal T1 T2:=y real+ T3 x:=T2 關于產生式 E→E1 + E2 的語義動作 { :=newtemp。 if =integer and =integer then begin emit ( ‘:=’ E ‘int+’ E )。 :=integer end else if =real and =real then begin emit ( ‘:=’ E ‘real+’ E )。 :=real end else if =integer and =real then begin u:=newtemp。 emit (u ‘:=’ ‘inttoreal’ E )。 emit ( ‘:=’ u ‘real+’ E )。 :=real end else if =real and =integer then begin u:=newtemp。 emit (u ‘:=’ ‘inttoreal’ E )。 emit ( ‘:=’ E ‘real+’ u)。 :=real end else :=type_error} 記錄中域的引用 ? 符號表表項之中保存記錄中的域的類型和相對地址信息 布爾表達式的翻譯 ? 布爾表達式的兩個基本作用 : ?用于邏輯演算 ,計算邏輯值 。 ?用于控制語句的條件式 . ? 產生布爾表達式的文法 : E→E or E | E and E | ? E | (E) | i rop i | i ? 計算布爾表達式通常采用兩種方法 : 1. 如同計算算術表達式一樣 ,一步步算 1 or (not 0 and 0) or 0 =1 or (1 and 0) or 0 =1 or 0 or 0 =1 or 0 =1 2. 采用某種優(yōu)化措施 把 A or B解釋成 if A then true else B 把 A and B解釋成 if A then B else false 把 ? A解釋成 if A then false else true 兩種不同的翻譯方法 : ? 第一種翻譯法: A or B and C=D翻譯成 (1) (=, C, D, T1) (2) (and, B, T1, T2) (3) (or, A, T2, T3) ? 第二種翻譯法適合于作為條件表達式的布爾表達式使用 . 數(shù)值表示法 ? a or b and not c 翻譯成 T1:=not c T2:=b and T1 T3:=a or T1 ? ab的關系表達式可等價地寫成 if ab then 1 else 0 , 翻譯成 100: if ab goto 103 101: T:=0 102: goto 104 103: T:=1 104: 關于布爾表達式的數(shù)值表示法的翻譯模式 ? 過程 emit將三地址代碼送到輸出文件中 ? nextstat給出輸出序列中下一條三地址語句的地址索引 ? 每產生一條三地址語句后,過程 emit便把nextstat加 1 關于布爾表達式的數(shù)值表示法的翻譯模式 E→E 1 or E2 {:=newtemp。 emit( ‘:=’ E ‘or’ )} E→E 1 and E2 {:=newtemp。 emit( ‘:=’ E ‘and’ )} E→not E 1 {:=newtemp。 emit( ‘:=’ ‘not’ E )} E→(E 1) {:=} E?id1 relop id2 { :=newtemp。 emit(‘if’ relop. op id2. place ‘goto’ nextstat+3)。 emit( ‘:=’ ‘0’)。 emit(‘goto’ nextstat+2)。 emit(‘:=’ ‘1’) } E→id { := } 關于布爾表達式的數(shù)值表示法的翻譯模式 布爾表達式 ab or cd and ef翻譯成如下的一串三地址代碼 布爾表達式 ab or cd and ef翻譯成如下的一串三地址代碼 100: if ab goto 103 101: T1:=0 102: goto 104 103: T1:=1 104: if cd goto 107 105: T2:=0 106: goto 108 107: T2:=1 108: if ef goto 111 109: T3:=0 110: goto 112 111: T3:=1 112: T4:=T2 and T3 113: T5:=T1 or T4 作為條件控制的布爾式翻譯 ? 條件語句 if E then S1 else S2 賦予 E 兩種出口 :一真一假 To To goto …… : : ? 例:把語句 : if ac or b d then S1 else S2 翻譯成如下的一串三地址代碼 if ac goto L2 “真”出口 goto L1 L1: if bd goto L2 “真”出口 goto L3 “假”出口 L2: (關于 S1的三地址代碼序列 ) goto Lnext L3: (關于 S2的三地址代碼序列 ) Lnext: 考慮 and 的語義