CD WINTER 2020 Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

3170701 Complier Design WINTER 2020

Q.1 (a) Compare and contrast compilers and interpreters.

(b) Write a short note on LEX Tool.


 Lex is a tool in lexical analysis phase to recognize tokens using regular expression.
Lex tool itself is a lex compiler.

1 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
 lex.l is an a input file written in a language which describes the generation of lexical
analyzer. The lex compiler transforms lex.l to a C program known as lex.yy.c.
 lex.yy.c is compiled by the C compiler to a file called a.out.
 The output of C compiler is the working lexical analyzer which takes stream of input
characters and produces a stream of tokens.
There are two parts of the LEX source program −

 Auxiliary Definitions
 Translation Rules

Auxiliary Definition
 It denotes the regular expression of the form.

Translation Rules
 It is a set of rules or actions which tells Lexical Analyzer what it has to do or what it has
to return to parser on encountering the token.

2 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(c) Explain phases of compilers with suitable example.

3 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
4 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
5 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
Q.2 (a) Define terms: pattern, lexeme, token
Pattern: A set of strings in the input for which the same token is produced as output. This set
of strings is described by a rule called a pattern associated with the token.
Lexeme: A lexeme is a sequence of characters in the source program that is matched by the
pattern for a token.
 Token: Token is a sequence of characters that can be treated as a single logical entity.
Typical tokens are,
1) Identifiers 2) keywords 3) operators 4) special symbols 5)constants

Description of token

Token lexeme pattern

const const const

if if if
< or <= or = or < > or >= or
<,<=,= ,< letter
relation >,>=,> followed by letters & digit

i pi any numeric constant

nun 3.14 any character b/w “and “except"

literal "core" pattern

A patter is a rule describing the set of lexemes that can represent a particular token in source
program.

(b) Write any one method used in lexical analyzer for buffering the input.
X

(c) Construct nondeterministic finite automata by using Thomson’s


Construction for following regular expression. Show the sequence of moves
made in processing the input string ababbab. (a|b)* abb

6 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
7 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
8 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
Q.3 (a) Consider the following grammar to construct leftmost and right most
derivation for the sentence abab. SaSbS|bSaS|ε

9 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(b) Write rule(s) to check grammar is left recursive or not. Remove left
recursive from the following grammar.

S aBDh BBb|c DEF E g| ε F f| ε

10 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(c) Construct the SLR parsing table for the following Grammar.

E E+T | T

T  T*F | F

F  (E) | id

(refer technical publication page 3-78 example-3.7.4)

Q.4 (a) Write rule(s) to check grammar is operator grammar or not.


If the grammar satisfies the following two conditions, then we can say that type of grammar is
called as operator precedence grammar.
1. f ε is on its RHS then there exists no production rule.
2. If two non-terminals are adjacent to each other on its RHS then there exists no
production rule.
Operator Grammars have the property that no production right side is empty or has two
adjacent non-terminals.

Example

EE A E | id
A + | *
The above grammar is not an operator grammar but we can convert that grammar into operator
grammar like −
E E + E | E * E | id

11 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(b) Explain any two error-recovery strategies.

12 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
13 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(c) Construct LALR parsing table for the following grammar.
S CC C  cC C  d

14 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
15 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
Q.5 (a) Define: 1) synthesized attribute 2) inherited attribute
1) synthesized attribute

Synthesized attribute is an attribute whose parse tree node value is determined by the attribute
value at child nodes.
To illustrate, assume the following production S → ABC if S is taking values from its child
nodes (A, B, C), then it is said to be a synthesized attribute, as the values of ABC are synthesized
to S.
Synthesized attribute is used by both S-attributed SDT and L-attributed STD.

2) inherited attribute
On other hand an attribute is said to be Inherited attribute if its parse tree node value is
determined by the attribute value at parent and/or siblings node.
In case of S → ABC if A can get values from S, B and C. B can take values from S, A, and C.
Likewise, C can take values from S, A, and B then S is said to be Inherited Attribute.
Inherited attribute is used by only L-attributed SDT.

