CD Unit-3
CD Unit-3
UNIT – 3
PART –A
Type checker for a language is based on information about the syntactic constructs in the
language, the notion of types, and the rules for assigning types to language constructs.
A parse tree showing the values of attributes at each node is called an Annotated parse
tree.
The process of computing the attributes values at the nodes is called annotating (or
decorating) of the parse tree.
Of course, the order of these computations depends on the dependency graph induced by
the semantic rules.
4. Define Type checker.
A type checker verifies that the type of a construct matches that expected by its context.
For example:
Arithmetic operator mod in Pascal requires integer operands, so a type checker verifies
that the operands of mod have type integer. Type information gathered by a type checker may be
needed when code is generated.
5. What is a syntax tree? Draw the syntax tree for the assignment statement a := b * -c + b
* -c
A syntax tree for the assignment statement a:=b*-c+b*-c appear in the following figure.
The design of a type checker for a language is based on information about the syntactic
constructs in the language, the notion of types, and the rules for assigning types to language
constructs.
For example :
“ if both operands of the arithmetic operators of +,- and * are of type integer, then the
result is of type integer ”.
Production
E->E1+T
Semantic Rule
E.code=E1.code||T.code||’+
Syntax Tree
Post fix
Three-Address Code
10. Give the difference between syntax-directed definitions and translation schemes.
Quadruples
Triples
Indirect Triples.
S-attribute L-attribute
If an SDT uses only synthesized attributes, it is If an SDT uses both synthesized attributes and
called as S-attributed SDT. inherited attributes with a restriction that
inherited attribute can inherit values from left
siblings only, it is called as L-attributed
SDT.
S-attributed SDTs are evaluated in bottom-up Attributes in L-attributed SDTs are evaluated
parsing, as the values of the parent nodes by depth-first and left-to-right parsing
depend upon the values of the child nodes. manner.
Semantic actions are placed in rightmost place Semantic actions are placed anywhere in the
of RHS. RHS
POSTFIX NOTATION :
a+b*c = abc*+
15. Translate the conditional statement if a<b then 1 else 0 into three address code.
16. Test whether the following rules are L-attribute or not? Semantic rules
A.s = B.b;
B.i = f(C.c,A.s)
Inherited with limited rules. For A->X1X2…Xn, and there is an inherited attribute Xi.a
computed by a rule.
The rule must use only
Inherited attributes attributes of head A
Either inherited or synthesized attributes of X1,X2,…Xi‐1
Either inherited or synthesized attributes of Xi, but only if there are no
cycles
A->BC with semantic rules A.s=B.b; B.i=f(C.c,A.s) is not L‐attributed
Syntax directed definition specifies the values of attributes by associating semantic rules
with the grammar productions. It is a context free grammar with attributes and rules together
which are associated with grammar symbols and productions respectively.
20. Show the three address code sequence for the assignment statement. d=(a-b)+(a-c)+(a-c)