

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Example : consider, the boolean expression
a < b or c < d and e < f
Recall the syntax directed translation for the production
E → id 1 relop id 2 { E.truelist = makelist(nextquad()); E.falselist = makelist(nextquad()+1); emit(‘if’ id 1 relop id 2 ‘goto _’) ; emit(‘goto _’ ); }
We carry out a bottom-up parse. In response to reduction of a < b to E, the list E.truelist gets {100} and E.falselist gets {101} and the two quadruples
100: if a < b goto _ 101: goto _
are generated. Notice that the goto’s are generated with targets. These are precisely the goto’s whose quad indices 100 and 101 are recorded in the truelist and falselist attributes of E. These will be patched later in the parse via the backpatching mechanism.
The next reduction to happen is M → ε which is in the production
E → E 1 or M E 2
This reduction will eventually take place when reduction to E 2 happens. This marker non- terminal M records the value of nextquad which at this time is 102. Next, the reduction of c < d to E leads to the list E.truelist getting {102}, E.falselist getting {103} and the two quadruples
102: if c < d goto _ 103: goto _
are generated.
Next reduction is M → ε. Τhe marker non-terminal M in the production
E → E 1 and M E 2
records the value of nextquad which at this time is 104, the quad index of first quad of E 2. Reducing e < f to E causes E.truelist to get {104}, E.falselist to get {105} and the generation of quads
104: if e < f goto _ 105: goto _
We now reduce by the production
E → E 1 and M E 2
Recall the semantic actions associated with this rule:
E → E 1 and M E 2 { backpatch(E 1 .truelist, M.quad); E.truelist = E 2 .truelist; E.falselist = merge(E 1 .falselist, E 2 .falselist); }
The six quadruples generated so far are
100: if a < b goto _ 101: goto _ 102: if c < d goto _ 103: goto _ 104: if e < f goto _ 105: goto _
The semantic action calls
backpatch({102},104)
The backpatch fills in 104 as the target of the goto in quad 102.
100: if a < b goto _ 101: goto _ 102: if c < d goto 104 103: goto _ 104: if e < f goto _ 105: goto _
The next two semantic actions define E.truelist and E.falselist. This way, the synthesized attributes propagate the attributes up the parse tree.
We now reduce by the production