Professional Documents
Culture Documents
Compilers Theory
Compilers Theory
Compilers Theory
1- The lexical analyzer takes ____ as input and produces a list of ___ as output
a) Machine code, tokens b) Tokens, source code c) Source code, tokens d) Both a and b
5- What is the name of the process that determining whether of tokens can be generated by a
grammar?
a) Analyzing b) Parsing c) Translating d) Recognizing
6- The process of searching for matched tokens is typically described using ___
a) Finite Automata b) Regular Expressions c) Context Free Grammar d) a and b
9- …… or scanning is the process, where the stream of characters making up the source
program is read from left to right and grouped into tokens.
a)Modeling b) Diversion c) Lexical analysis d) All of the mentioned
1
11- Which of the following is/are the phases of compiler?
a) Code generation b) Syntax analyzer c) Lexical analyzer d) All of the mentioned
15- A grammar that produces more than one parse tree for some sentence is called …
a) Ambiguous b) Regular c) Unambiguous d) All of the mentioned
19- In which parsing, the parser constructs the parse tree from the start symbol and
transforms it into the input symbol.
a) Bottom-up parsing b) Top-down parsing c) None of the mentioned d) Both a and b
20- Which part of the compiler highly used the grammar concept?
a) Code optimization b) Code generation c) Parser d) Lexical Analysis
21- Which phase of the compiler checks the grammar of the programming?
a) Code Optimization b) Semantic Analysis c) Code Generation d) Syntax Analysis
2
25- Which phenomenon happens when a grammar has any non-terminal ‘A’ whose
derivation contains ‘A’ itself as the left-most symbol.
a) Left-most derivation b) Left recursion c) Left factoring d) Left parsing
26- In which derivation the leftmost non-terminal symbol is replaced at each step?
a) Left recursion b) Left non-terminal c) Left pushdown d) Leftmost
28- Which of the following is used in various stages or phases of the compiler?
a) Records b) Program c) Symbol Table d) Table
37- The data structure responsible for the management of information about variables and
their attributes is
a) semantic stack b) symbol table c) parser table d) abstract syntax tree
38- Grammar of the programming language is checked at …….. phase
a) semantic analysis b) syntax analysis c) code optimization d) code generation
3
39- Languages that need heap allocation in runtime environment are
a) those that use b) those that use dynamic c) those that support d) those that allow
global variables scoping recursion dynamic data structures
40- Intermediate code generation phase gets input from
a) Lexical analysis b) Syntax Analysis c) Semantic Analysis d) Error handling
41- Annotated syntax tree is the output generated from …………phase
a) syntax analysis b) semantic analysis c) lexical analysis d) code optimizer
42- Annotated syntax tree is the input to…………phase
a) syntax analysis b) semantic analysis c) lexical analysis d) code optimizer
4
53- Context free grammars are used in ………. phase
a) lexical analysis b) syntax analysis c) semantic analysis d) None
54- Syntax tree is the output of ….. phase and it is also an input to …… phase
a) semantic, syntax b) syntax, lexical c) syntax, semantic d) semantic, optimization
55- A compiler needs …….. types of storage
a) two b) three c) four d) five
56- Storage used by a compiler can be classified into the following
a) static, stack and heap b) dynamic, stack and heap c) static, queue and heap d) None
57- ……. and ……. are considered as mechanisms of code optimization
a) constant folding, b) constant folding, c) constant folding, d) a and b
variable folding definite assignment combine operations
5
8- Interpreters generate intermediate code in order not to create a full interpreter for each
machine
a) True b) False
9- Symbol table is a basic phase of any compiler
a) True b) False
10- Annotated syntax tree is the output of syntax analysis phase
a) True b) False
11- Tokens are generated by lexical analysis
a) True b) False
12- Order is not important in concatenation operation
a) True b) False
13- Using empty strings in DFA is legal
a) True b) False
14- When we derive a string from a context free grammar, once using leftmost approach and
once using rightmost approach, then the grammar must be ambiguous
a) True b) False
15- Attribute grammar is used on the syntax tree to generate the annotated syntax tree
a) True b) False
16- Inherited attributes get values from the attribute values of their child nodes
a) True b) False
17- Peephole optimization is a type of code optimization performed on a small part of the
code
a) True b) False
6
1- What are the phases of the compiler?
S → 0A | 1B
A → 0AA | 1S | 1
B → 1BB | 0S | 0
⇒ Since there are more than one leftmost derivation to the same string, the given context free
grammar is ambiguous.
7
locations are known in advance, runtime support package for
memory allocation and de-allocation is not required.
Stack Allocation: Procedure calls and their activations are managed
by means of stack memory allocation. It works in last-in-first-out
(LIFO) method and this allocation strategy is very useful for recursive
procedure calls.
Heap Allocation: Variables local to a procedure are allocated and de-
allocated only at runtime. Heap allocation is used to dynamically
allocate memory to the variables and claim it back when the variables
are no more required. Except statically allocated memory area, both
stack and heap memory can grow and shrink dynamically and
unexpectedly. Therefore, they cannot be provided with a fixed amount
of memory in the system.
S→AB | C
A→aAb | ab
B→cBd | cd
C→aCd | aDd
D→bDc | bc
(i) Show that the string aabbccdd has two leftmost derivations AND draw the two
leftmost parsing trees.
S→AB ⇒ AB S→C ⇒C
A→aAb ⇒ aAbB C→aCd ⇒aCd
A→ab ⇒aabbB C→aDd ⇒ aaDdd
B→cBd⇒aabbcBd D→bDc ⇒ aabDcdd
B→cd ⇒aabbccdd D→bc ⇒aabbccdd
b a a,b
0 0 c
1 c
a c
2
d 2 d
3 d
9
8- Differentiate between synthesized attributes and inherited attributes.
Synthesized attributes are passed upwards in the syntax tree, from the leaves up to the
root. Inherited attributes are, conversely, passed downwards in the syntax tree. Note,
however, that information that is synthesized in one subtree may be inherited by another
subtree or, in a later pass, by the same subtree. An example of this is a symbol table: This
is synthesized by a declaration and inherited by the scope of the declaration.
9- Consider the following C++ code segment: Show the printed output in cases of static and
dynamic binding environments.
x=1
void main() { y=2
int x = 1; x(new) = 3
int y = 2; y=y+x
{ 2 + 3 = 5 output: 5
int x = 3; y= y + x
y += x; = 5 + 1 = 6 Static Binding
cout<<y; x=1
} y=2
y += x; x(New) = x = 3
cout<< y; y=y + x
} = 2 + 3 =5 output:5
y=y+x
= 5 +3 = 8 Dynamic Binding
Draw a symbol table with a linked list for all scopes of the above code
1 YES next 0 YES NULL
x
0 YES NULL
10
10- Differentiate between compilers and interpreters?
A variable or location can be said to be in one of three states at any given point in the
program:
11
0 1 ε
1 3 - 1
2 3 - 2
3 1 2,3 3
0 1
1 3 -
3 1 2,3
2,3 1,3 2,3
1,3 1,3 2,3
0 3 1
1 1
0
2,3
0
0
1
1,3
12
13- Consider the following C++ code:
int a[ ]={10,20,30};
int i=2;
void my_function(int j, int x)
{
int i=0;
j:=j+1;
x:=x+3;
a[2]:=0;
cout<<j; cout<<x; cout<<a[0]; cout<<a[1]; cout<<a[2];
}
void main()
{
cout<< i; cout<<a[0]; cout<<a[1]; cout<<a[2];
my_function (i, a[i]);
cout<< i; cout<<a[0]; cout<<a[1]; cout<<a[2];
}
a- State the output of the program when parameters are passed by value, result, and value-
result.
b- Draw a single symbol table with a linked list for all the scopes for the given code
a[3]
0 YES NULL
13
14- Consider the following grammar:
- Syntax Directed Translation is augmented rules to the grammar that facilitate semantic
analysis.
- SDT involves passing information bottom-up and/or top-down the parse tree in form of
attributes attached to the nodes.
- The general approach to Syntax-Directed Translation is to construct a parse tree or
syntax tree and compute the values of attributes at the nodes of the tree by visiting them
in some order.
- Generalizing, SDT are augmented rules to a CFG that associate 1) set of attributes to
every node of the grammar and 2) set of translation rules to every production rule using
attributes, constants and lexical values.
16- Construct the regular expression of the following FA: solution: e a+b | ab+
14
17- Show the stack when parsing the string id+id*id using the top down parser.
E→E+T|T
T→T*F|F
F → ( E ) | id
15
E
E
- E
E
+
E E
+
id
E - E
id
E E
+
id
id
id
id
16