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

Ministry of Higher Education Academic Year: 2021-2022

Modern Academy For Computer Subject: Compilers Theory


Science and Management Technology Code: CS309 – C402
Department: Computer Science Specialization: 3rd Level(Cr)-4th Year (Terms)
Program: Computer Science Dr.\Lamiaa Hassaan

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

2- Lexical analysis is about breaking a sequence of characters into


a) Tokens b) Lines c) Groups d) Packets

3- The phase Syntax Analysis is modeled on the basis of


a) High level language b) Low level language c) Context free grammar d) Regular grammar

4- Compiler is a program that


a) Accepts a program written in a high level language and produces an object program
b) Appears to execute a source program as if it were machine language
c) Automates the translation of assembly language into machine language
d) Places programs into memory and prepares them for execution

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

7- Which of the following is used for grouping of characters into tokens?


a) Scanner b) Code Generator c) Code Optimizer d) Parser
8- Type checking is normally done during
a) Code optimization b) Syntax directed translation c) Lexical analysis d) Syntax analysis

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

10- Symbol table can be used for


a) Storage allocation b) Checking type c) Suppressing duplication d) All of the mentioned
compatibility of error messages

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

12- Compiler translates the source code to


a) Machine code b) Executable code c) Binary code d) Both A and C

13- Grammar of the programming is checked at ………… phase of compiler.


a) Syntax analysis b) Semantic analysis c) Code generation d) Code optimization

14- ……… is a process of finding a parse tree for a string of tokens.


a) Analyzing b) Recognizing c) Parsing d) Tokenizing

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

16- A top-down parser generates


a)Left-most b) Left-most derivation c) Right-most derivation in d) Right –most derivation
derivation in reverse reverse
17- A bottom-up parser generates
a) Left-most b) Left-most derivation c) Right-most derivation in d) Right –most derivation
derivation in reverse reverse
18- The bottom-up parsing method is also called
a) Shift reduce parsing b) Predictive parsing c) Recursive descent parsing d) None of these

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

22- Keywords are recognized in a compiler during


a) code generation b) data flow analysis c) lexical analysis d) program parsing

23- Leaf nodes in a parse tree indicate


a) terminals b) sub-terminals c) half-terminals d) non-terminals

24- Which of the following is not a characteristic of the compiler?


a) More execution b) Debugging process is c) The execution takes d) it scans the entire
time slow place after the removal of program then transforms
all syntax errors it into machine code

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

27- Which symbol is not related to context-free grammar?


a) End symbol b) Start symbol c) Non-terminal symbol d) Terminal symbol

28- Which of the following is used in various stages or phases of the compiler?
a) Records b) Program c) Symbol Table d) Table

29- .....................is a process of finding a parse tree for a string of tokens.


a) Analyzing b) Recognizing c) Tokenizing d) Parsing

30- A parse tree showing the value of attributes at each node


a) Annotated Parse Tree b) Syntax Tree c) Semantic Tree d) None of the mentioned

31- What does a syntactic analyzer do?


a) maintain symbol b) collect data types c) create paring tree d) None of the mentioned
table
32- One or more parse tree for some sentence, that is ………grammar.
a) unambiguous b) ambiguous c) a or b d) None of the mentioned

33- A compiler indicates the ………error.


a) syntax b) logic c) runtime d) All of the mentioned

34- ………. is not a phase of compiler.


a) Syntax analysis b) Testing c) Lexical analysis d) None of the mentioned

35- Parsing is also called………analysis


a) semantic b) lexical c) syntax d) None of the mentioned

36- Type checking is done during………


a) syntax directed b) lexical analysis c) code optimization d) syntax analysis
translation

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

43- ………… is not a phase of a compiler


a) Scanner b) Symbol table c) Parser d) Code optimizer
44- SDT is an abbreviation of:
a) Syntax Directed b) Semantic Directed c) Syntax Derivation d) Syntax Derivation
Translation Translation Translation Transition
45- The process of using attribute grammar on syntax tree to generate annotated syntax tree
is called
a) Syntax Directed b) Semantic Directed c) Syntax Derivation d) Syntax Derivation
Translation Translation Translation Transition
46- The attributes that get values from the attribute values of their child nodes
a) Inherited attributes b) Child attributes c) Synthesized attributes d) None of the mentioned

47- When the variable is known with certainty to be assigned it is called


a) definitely assigned b) definitely known c) indefinitely assigned d) None of the mentioned

48- Inherited attributes get their attribute values from


a) child nodes b) leaves nodes c) parent nodes d) semantic nodes

49- When a variable may or may not assigned, it is called


a) definitely unassigned b) unknown c) definitely assigned d) None of the mentioned
50- The main disadvantage of creating a symbol table for every scope is
a) memory overhead b) large processing time c) redundant code d) none of the mentioned

51- Static object binding is done based on


a) the structure of the b) the structure of the code c) data types d) none of the mentioned
syntax tree, i.e., braces optimizer
52- A context free grammar is said to be ambiguous when
a) it has a leftmost and a b) it can’t accept a c) it achieves the same string d) None
rightmost derivations for specific string following more than one path using
the same string either leftmost or rightmost derivation

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

58- The purpose of intermediate code generation is to eliminate the need of


a) parsing trees b) a unique compiler for each machine c) context free grammar d) NFA
59- A leftmost parsing tree grows from the ……. side
a) left b) equal sides c) right d) the side can not be determined

60- Attribute grammar is generated by


a) attaching attributes to b) removing extra code c) attaching d) none of the mentioned
each nonterminal of the by the CFG attributes to the
CFG stack storage

