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
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()
}