Compiler Design and Construction - Old Questions

2. What are static and dynamic type checking? Write SDD to carry out type checking for the following expression.

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

10 marks | Asked in Model Question

In static type checking, the type of variable is known at compile time (it checks the type of variable before running). Typical examples of static checking are:

    Type checks: Report an error if an operator is applied to an incompatible operand.

    Flow-of-control checks: Statements that results in a branch need to be terminated correctly.

    Uniqueness checks: There are situations where an object must be defined exactly once.

    Name-related checks: Sometimes the same names may be appeared two or more times.

In dynamic type checking, the type of variable is known at runtime (it checks the type of variable while executing). Compiler generates verification code to enforce programming language's dynamic semantics. If a programming language has no any dynamic checking, it is called strongly typed language i.e. there will be no type errors during run-time.


Now,

SDD to carry out type checking for the given expression is given below:

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}