1- Loop optimization is necessary to improve cache performance and reduce overheads


a) True b) False
2- Compilers take long time to execute source code
a) True b) False
3- Interpreters do not generate intermediate object code
a) True b) False
4- Lexical analysis receives source code as an input and generates tokens as an output
a) True b) False
5- Synthesized attributes are attributes that get values from the attribute values of their child
nodes
a) True b) False
6- There is only one approach to implement symbol tables
a) True b) False
7- The structure of a symbol table is time consuming when an identifier returns a linked list
of usages of itself
a) True b) False

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?

2- Consider the grammar

S → 0A | 1B
A → 0AA | 1S | 1
B → 1BB | 0S | 0

Derive the string 001101 stating if the grammar is ambiguous or not.


S ⇒ 0A S ⇒ 0A
A⇒ 0AA ⇒00AA A⇒ 0AA ⇒00AA
A⇒1S ⇒ 001SA A⇒1 ⇒ 001A
S⇒ 1B ⇒0011BA A⇒ 1S ⇒0011S
B⇒0 ⇒00110A S⇒0A ⇒00110A
A⇒1 ⇒001101 A⇒1 ⇒001101

⇒ Since there are more than one leftmost derivation to the same string, the given context free
grammar is ambiguous.

3- Show by figure the memory layout allocated by a compiler.

 Static Allocation: In this allocation scheme, the compilation data is


bound to a fixed location in the memory and it does not change when
the program executes. As the memory requirement and storage

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.

4- Propose an optimized version of the following loop with explanation:

for ( ;for (i=0; i < a→length-1 ; i++)


swapswap_elements(a[i],a[ i+1]);

Solution: int x = a→length-1;


for (i=0; i < x ; i++)
swap_elements(a[i],a[ i+1]);
Explanation: instead of calling length-1 as many times as the length of the array, we can
use an integer to store the length of the array (x), and make it the terminating condition
of the loop. So, length-1 will be called and executed only once, which saves memory,
processing and time.
5- Given the following ambiguous context free grammar
S → Ab | aaB
A → a | Aa
B→b

Show that the string aab has two leftmost derivations.


S->aaB S->Ab
B->b aab A->Aa Aab
A->a aab
8
6- Given the following ambiguous context free grammar:

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

7- Draw a finite automaton for each of the following regular expressions:

b*a+(c|d) (a|b)* (c+|d+)

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?

11- What is definite assignment analysis? Explain each type.

- Definite assignment analysis is a data-flow analysis used by compilers to conservatively


ensure that a variable or location is always assigned to before it is used.

A variable or location can be said to be in one of three states at any given point in the
program:

- Definitely assigned: The variable is known with certainty to be assigned.


- Definitely unassigned: The variable is known with certainty to be unassigned.
- Unknown: The variable may be assigned or unassigned.
12- Consider the following NFA:
(a) State the reason(s) of being non-deterministic.
(b) Convert into DFA

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.

By value By result By value-result


a = {10, 20, 30} a = {10, 20, 30} a = {10, 20, 30}
i=2 i=2 i=2
my_fun(2, 30); my_fun(i=2, a[2]=30) my_fun(i=2, a[2],30)
j=2 x=30 j=0 x=0 j=2 x=30
j= 3 x= 33 {10,20,0} j=1 x=3 {10,20,0} j=3 x=33 {10,20,0}
i=1 a[2]=3 i=j=3 x=a[2] = 33
Output: Output: Output:
2 10 20 30 2 10 20 30 2 10 20 30
3 33 10 20 0 1 3 10 20 0 3 33 10 20 0
2 10 20 0 1 10 20 3 3 10 20 33

b- Draw a single symbol table with a linked list for all the scopes for the given code

i 1 YES next 0 YES NULL

a[3]

0 YES NULL
13
14- Consider the following grammar:

statement  if-stmt | other


if-stmt  if ( exp ) statement | if ( exp ) statement else statement
exp  0 | 1
Show the stack when parsing the string: if (0) other else other in a top-down manner.

Stack Input Action


$ $ if (0) other else other Statement → if-stmt
$ if-stmt $ if (0) other else other if-stmt → if ( exp ) statement else statement
$ if ( exp ) statement else statement $ if (0) other else other match
$ ( exp ) statement else statement $ (0) other else other match
$ exp ) statement else statement $ 0) other else other exp → 0
$ 0 ) statement else statement $ 0) other else other match
$ ) statement else statement $ ) other else other match
$ statement else statement $ other else other Statement → other
$ other else statement $ other else other match
$ else statement $ else other match
$ statement $ other Statement → other
$ other $ other match
$ $ accept

15- Explain the purpose of SDT?

- 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

Stack Input Action


E$ id+id*id$ E->E+T
E+T$ id+id*id$ E->T
T+T$ id+id*id$ T->F
F+T$ id+id*id$ F->id
id+T$ id+id*id$ Match id
+T$ +id*id$ Match +
T$ id*id$ T->T*F
T*F$ id*id$ T->F
F*F$ id*id$ F->id
id*F$ id*id$ Match id
*F$ *id$ Match *
F$ id$ F->id
id$ id$ Match id
$ $ Accept

18- Consider the grammar E → E + E | E-E |id


Prove that the grammar is ambiguous using leftmost derivations and parsing trees on the
string id-id+id

E-> E+E E->E-E


E-> E-E E-E+E E-> id id-E
E-> id id-E+E E->E+E id-E+E
E->id id-id+E E->id id-id+E
E->id id-id+id E->id id-id+id

15
E
E

- E
E
+
E E
+

id
E - E

id
E E
+

id

id

id
id

16

You might also like