Compiler Design and Construction - Old Questions

6.  Write Syntax Directed Definition to carry out type checking for the following expression.

        E -> id | E1 op E2 | E1 relop E2 | E1[E2] | E1 

6 marks | Asked in 2076

E → id     { E.type = lookup(id.entry) }


E → E1 op E2     { if (E1.type = integer and E2.type = integer) then E.type = integer

        else if (E1.type = integer and E2.type = real) then E.type = real

        else if (E1.type = real and E2.type = integer) then E.type = real

        else if (E1.type = real and E2.type = real) then E.type = real

        else E.type = type-error }


E → E1 relop E2    { if E1.type = boolean and E2.type = boolean then E.type = boolean

                    else E.type = error}


E → E1 [E2]     { if (E2.type = int and E1.type = array(s, t)) then E.type = t

                   else E.type = type-error }


E →E1     { if E1.type = pointer(t) then E.type = t

                else type-error}