16 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(b) Explain any two parameter passing methods.
Or b) Explain the following parameter passing methods. 1. Call-by-value 2.
Call-by-reference 3. Copy-Restore 4. Call-by-Name (WINTER 2021 Q.4-b)

17 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
18 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(c) Write SDD for arithmetic expression and Construct annotated parse tree
for the input expression (4*7+1)*2 .

X

19 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
Q.6 (a) What is activation record?
 An activation record contains all the necessary information required to call a procedure. An
activation record may contain the following units.

Temporaries Stores temporary and intermediate


values of an expression.
Local Data Stores local data of the called procedure.
Machine Status Stores machine status such as Registers,
Program Counter etc., before the
procedure is called.
Control Link Stores the address of activation record of
the caller procedure.
Access Link Stores the information of data which is
outside the local scope.
Actual Parameters Stores actual parameters, i.e., parameters
which are used to send input to the called
procedure.
Return Value Stores return values.

(b) What is Intermediate form of the code? What are the advantages of it?
 During the translation of a source program into the object code for a target machine, a
compiler may generate a middle-level language code, which is known as intermediate code or
intermediate text.
 The complexity of this code lies between the source language code and the object code.
 The intermediate code can be represented in the form of postfix notation, syntax tree, directed
acyclic graph (DAG), three-address code, quadruples, and triples.

The benefits of using an intermediate code are as follows:


Intermediate code is machine independent, which makes it easy to retarget the compiler to
generate code for newer and different processors.
 Intermediate code is nearer to the target machine as compared to the source language so it is
easier to generate the object code.

20 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
The intermediate code allows the machine-independent optimization of the code. Several
specialized techniques are used to optimize the intermediate code by the front end of the
compiler.
Syntax-directed translation implements the intermediate code generation; thus, by augmenting
the parser, it can be folded into the parsing.

(c) Construct syntax directed translation scheme and definition that translate arithmetic
operation from infix to postfix in which an operator appears before its operand as for
example xy- is a positive notation for x-y . Give annotated parse tree for the input 9-5+2

21 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
22 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
Q.7 (a) Explain any two issues in design of code generator.

23 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
24 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(b) Compare: static v/s dynamic Memory Allocation.

25 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(c) Translate the expression –(a+b)*(c+d)+(a+b+c) into

1. Quadruples 2. Triples 3. Indirect triples.

Q.8 (a) What are the limitations of static storage allocation?


It is incompatible with recursive subprograms.
It is not possible to use variables whose size has to be determined at run time.
The static allocation can be done if the size of the data object is known as compile time.
The data structure cannot be created dynamically, in the sense that, the static allocation cannot
manage the allocation of memory at runtime.

26 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(b) Write a short note on Symbol table management.
Symbol table is an important data structure created and maintained by compilers in order to
store information about the occurrence of various entities such as variable names, function
names, objects, classes, interfaces, etc.

Symbol table is used by both the analysis and the synthesis parts of a compiler.

A symbol table may serve the following purposes depending upon the language in hand:

 To store the names of all entities in a structured form at one place.


 To verify if a variable has been declared.
 To implement type checking, by verifying assignments and expressions in the source code
are semantically correct.
To determine the scope of a name (scope resolution).
It is built-in lexical and syntax analysis phases.
The information is collected by the analysis phases of the compiler and is used by the
synthesis phases of the compiler to generate code.
It is used by the compiler to achieve compile-time efficiency.
It is used by various phases of the compiler as follows:-
o Lexical Analysis
o Syntax Analysis
o Semantic Analysis
o Intermediate Code generation
o Code Optimization
o Target Code generation
 Items stored in Symbol table:
o Variable names and constants
o Procedure and function names
o Literal constants and strings
o Compiler generated temporaries
o Labels in source languages

27 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
28 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
(c) Write any two methods of code-optimization in detail.

29 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
30 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA
31 YouTube:
https://youtube.com/channel/UClk43_DjgTzodjJjumyRlwA

You might also like