0% found this document useful (0 votes)
29 views

CD Unit-3

Uploaded by

Akshaya Akshaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

CD Unit-3

Uploaded by

Akshaya Akshaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COMPILER DESIGN

UNIT – 3

PART –A

1.List out the two rules for type checking.

RULES FOR TYPE CHECKING:

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.

2. Compare synthesized attributes and inherited attributes.

3. What is Annotated parse tree?

ANNOTATED PARSE TREE:

 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 depicts the natural hierarchical structure of a source program.


 A DAG (Directed Acyclic Graph) gives the same information but in a more compact way
because common sub-expressions are identified.

A syntax tree for the assignment statement a:=b*-c+b*-c appear in the following figure.

6. Define type systems

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 ”.

7. Express the rule for checking the type of a function


8. Define Syntax directed definition of a simple desk calculator.

 A syntax directed definition specifies the values of attributes by associating semantic


rules with the grammar productions

Production

E->E1+T

Semantic Rule

E.code=E1.code||T.code||’+

9. Identify the different types of intermediate representation

TYPES OF INTERMEDIATE REPRESENTATION:

 Syntax Tree
 Post fix
 Three-Address Code

10. Give the difference between syntax-directed definitions and translation schemes.

SYNTAX-DIRECTED DEFINITIONS SYNTAX-DIRECTED TRANSLATION


SCHEMES
SDD is a generalization of CFG in which A SDT scheme is a context-free grammar
each grammar productions X->α is with program fragments embedded within
associated production bodies . The program fragments
with it a set of semantic rules of the form are called semantic actions and can appear
a: = f(b1,b2.....bk) at any position within the production body.
Where a is an attributes obtained from the
function f.
Semantic rules set up dependencies between Any SDT can be implemented by first
attributes which can be represented by a building a parse tree and then pre-forming
dependency Graph the actions in a left to-right depth first order.
i.e during preorder traversal.

11. State the type expressions.

The type of a language construct will be denoted by a “type expression” . Informally a


type expression is either a basic type or is formed by applying an operator called a type
constructor to other type expression.
12. Illustrate the methods of implementing three-address statement

THE METHODS OF IMPLEMENTING THREE-ADDRESS STATEMENT:

 Quadruples
 Triples
 Indirect Triples.

13. Differentiate S-attribute and L-attribute definitions

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

14. Create postfix notation for the given expression a+b*c

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

17. What are the methods of representing a syntax tree?

 Mknode (op, left, right)


 Mkleaf (id,entry)
 Mkleaf (num, val)

18. Explain the syntax directed definition for if-else statement

19. Examine the usage of syntax directed definition

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)

You might also like