Compiler Design and Construction - Old Questions

9.  Consider the following grammar for arithmetic expression using an operator ‘op’ to integer or real numbers.

    E → E1 op E2 | num.num | num | id

Give the syntax directed definitions to determine the type of expression as when two integers are used in expression, resulting type is integer otherwise real.
6 marks | Asked in 2072

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

E → num {E.type = integer }

E → num.num { E.type = real}

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

                        then integer

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

                        then real

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

                        then real

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

                        then real

                        else type_error()

            }