GATE / CS ARCHIVE

GATE CSE 1997 / QUESTION 23

Typed postfix syntax-directed translation

Compiler Design · Semantic Analysis & Type Checking · Full GATE CSE 1997 paper

GATE 1997, Q23 5 marks · Descriptive
Report issue

Semantic Analysis & Type Checking

Question 23, GATE 1997: The grammar is assign-stmt→LHS := E; E→E+T | T; T→T*V | V; V→id | (E); LHS→id. Variables have integer or real types. Convert expressions and assignments to postfix using typed tokens (+,i), (+,r), (*,i), (*,r), (:=,i), (:=,r). give-type(id) and give-name(id) return an identifier's type and name. Give a syntax-directed translation scheme. Mixed-type conversion and narrowing policies are not stated in the source.
Question 23, GATE 1997: The grammar is assign-stmt→LHS := E; E→E+T | T; T→T*V | V; V→id | (E); LHS→id. Variables have integer or real types. Convert expressions and assignments to postfix using typed tokens (+,i), (+,r), (*,i), (*,r), (:=,i), (:=,r). give-type(id) and give-name(id) return an identifier's type and name. Give a syntax-directed translation scheme. Mixed-type conversion and narrowing policies are not stated in the source.
Tap to enlarge
Answer & solution

Answer: Synthesize type and postfix code; choose numeric promotion for each operator, emit left operand then right operand then typed operator, and check assignment compatibility. (no official key was published)

Use synthesized attributes X.type and X.code for E,T,V; use LHS.type and LHS.name for the assignment target. A code value is a token list, and || denotes concatenation. Identifier type is i or r. The following semantic actions specify the traversal without depending on a particular parser implementation.

V→id:       V.type=give-type(id); V.code=[give-name(id)]
V→(E):      V.type=E.type; V.code=E.code
T→V:        T.type=V.type; T.code=V.code
E→T:        E.type=T.type; E.code=T.code
LHS→id:     LHS.type=give-type(id); LHS.name=give-name(id)

For E→E1+T, let t=join(E1.type,T.type), where join(i,i)=i and every other numeric pair gives r. Set E.type=t and E.code=E1.code || T.code || [(+,t)]. For T→T1*V, set t=join(T1.type,V.type), T.type=t and T.code=T1.code || V.code || [(*,t)]. Subscripting E1 and T1 distinguishes the right-hand-side occurrence from the left-hand-side occurrence of the same nonterminal.

For assign-stmt→LHS:=E, check that the expression can be assigned to the destination. Emit [address(LHS.name)] || E.code || [(:=,LHS.type)]. Writing the LHS first follows the conventional postfix assignment form x y (:=,i); the assignment operator consumes the target address and the computed value. In a textual postfix language the name in that first position can denote its address implicitly.

A necessary semantic convention: with precisely the six listed typed operator tokens, the r arithmetic operators must accept integer operands by implicit widening, and (:=,r) must widen an integer value when needed. Under that convention the rules above handle mixed expressions. For integer x,y and real z, the translation of z:=x+y*z is z x y z (*,r) (+,r) (:=,r). Multiplication is emitted first, preserving the grammar's precedence. A real-to-integer assignment is rejected unless an explicit narrowing rule has been authorized.

If the target instead requires both operands already to have the operator's physical type, implicit widening is unavailable. Then replace operand code c by c||[i2r] whenever its integer result is consumed as real, and similarly before assignment to a real target. This alternative introduces an extra conversion token not supplied in the question. Homogeneous expressions need no such extension. Type tags alone do not define a real-to-integer rounding rule or a machine-level cast. Keeping these conventions explicit avoids silently inventing conversion behavior while still giving a complete translation scheme for the stated operator alphabet.

Original paper, page 9