Compiler Design and Construction - Old Questions

Question Answer Details

8.  For the following grammar,

    

    Annotate the grammar with syntax directed definitions using synthesized attributes. Remove left recursion from the grammar and re-write the annotation correspondingly.

6 marks
Asked in 2072

Answer

AI Generated Answer

AI is thinking...

Official Answer

First part:

E → E + E  {E.val = E1.val + E2.val}

E → E * E  {E.val = E1.val * E2.val}

E → (E)  {E.val = E1.val}

E →id  {E.val = id.lexval}

Second part:

Removing left recursion as,

E → (E)R | id R

R → +ER1 | *ER1 | ε

Now add the attributes within this non-left recursive grammar as,

E → (E) {R.in = E1.val}R {E.val = R.s}

E →id {R.in = id.lexval} R1 { E.val = R.s}

R → +E {R1.in = E.val+R.in}R1 {R.s = R1.s}

R → *E { R1.in = E.val*R.in }R1 { R.s = R1.s }

R → ε { R.s = R.in}