【正文】
ar} ( 5) T→integer {T. Type:= integer} ( 6) T→↑T 1 {T. Type:= pointer (T1. type)} ( 7) T→array [num]of T1 {T. Type: = array (, )} 語句的類型檢查的翻譯模式 S→id :=E { if id. Type= E. Type Then S. Type:= void else S. Type:= type_ error} S→if E then S1 {if E. type= boolean then S. Type:= S1. type else S. type := type_ error} S→ while E do S1 {if E. type= boolean Then S. type:= S1. Type else S. type:= type_ error} 設(shè)計 類型檢查程序 In Decaf base types : int, double, bool, string pound types : arrays and classes. An array can be made of any type (either a base type, a class, or out of other arrays). Classes are a bit special in that the class name must be declared before it can be used in a declaration. ADTs can be constructed using classes, but they aren?t handled in any way differently than classes, so we don?t need to consider them specially. In Decaf the relevant language constructs ? constants, every constant has an associated type. A scanner tells us these types as well as the associated lexeme. ? variables : all variables (global, local, and instance) must have a declared type of either int, double, bool, string, array, or class. ? functions : functions have a return type, and each parameter in the function definition has a type, as does each argument in a function call. ? expressions an expression can be a constant, variable, function call, or some operator (binary or unary) applied to expressions. Each of the various expressions have a type based on the type of the constant, variable, return type of the function, or type of operands. ? The other language constructs in Decaf (if, while, Print, assignments, etc.) also have types associated with them, because somewhere in each of these we find an expression. The semantic rules ——govern what types are allowable in the various language constructs. In Decaf, ? operand to a unary minus must either be double or int, ? the expression used in a loop test must be of bool type, ? general rules, such as all variables must be declared before use, all classes are global, and so on. ? arrays: the index used in an array selection expression must be of integer type ? expressions: ? the two operands to % must both be int. The result type is int. ? this is bound to the receiving object within class scope, it is an error outside class scope ? variables : a variable declared of class type must refer to a defined class name ? functions : the type of each actual argument in a function call must be patible with the formal parameter。 if a function has a void return type, it may only use the empty return statement 實現(xiàn) 類型檢查程序 . ? 首先,將每個名字(標(biāo)識符)的類型信息記錄在符號表中 作用域檢查 作用域和可見性 基本作用域規(guī)則( lexical rule) int a。 void Binky(int a) { int a。 a = 2。 ... } 作用域檢查實現(xiàn): 1每個作用域一個獨立的符號表,這些符號表組織成作用域棧 2對所有作用域的全局符號表,每個作用域有一個作用域號 ?各自的優(yōu)缺點, PL/0用的是哪種 運算符(函數(shù))的重載 多態(tài)函數(shù) 重載運算符 ( overloading operator) 根據(jù)上下文可以執(zhí)行不同的運算 。 +是重載符號 , 在 A+ B中 , 當(dāng) A和 B為整數(shù) 、 實數(shù) 、復(fù)數(shù)或者矩陣時 , 運算符執(zhí)行不同類型的運算當(dāng)出現(xiàn)重載運算符時 , 要確定它所表示的唯一的意義 , 稱為運算符識別 。檢查運算符的操作數(shù) 。 多態(tài)函數(shù) 能實現(xiàn)對數(shù)據(jù)結(jié)構(gòu)進行操作的算法 , 不管數(shù)據(jù)結(jié)構(gòu)的元素類型是什么 多態(tài)函數(shù)的特點是,每次被調(diào)用時,傳遞過來的參數(shù)可以具有不同類型。 . . . 何謂中間代碼 ( Intermediate code ) ( Intermediate representation ) ( Intermediate language ) 源程序的一種內(nèi)部表示,不依賴目標(biāo)機的結(jié)構(gòu),易于機械生成 目 標(biāo)代碼的中間表示。 為什麼要此階段 邏輯結(jié)構(gòu)清楚;利于不同目標(biāo)機上實現(xiàn)同一種語言; ( 參考第 12 章的 275,276頁 ) 利于進行與機器無關(guān)的優(yōu)化 ;這些內(nèi)部形式也能用于解釋。 中間代碼的幾種形式 逆波蘭 四元式 三元式 間接三元式 樹 中間代碼 逆波蘭 : A B C D * + E C D – N ^ / + 四元式 : (1 ) ( C D T 1 ) (2 ) ( * B T 1 T 2) (3 ) ( + A T 2 T 3) (4 ) ( C D T 4) (5 ) ( ^ T 4 N T 5) (6 ) ( / E T 5 T 6) (7 ) ( + T 3 T 6 T 7)例 : A + B * ( C D ) + E / ( C D ) ^N 三元式 : (1 ) ( C D ) (2) ( * B (1) ) (3) ( + A (2) ) (4) ( C D ) (5) ( ^ (4) N ) (6) ( / E (5) ) (7) ( + (3 ) (6) )例 : A + B * ( C D ) + E / ( C D ) ^N 間接三元式 : 間接三元式序列 間接碼表 ( 1) ( C D ) ( 1 ) ( 2) ( * B ( 1) ) ( 2 ) ( 3) ( + A ( 2) ) ( 3 ) ( 4 ) ( ^ ( 1 ) N ) ( 1 ) ( 5) ( / E ( 4) ) ( 4 ) ( 6) ( + ( 3) (5 ) ) ( 5 ) ( 6 )例 : A + B * ( C D ) + E / ( C D ) ^N 簡單賦值語句的 ( 四元式)翻譯 四元式形式 : result := arg1 op arg2 語義屬性: , 函數(shù): lookup( ) 。 過程: emit(t := arg1 op arg2)。 newtemp。 產(chǎn)生式和語義描述: ( 1 ) S ? id := E { P :=lookup ( ) 。 if P ? nil then emit( P ― := ‖ ) else error } (op ,arg1,arg2,result)