📜  布尔表达式

📅  最后修改于: 2020-12-06 07:28:27             🧑  作者: Mango

#### 布尔表达式

``````E  →  E OR E
E  →  E AND E
E  →  NOT E
E  →  (E)
E →  id relop id
E  →  TRUE
E  →  FALSE
``````

AND和OR保持关联。 NOT的优先级高于AND和最后OR。

Production rule Semantic actions
E → E1 OR E2 {E.place = newtemp();
Emit (E.place ‘:=’ E1.place ‘OR’ E2.place)
}
E → E1 + E2 {E.place = newtemp();
Emit (E.place ‘:=’ E1.place ‘AND’ E2.place)
}
E → NOT E1 {E.place = newtemp();
Emit (E.place ‘:=’ ‘NOT’ E1.place)
}
E → (E1) {E.place = E1.place}
E → id relop id2 {E.place = newtemp();
Emit (‘if’ id1.place relop.op id2.place ‘goto’
nextstar + 3);
EMIT (E.place ‘:=’ ‘0’)
EMIT (‘goto’ nextstat + 2)
EMIT (E.place ‘:=’ ‘1’)
}
E → TRUE {E.place := newtemp();
Emit (E.place ‘:=’ ‘1’)
}
E → FALSE {E.place := newtemp();
Emit (E.place ‘:=’ ‘0’)
}

EMIT函数用于生成三个地址代码，而newtemp()函数用于生成临时变量。

E→id relop id2包含next_state，并给出输出序列中下三个地址语句的索引。

``````p>q AND rr
100: if p>q goto 103
101: t1:=0
102: goto 104
103: t1:=1
104: if r>s goto 107
105: t2:=0
106: goto 108
107: t2:=1
108: if u>v goto 111
109: t3:=0
110: goto 112
111: t3:= 1
112: t4:= t1 AND t2
113: t5:= t4 OR t3
``````