【正文】
if P ? nil then emit( P ― := ‖ ) else error } (op ,arg1,arg2,result) 或 。 newtemp。 中間代碼的幾種形式 逆波蘭 四元式 三元式 間接三元式 樹 中間代碼 逆波蘭 : 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( ) 。 . . . 何謂中間代碼 ( Intermediate code ) ( Intermediate representation ) ( Intermediate language ) 源程序的一種內(nèi)部表示,不依賴目標(biāo)機的結(jié)構(gòu),易于機械生成 目 標(biāo)代碼的中間表示。檢查運算符的操作數(shù) 。 ... } 作用域檢查實現(xiàn): 1每個作用域一個獨立的符號表,這些符號表組織成作用域棧 2對所有作用域的全局符號表,每個作用域有一個作用域號 ?各自的優(yōu)缺點, PL/0用的是哪種 運算符(函數(shù))的重載 多態(tài)函數(shù) 重載運算符 ( overloading operator) 根據(jù)上下文可以執(zhí)行不同的運算 。 void Binky(int a) { int a。 假定所有數(shù)組都從下標(biāo) 1開始 確定標(biāo)識符類型的部分翻譯模式 ( 1) P→D ; E ( 2) D→D ; D ( 3) D→id :T {addtype (id. Entry, T. type)} ( 4) T→char {T. Type:= char} ( 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。 key mod 1999 語言本身提供兩種基本類型: char和 integer。 ? ... ? } 強類型的 任何數(shù)據(jù) 類型都可以在 編譯時確定 弱類型的 . 進行類型檢查的時間:編譯時,運行時,或者兩者結(jié)合 . 靜態(tài)類型檢查 編譯時進行類型檢查 動態(tài)類型檢查,將類型信息并到運行時每個數(shù)據(jù)單元中 . 隱含類型轉(zhuǎn)換 . P→ D; E D→ D; |id:T T→ char | integer | aray [num]of T| ↑ T E→ literal|num | id| E mod E| E [E]|E↑ P代表程序; D代表說明; E代表表達式 。 // the program ? int main() { ? int m[3]。 // function prototype ? int x = 0。 聲明是程序中的一個語句 ,是把數(shù)據(jù)對象的名稱和類型,以及生命周期信息