60 likes | 188 Views
Compiler 報告. 第 10 組 91156235 鮑志軒 92156227 劉致顯. Mixed-Mode Boolean Expressions. Boolean 簡單來說,其實就是一項敘述的正確值為1 ,若否定則其值為0 一個簡單的例子: (a<b) + (b<a) 此種情況下,若 a = b ,則其值為 0 a != b ,則其值為 1. Mixed-Mode Boolean Expressions.
E N D
Compiler報告 第10組 91156235鮑志軒 92156227劉致顯
Mixed-Mode Boolean Expressions • Boolean簡單來說,其實就是一項敘述的正確值為1 ,若否定則其值為0 • 一個簡單的例子:(a<b) + (b<a)此種情況下,若a = b ,則其值為0 a != b ,則其值為1
Mixed-Mode Boolean Expressions • 今天有一個productionE -> E + E | E and E | E relop E | id • (E and E) ,(E relop E) 為boolean • (id) 為arithmetic • (E + E) 則比較特別,因為它有可能是2個arithmetic type相加,也有可能其中包含了boolean type
Mixed-Mode Boolean Expressions • 因此在探討 (E -> E + E) 這一個部份時,我們給予其type的attribute值,並且把它改成(E -> E1 + E2) 以方便辨別 • 並且把 arithmetic 簡寫為 arith boolean 簡寫為 bool • 其中boolean type包含E.true & E.false兩值 • 而E.place則負責運算的部份
Semantic rule for (E -> E + E) • E.type := arith; • if E1.type = arith and E2.type = arith • thenbegin • /* normal arithmetic add */ • E.place := newtemp; • E.code := E1.code || E2.code || • gen(E.place ':=' E1.place '+' E2.place) • end • elseif E1.type = arith and E2.type = bool • then begin • E.place := newtemp; • E2.true := newlabel; • E2.false := newlabel; • E.code := E1.code || E2.code || • gen(E2.true ':' E.place ':=' E1.place + 1)|| • gen('goto' nextstat + 1)|| • gen(E2.false ':' E.place ':=' E1.place) • end • else if .......................
Mixed-Mode Boolean Expressions • E2.true : E.place := E1.place + 1 • goto nextstat + 1 • E2.false : E.place := E1.place