1、Compilers TechniquesSyntax-DirectedTranslatione.g. Language for typesetting mathematical formula(EQN)S B.ps := 10 B S.ht := B.ht B B1.ps := B.ps B1 B2.ps := B.ps B2 B.ht := max(B1.ht, B2.ht ) B B1.ps :=B.ps B1sub B2.ps := shrink(B.ps) B2 B.ht := disp (B1.ht, B2.ht ) B text B.ht := text.h B.ps Simula
2、te the Computation of Inherited AttributesWhen computing the font and height of text, it is impossible to determine the position of the inherited attribute values on which it depends.For example, the top of the stack element may be.text = BB text = B BB sub text = B sub BSimulate the Computation of
3、Inherited AttributesSimulate the Computation of Inherited AttributesProduction Semantic rulesS LB B.ps := L.s; S.ht := B.htL L.s := 10 B B1 MB2 B1.ps := B.ps; M.i := B.ps;B2.ps := M.s; B.ht := max(B1.ht, B2.ht )M M.s := M.iB B1 sub NB2 B1.ps := B.ps; N.i := B.ps;B2.ps := N.s; B.ht := disp(B1.ht, B2.
4、ht )N N.s := shrink(N.i) B text B.ht := text.h B.psSimulate the Computation of Inherited AttributesSM BL BBtextE B sub N BtextE text1s=10 psps s pshth=E.hh=E.h spsii psh=1.hhththththtSimulate the Computation of Inherited AttributesSimulate the Computation of Inherited AttributesProduction Semantic r
5、ulesS LB B.ps := L.s; S.ht := B.htL L.s := 10 B B1 MB2 B1.ps := B.ps; M.i := B.ps;B2.ps := M.s; B.ht := max(B1.ht, B2.ht )M M.s := M.iB B1 sub NB2 B1.ps := B.ps; N.i := B.ps;B2.ps := N.s; B.ht := disp(B1.ht, B2.ht )N N.s := shrink(N.i) B text B.ht := text.h B.psPut B.ps into the stack to useSimply f
6、or predictable attribute positionsFunction of calculationSimulate the Computation of Inherited AttributesProduction Semantic rulesS LB B.ps := L.s; S.ht := B.htL L.s := 10 B B1 MB2 B1.ps := B.ps; M.i := B.ps;B2.ps := M.s; B.ht := max(B1.ht, B2.ht )M M.s := M.iB B1 sub NB2 B1.ps := B.ps; N.i := B.ps;
7、B2.ps := N.s; B.ht := disp(B1.ht, B2.ht )N N.s := shrink(N.i) B text B.ht := text.h B.psRewrite to stack codeSimulate the Computation of Inherited AttributesProduction Semantic rulesS LB B.ps := L.s; S.ht := B.htL L.s := 10 B B1 MB2 B1.ps := B.ps; M.i := B.ps;B2.ps := M.s; B.ht := max(B1.ht, B2.ht )
8、M M.s := M.iB B1 sub NB2 B1.ps := B.ps; N.i := B.ps;B2.ps := N.s; B.ht := disp(B1.ht, B2.ht )N N.s := shrink(N.i) B text B.ht := text.h B.psSimulate the Computation of Inherited AttributesProduction Semantic rulesS LB B.ps := L.s; S.ht := B.htL L.s := 10 B B1 MB2 B1.ps := B.ps; M.i := B.ps;B2.ps :=
9、M.s; B.ht := max(B1.ht, B2.ht )M M.s := M.iB B1 sub NB2 B1.ps := B.ps; N.i := B.ps;B2.ps := N.s; B.ht := disp(B1.ht, B2.ht )N N.s := shrink(N.i) B text B.ht := text.h B.psSimulate the Computation of Inherited AttributesProduction Code segmentsS LB stacktop1.val := stacktop.valL L.s := 10 B B1 MB2 B1
10、.ps := B.ps; M.i := B.ps;B2.ps := M.s; B.ht := max(B1.ht, B2.ht )M M.s := M.iB B1 sub NB2 B1.ps := B.ps; N.i := B.ps;B2.ps := N.s; B.ht := disp(B1.ht, B2.ht )N N.s := shrink(N.i) B text B.ht := text.h B.psSimulate the Computation of Inherited AttributesProduction Code segmentsS LB stacktop1.val := s
11、tacktop.valL stacktop+1.val := 10B B1 MB2 B1.ps := B.ps; M.i := B.ps;B2.ps := M.s; B.ht := max(B1.ht, B2.ht )M M.s := M.iB B1 sub NB2 B1.ps := B.ps; N.i := B.ps;B2.ps := N.s; B.ht := disp(B1.ht, B2.ht )N N.s := shrink(N.i) B text B.ht := text.h B.psSimulate the Computation of Inherited AttributesPro
12、duction Code segmentsS LB stacktop1.val := stacktop.valL stacktop+1.val := 10B B1 MB2 stacktop2.val :=max(stacktop2.val, stacktop.val)M M.s := M.iB B1 sub NB2 B1.ps := B.ps; N.i := B.ps;B2.ps := N.s; B.ht := disp(B1.ht, B2.ht )N N.s := shrink(N.i) B text B.ht := text.h B.psSimulate the Computation o
13、f Inherited AttributesProduction Code segmentsS LB stacktop1.val := stacktop.valL stacktop+1.val := 10B B1 MB2 stacktop2.val :=max(stacktop2.val, stacktop.val)M stacktop+1.val := stacktop1.valB B1 sub NB2 B1.ps := B.ps; N.i := B.ps;B2.ps := N.s; B.ht := disp(B1.ht, B2.ht )N N.s := shrink(N.i) B text
14、 B.ht := text.h B.psSimulate the Computation of Inherited AttributesProduction Code segmentsS LB stacktop1.val := stacktop.valL stacktop+1.val := 10B B1 MB2 stacktop2.val :=max(stacktop2.val, stacktop.val)M stacktop+1.val := stacktop1.valB B1 sub NB2 stacktop3.val:= disp (stacktop3.val, stacktop.val
15、 )N N.s := shrink(N.i) B text B.ht := text.h B.psSimulate the Computation of Inherited AttributesProduction Code segmentsS LB stacktop1.val := stacktop.valL stacktop+1.val := 10B B1 MB2 stacktop2.val :=max(stacktop2.val, stacktop.val)M stacktop+1.val := stacktop1.valB B1 sub NB2 stacktop3.val:= disp
16、 (stacktop3.val, stacktop.val )N stacktop+1.val := shrink(stacktop2.val)B text B.ht := text.h B.psSimulate the Computation of Inherited AttributesProduction Code segmentsS LB stacktop1.val := stacktop.valL stacktop+1.val := 10B B1 MB2 stacktop2.val :=max(stacktop2.val, stacktop.val)M stacktop+1.val := stacktop1.valB B1 sub NB2 stacktop3.val:= disp (stacktop3.val, stacktop.val )N stacktop+1.val := shrink(stacktop2.val)B text stacktop.val := stacktop.val stacktop1.valCompilers TechniquesSyntax-DirectedTranslation