【正文】
分析舉例 ? 一個(gè) C源程序片段: int a。 – 確定類型 – 類型檢查 – 識(shí)別含義與相應(yīng)的語(yǔ)義處理 – 其它靜態(tài)語(yǔ)義檢查 ? 為了優(yōu)化,往往先生成內(nèi)部中間表示代碼:如逆波蘭表示、三元式序列、四元式序列,或者抽象語(yǔ)法樹(shù)。 ( 2)語(yǔ)法分析:將單詞序列重組為程序結(jié)構(gòu) (通常表示為分析樹(shù))。 2020/6/28 40 錯(cuò)誤處理 單詞錯(cuò) 語(yǔ)法錯(cuò) 語(yǔ)義錯(cuò) (如多重定義、類型不匹配) 環(huán)境錯(cuò) (如數(shù)組太大、名字太長(zhǎng)) 溢出 外設(shè)錯(cuò)誤 ( I/O錯(cuò)) 訪問(wèn)內(nèi)存越界 編譯 錯(cuò)誤 運(yùn)行錯(cuò)誤 邏輯錯(cuò)誤 錯(cuò)誤分類 2020/6/28 41 Compiler Cousins ? Preprocessors : Provide Input to Compilers – Macro Processing – File Inclusion – Rational Preprocessors – Language Extensions for a Database System ? Assemblers ? Loaders and LinkEditors 2020/6/28 42 define in C: does text substitution before piling define X 3 define Y A*B+C define Z getchar() Macro Processing 2020/6/28 43 File Inclusion include in C bring in another file before piling ////// ////// ////// include “” … … … … … … … … … ////// ////// ////// … … … … … … … … … 2020/6/28 44 Rational Preprocessors ? Augment ―Old‖ Languages With Modern Constructs ? Add Macros for If Then, While, Etc. ? Define Can Make C Code More Pascallike define begin { define end } define then 2020/6/28 45 Language Extensions for a Database System EQUEL Database query language embedded in C Retrieve (DN=) where = ?Research? is Preprocessed into: ingres_system(“Retr…..Research?”,____,____)。典型的編譯程序一般遍數(shù)在二至三遍。 一個(gè)編譯系統(tǒng)可以看成是一個(gè)變換系統(tǒng),進(jìn)行一系列的變換,即 (SL, L1), (L1, L2),….( Lk, TL) 其中 SL為源語(yǔ)言, TL為目標(biāo)語(yǔ)言 2020/6/28 55 Number of Passes: Single Preferred Multiple Easier, but less efficient Tradeoffs …….. 遍 (pass) ? 多遍掃描的好處 – 節(jié)省內(nèi)存空間,提高目標(biāo)代碼質(zhì)量,使編譯的邏輯結(jié)構(gòu)清晰。 – 注:在內(nèi)存許可情況下,還是遍數(shù)盡可能少些為好。所謂一遍是指,編譯程序以一種表示形式為輸入,經(jīng)過(guò)處理產(chǎn)生下一種表示形式的過(guò)程。 2020/6/28 39 符號(hào)表管理 ? 符號(hào)表存放與單詞有關(guān)的信息,如標(biāo)識(shí)符的類型、地址,常數(shù)的值等等。 … position := initial + rate*60 2020