七.控制语句也可采用改写文法的方法 S→ ifb then s ifB then S, else S2 while b do s 可将上述文法改写为:
七. 控制语句也可采用改写文法的方法 S→if B then S1 │if B then S1 else S2 │while B do S1 可将上述文法改写为:
C→→ ifb then T→>CS1else W→ while R→WBdo S→CS1TS1|RS1
C→if B then T→C S1 else W→while R→W B do S→C S1 | T S1 | R S1
C→→ ifb then i backpatch(B truelist, ip) C netlist: =B. falselist T→→CS1else i g: ip; emt6220); ); backpatch(C netlist, p T netlist: merge(q (q, s netlist))
C→if B then { backpatch(B.truelist, ip); C.nextlist:=B.falselist } T→C S1 else { q:=ip; emit(j,-,-,0); backpatch(C.nextlist,ip); T.nextlist:=merge(q, S1 .nextlist) }
W→ while ( Wcode = ip R→WBdo backpatch(B truelist, p R netlist =B. falselist R code: =Wcode j
W→while { W.code:=ip } R→W B do { backpatch(B.truelist,ip); R.nextlist:=B.falselist; R.code:=W.code }
S→CS Snetlist: merge (C. nxtlisrt, S,. nextlist)) S→TS1 iS netlist: -merge(T. nxtlisrt, S netlist)) S→RS1 backpatch(S,. nextlist, R code) emit(, ",",Rcode); S netlist: R netlist
S→C S1 { S.nextlist:=merge(C.nxtlisrt, S1 .nextlist) } S→T S1 { S.nextlist:=merge(T.nxtlisrt, S1 .nextlist) } S→R S1 { backpatch(S1 .nextlist,R.code); emit(j,-,-,R.code); S.nextlist:=R.nextlist }
八.数组元素按列存放时的处理 D=a+(in-In*dn- *dn-2..d +(in1-n1)*dn2*dn3*….*d ++(212)*d1+(i1-1) D=CONSPART+ VARPART CONSPART=a-C C=ln*dn1*dn2*….*d1+ln1*dn2*dn3*…,*d1 .+12*d1+1 VARPART=n*dn1*dn2*…*d1+in1*dn2*dn32…,*d1+…+i2*d1+i1 (.((n0dn1+in1)*dn2+in2)*dn3+..+i2)*d1+i
八. 数组元素按列存放时的处理 D=a+(in -ln )*dn-1*dn-2*…*d1 +(in-1 -ln-1 )*dn-2*dn-3*…*d1 +…+(i2 -l2 )*d1+(i1 -l1 ) D=CONSPART+VARPART CONSPART=a-C C=ln*dn-1*dn-2*…*d1 +ln-1*dn-2*dn-3*…*d1 +…+l2*d1+l1 VARPART =in*dn-1*dn-2*…*d1 +in-1*dn-2*dn-3*…*d1 +…+i2*d1+i1 =(. . .((in*dn-1+in-1 )*dn-2+ in-2 )*dn-3 +. . .+i2 )*d1+i1
方法 VARPART的值不能按下标式的出现顺序 从左到右进行累计,它必须待所有下标 式都处理完毕之后,再从右到左进行计 算 设置一个栈 STACK,记录每个下标式结 果值的存放单元。待到所有下标式都处 理完毕之后,再自栈顶而下,按 VARPART的算式,累计它的值
方法: VARPART的值不能按下标式的出现顺序 从左到右进行累计,它必须待所有下标 式都处理完毕之后,再从右到左进行计 算。 设置一个栈STACK,记录每个下标式结 果值的存放单元。待到所有下标式都处 理完毕之后,再自栈顶而下,按 VARPART的算式,累计它的值
为Elis赋予一个语义属性 STACK 不用改写文法 A→V:=E V→[Elist] Elist→ Elist1,E|E E→E1opE2|E1|(E1)|V
为Elist赋予一个语义属性STACK 不用改写文法 A→V:=E V→i[Elist]│i Elist→Elist1 ,E | E E→E1 op E2│-E1│(E1 )│V