0% found this document useful (0 votes)
41 views33 pages

Compiler Advance Sheet - 2023

Co

Uploaded by

Samarth Burkule
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)
41 views33 pages

Compiler Advance Sheet - 2023

Co

Uploaded by

Samarth Burkule
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/ 33

GATE Computer Science & IT

ADVANCED COMPILER

Practice Questions Booklet


ANALYSIS OF COMPILER DESIGN IN GATE PAPER

Years Marks
2015 3
2016 5
2017 6
2018 5
2019 6
2020 4
2021 Set (1) 7
2021 Set (2) 8
COMPILER DESIGN GATE SYLLABUS

 Features of programming language


 Parameter Passing Techniques
 Static Scoping & Dynamic Scoping
 Introduction of compiler
 Phases of Compiler
 Scanning
 Parsing
 LL(1) parser
 SLR Parser
 CLR Parser
 LaLR Parser
 Syntax Directed Translation
 Attributed Grammar (S-attributed, L-attributed, Inherited attributed)
 Annoted Parse tree
 Intermediate Code Generation
 DAG(Directed Acyclic graph)
 Abstract Syntax tree
 Three address code
 Static single assignment
 Control Flow graph
 Runtime Environment
COMPILER DESIGN GATE REFERENCE BOOKS
Introduction to Compiler Design by “ Alfred V. Aho. Ravi Sethi, D. Ulmann”
COMPILER
Q1. [MSQ]
Given the following pseudo code determine the program output based on the
specified scoping rule:
int y; void A(){
int g; int g = y + 3;
void T(){ B();
y += 1; T();
g -= 1; }
} void init (int value){
void F(){ y = - value;
T(); g = value;
if (y > g){ T();
y - = 10; T();
g + = 10; if (y > g){
} A();
} printf(“%d”, y);
void B() { }
int y = g – 2; else{ B();
F(); printf(“%d”, g);
} }
}

Which of the following statement is/are True?


(a) If the language uses static scoping then the output of the program for
function calls init (-1) is -5.
(b) If the language uses static scoping then the output of the program for
function calls init (2) is 9.
(c) If the language uses Dynamic scoping then the output of the program for
function calls init (-1) is 4.
(d) If the language uses Dynamic scoping then the output of the program for
function calls init (2) is 1.

Q2. [MSQ]
ADVANCED COMPILER - 2023 1
Consider the following program:
int a = 5; x = 100, y=1; void main(){
void P(){ int a, y = 10;
int a = 1; a= x+1;
y++; P();
x = x + a; a = x – y;
Q(); print(a, x, y); // S2
print( a, x, y); // S1 Q();
} a = x + y;
void Q(){ print(a, x, y); // S3
int x =2; R();
a = a+10; print(a, x, y); // S4
R(); }
}
void R(){
x = x – 1;
}
Which of the following statement is/are true?
(a) Using Dynamic Scoping, the output of the given program is:
S1:11, 101, 11 S2:90, 101, 11 S3: 112, 101, 11 S4: 112, 100, 11
(b) Using Static Scoping , the output of the given program is:
S1:1, 100, 2 S2:90, 100, 10 S3:109, 99, 10 S4: 109, 98, 10
(c) If the initial value of the global variable „a‟ is 2 instead of 5 then
Using Dynamic Scoping, the output of the given program is:
S1:11, 101, 11 S2:90, 101, 11 S3:112, 101, 10 S4: 109, 98, 10
(d) If the initial value of the global variable „a‟ is 2 instead of 5 then
Using Static Scoping , the output of the given program is:
S1:1, 100, 2 S2:90, 100, 10 S3: 109, 99, 10 S4: 109, 98, 10
Q3. In a language that uses call-by-reference, a function f(x, y) is defined as follows:

ADVANCED COMPILER - 2023 2


x = x + 10;
y = y + 5;
return x + y – 1;
If a variable „a‟ is assigned the integer value k, then f(a, a) returns 37. If k is
perfect square and less than 12, then what is the value of k? ________

Data for next two questions: Given the following C code:


int A = 5; int main (int argc, char * argv [ ] ) {
int * B; c = (int *) calloc (1, size of (int));
int * C; int ** H = &C;
void D (int E) { D (360);
int F; < - - - - /* determine runtime
static int G = 2; areas at this point in time */
B = & G; free (C);
return; C = NULL;
B = & F; return 0;
} }

Q4. [MSQ]
Which of the following pair(s) is/are not correct; (where pair contains the
expression and areas of the C runtime Environment)?
(a) (*H , Static) (b) (C, Heap)
(c) (H, Stack) (d) (G, Stack)
Q5. [MSQ]
Which of the following pair(s) is/are correct; (where pair contains the expression
and areas of the C runtime Environment)?
(a) (**H , Static) (b) (*C, Heap)
(c) (*B, Stack) (d) (E, Stack)
Q6. Consider the set of tokens: {ab, abc, abd, b, bc, bb, ca, cd, da, daa}. What is the

ADVANCED COMPILER - 2023 3


sequence of maximal length tokens generated from the
Input: “abcabbdaadacaabcbb”?
(a) {ab ca bb daa da ca abc bb}.
(b) {ab ca bb daa da caa bc bb}.
(c) {abc ab b daa da ca abc bb}.
(d) Both a & c

Q7. [MSQ]
Consider following simple Lexical rules:
/* regular definitions */
letter [A-Z][a-z]
uletter [A-Z]
p {uletter} + {letter}*
q {letter}{uletter}
r {letter}*
{p} {return(01);}
{q} {return(10);}
{r} {return(11);}
Suppose the input consists of a string of (upper- or lower-case) letters. Select
from the list below, a correct pair (w, i), where the program returns i when the
input is w.
(a) (BcDab, 011011)
(b) (aabBcC, 110110)
(c) (E, 01)
(d) (gHH, 10)

Q8. A predict set for production rule A  can be computed as follows:

ADVANCED COMPILER - 2023 4


 if ε is in First(α) then Predict(A ⟶ α) = First(α) union Follow(A)
 else Predict(A ⟶ α) = First(α)
For the grammar G:
S → X$
X → aXB
X → bAB
A→a
B→b
B→ε
What is the predict set for rule (B→ ε)
(a){$, 𝜆} (b){$, 𝜆, b} (c){$, b} (d) { 𝜆 , b}

Q9. Consider a context-free grammar that has productions S → α | β where α derives


the empty string. Which of the following conditions always prevents the use of
recursive-descent parsing with this grammar:
(a) first(S) ∩ first(β) ∅
(b) follow(S) ∩ first(β) ∅
(c) follow(S) ∪ first(α) ∅
(d) None of the above conditions necessarily prevents the use of recursive-
descent parsing.

Q10. [MSQ]
Which of the following grammar(s) is/are not LL(1)?
(a) S → aAbB |bBcC A → Ba | Cc B → aB | 𝜆 C→ cC| c
(b) S → 𝑎𝑆𝑎 | 𝐴𝐵𝐶 A → aB | 𝜆 B → bB| 𝜆 C→ 𝑐| 𝜆
(c) S → 𝑎𝐵𝑏𝐴𝑐 A → 𝑎𝐵 | 𝑎𝐶 B → bB| 𝜆 C→ 𝑐| 𝜆
(d) S → AB | bC A→ cA| Bc B → SbB| 𝜆 C→ 𝑐| 𝜆

Q11. Consider the following grammar G with starting symbol S:

ADVANCED COMPILER - 2023 5


S → Aa | bAc | dc | bda
A→d
Which of the following statement is true?
(a) G is Both SLR(1) and LALR(1).
(b) G is neither SLR(1) nor LALR(1).
(c) G is LALR(1) but not SLR(1).
(d) G is SLR(1) but not LALR(1).

Q12. [MSQ]
Consider the following grammar G
S Aa | bAc | dc | bda
A a
Which of the following statement(s) is/are true?
(a) G is LR(1)
(b) G is LaLR(1)
(c) G is SLR(1)
(d) Let the number of rows in LR(1) parsing table is X and the number of rows in
LaLR(1) parsing table is Y, then |X - Y | is 0.

Q13. [MSQ]
Which of the following statement(s) is/are False?
(a) Given a predictive parsing table for a grammar, if the table contains no
duplicate entries then a predictive parser can be built for that grammar.
(b) The only way to eliminate a shift – reduce conflict from a grammar is to
rewrite the grammar.
(c) If a program specifies two functions with the same name, the error should be
reported by the compiler‟s lexer.
(d) The abstract syntax tree is the concrete parse tree with redundant
punctuation tokens.
Q14. A grammar with which of the following can *not* be LL (k)?

ADVANCED COMPILER - 2023 6


(a) Right recursive rules.
(b) Left recursive rules.
(c) Deeply nested parentheses.
(d) Rules requiring 5 symbol look ahead.

Q15. [MSQ]
Which of the following statement(s) is/are False?
1. Lexical analysis is recursive in order to handle nested parentheses.
2. Scanners don‟t know anything about the grammar of language.
3. A successful parse means the input is semantically correct.
4. Finite State machine can have an unlimited numbers of states.
5. A regular expression is a type of pattern used to classify lexemes
6. You can change state in a DFA without reading any input character.
(a)2 & 5 only
(b) 1, 3, 4 only
(c) All except 2
(d) 1, 3, 4, 6 only

Q16. Suppose we are adding a new loop “repeat–until” in C language. Semantically


this loop behaves like a while loop, except that the body block is guaranteed to
execute at least once; the loop repeats whenever exp evaluates to false and
terminates otherwise. To add this new feature to your language you would need
to modify which stages of compiler?
(a) Lexical Analysis phase
(b)Syntax Analysis phase
(c) Semantic Analysis phase
(d) Machine code generation

Q17. Which of the following accurately specifies a constraint that a parser puts on the

ADVANCED COMPILER - 2023 7


kind of grammar that it can use in parsing?
(a) A shift-reduce parser requires a grammar to be in Greibach Normal Form.
(b) A CKY parser requires grammar rules that rewrite to only one or two symbols
on their RHS.
(c) A recursive-descent parser requires a grammar to have no left recursive rules.
(d) An LL (1) parser requires a grammar to be in Chomsky Normal Form.

Q18. Consider the grammar G: S  Sa | a Which class of grammar does this


grammar belong to?
(a) LL(1)
(b) LALR(1)
(c) LR(0)
(d) LR(1)

Q19. Consider the following grammar G:


S → aAbBc A → bB(C)| 𝝀 B → 𝒅𝑩| 𝝀 C→e
If X = (First (S) ∪ First (A) ∪ First (c))− (Follow(C) ∪Follow(B)), and Y = (Follow (S)
∪ Follow (A) ∪ First (B))− (First(C) ∩ Follow(B)), then what is the cardinality of the
power set of (X ∪ Y)? ____________

Q20. [MSQ]
Consider the following grammar G:
S → aAb|Bc A → bBC| 𝝀 B → 𝒅𝑩| 𝝀 C→e | f
The grammar G is not LL(1) because
(a) G is ambiguous.
(b) G has left-recursive rules.
(c) G is not left-factored.
(d) In G, First(A) contains empty string and First(A) ∩ Follow(A) ≠ ∅.
Q21. Consider the following grammar G:

ADVANCED COMPILER - 2023 8


S → aAb|Bc A → aBC| 𝝀 B → 𝒅𝑩| 𝝀 C→e | f
Suppose you construct the predictive parsing table M[i, j] for the grammar G;
where „i' represents the variable or non-terminal and „j‟ represents the terminal
symbol or $. In that table some cell contains error entry and some not.
If you select one cell (say M(x, y)) of the table M then what is the probability that
cell M(x, y) doesn‟t contain any error entry? ___________

Data for next two questions: Consider the following grammar G:


S → aAB | Cc A → bAb | a B → Cc C → b | cB
Suppose you construct the predictive parsing table M[i, j] for the grammar G; where „i'
represents the variable or non-terminal and „j‟ represents the terminal symbol or $. In
that table some cell contains error entry and some not.
In M, number of rows and column are same because number of variable in G is 4 and
terminals are {a, b, c} and $. So, we can say that M contains 16 cells. We can
construct a square matrix A with the help of entry of M, defined as below:
0; 𝑖𝑓 𝑀 𝑖, 𝑗 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑠 𝑒𝑟𝑟𝑜𝑟 𝑒𝑛𝑡𝑟𝑦
A[i, j] = ;
1; 𝑖𝑓 𝑀 𝑖, 𝑗 𝑑𝑜𝑒𝑠𝑛′ 𝑡 𝑐𝑜𝑛𝑡𝑎𝑖𝑛 𝑒𝑟𝑟𝑜𝑟 𝑒𝑛𝑡𝑟𝑦
Where 1 ≤ 𝑖 ≤ 4and 1 ≤ 𝑗 ≤ 4.
Q22. Then what is the sum of trace (A) and determinant of A? ________________

Q23. Then what is the rank of matrix (A)? ________________


Data for next six questions: Let S be the set of eight grammars such that S =
𝐺𝑖 | 1 ≤ 𝑖 < 8 , in which some of them are LL(1) and some are not.
G1: S → aSa | a
G2: S → aSb | b
G3: S → aSb | b | 𝜆
G4: S → aSb | a
G5: S → aSb | Ab A → bA | Sc
G6: S → aSb | AB A → aSb | Bb B → aSb | b
G7: S → aAb | aS A → 𝜆 | bA
G8: S → aAb | bS A →𝜆|b
Definition: The size of the grammar G is the number of different production rules in
G. For example: If all the production rules of G are S→ aSb | bS | a then size of G is
3.

Q24. If you select one grammar (say Gk) from S at random then what is the probability

ADVANCED COMPILER - 2023 9


that Gk is LL(1)? _____________
Q25. If you select one grammar (say Gk) from S at random then what is the probability
that the size of Gk is 2? ___________

Q26. If you select one grammar (say Gk) from S at random then what is the probability
that Gk is LL(1) and the size of Gk is 2? ________

Q27. You select one grammar (say Gk) from S at random. If X be a random variable
that denotes the size of Gk, then what is the value of 𝐸 2𝑋 2 − 4𝑋 + 1 ? __________
[Assume E(X) represents the expectation of X]

Q28. If you select one grammar (say Gk) from S at random then what is the probability
That Gk is LL(1) and SLR(1) both? _______

Q29. If you select one grammar (say Gk) from S at random then what is the probability
that Gk is SLR(1) but not LL(1)? _______
Q30. Consider the grammar and LR(0) parsing table for the grammar
(1) E → E * B (2) E → E + B (3) E → B (4) B → 0 (5) B → 1

For the string “1 + 1 * 1”, the maximum number of symbols at a time in the
stack of LR parser is ____________
Q31. [MSQ]

ADVANCED COMPILER - 2023 10


Consider the following Grammar G:
E → E + T|T
T → TF|F
F → F*| a | b
We construct a DFA and Table for SLR parsing to check whether table contains
any conflict (SR or RR) or not. Then which of the following statement is/are true?
(a) DFA contains 9 states.
(b) Parsing Table contains 34 error entries.
(c) Since there doesn‟t exist any conflict (SR/RR) hence G is SLR(1).
(d) For the string “ a * b + a ”, the maximum number of symbols at a time in the
stack of SLR parser is 7.

Q32. Consider the following shift-reduce parse of a string:


|aabaa$ a|abaa$ aa|baa$ aab|aa$ aaS|aa$ aaSa|a$
aS|a$  aSa|$  S
Assuming this parse exercises all productions of the grammar, how many
production rules are there in the grammar for this shift-reduce parser?
(a) S → aSb | a
(b) S → bSa | a
(c) S → aSa | a
(d) S → aSa|

Q33. Consider the following incomplete grammar G:


S ⟶ aAb | bBc A ⟶ aA | a B ⟶ aB | ___
Here one production rule is missing in G i.e. (B ⟶___). If G is SLR then
maximum how many entries is errors entry in parsing table for SLR? __________

Q34. Consider the following grammar G:

ADVANCED COMPILER - 2023 11


S → aSbB | AB | 𝜆
A → aAb | 𝜆
B→ b
Minimum how many production rules should be removed so that G could be LL
(1)?
(a)0 (b) 1 (c) 2 (c) Not possible
Q35. [MSQ]
Which of the following grammar is/are not SLR(1)?
(a) G1 (b) G2 (c) G3 (d)G4
S→L=R S → aSb S → aSa S → Sa
S→R S → SS S→a S → bS
L →* R S→a S→b S→a
L → id S→b S→b
R→L S→∈
Q36. [MSQ]
Which of the following grammar is/are not LALR(1)?
(a) G1 (b) G2 (c) G3 (d)G4
S → AB | aB S → aS S → aSa S → Sa
A→a S → SS S→a S → aS
B→b S→a S→a
S→∈
Q37. [MSQ]
Which of the following grammar is/are CLR(1)?
(a) G1 (b) G2 (c) G3 (d)G4
S→L=R S → aSb S → aSa S → Sa
S→R S → SS S→a S → bS
L →* R S→a S→b S→a
L → id S→b S→b
R→L S→∈

ADVANCED COMPILER - 2023 12


Q38. [MSQ]
Consider the following grammar:
S → x A y |x B y |x A z
A→qs|q
B→q
Which of the following statement is/are true?
(a)G is LL(1) and LALR(1) (b) G is SLR(1) and LR(1)
(c) G is neither SLR(1) nor LR(1) (d) G is neither LL(1) nor LALR(1)
Q39. [MSQ]
Consider the following scenario of stack of activation records:

In what situations would we use the stack pointer instead of the frame pointer?
(a) Allocating space for local variables in the middle of a function, e. g.:
Line# Code Snippet
1 void f (char c, int x)
2 int a = 5;
3 < some code>
4 int b = 7; //here
5 < more code . . .>
6 }
(b) Accessing the arguments passed to the current function
(c) Returning to the caller functions after the stack and frame pointer is restored
to the caller‟s original values.
(d) None of the above

ADVANCED COMPILER - 2023 13


Q40. Some languages perform array bounds checking, which means that array
subscript values (indices) must stay within the specified array bounds. The
checks cannot always be performed at compile time, because array subscript
values often depend on program input data. What kind of error occurs when an
array subscript value is out of bounds at run time?
(a) Lexical error
(b) Syntax error
(c) Static semantic error
(d) Dynamic semantic error

Q41. Which of the last five values (nodes) added in the following tree created using
recursive descent parsing?

(a) x, 2, 3, y, 5
(b) 3, intit, plus, varref, y
(c) Varref, y, Write, Intit, 5
(d) Intit, Plus, Varref, Write, Intit

ADVANCED COMPILER - 2023 14


Q42. Consider the translation scheme
G  int S { G.count = S.count; }
G  float S { G.count = S.count; }
G  { G.count = 0; }
S  A “;” G { S.count = A.count + G.count; }
A  x “=” A1 { A.count = A1.count + 1; }
A  x { A.count = 1; }
What is the value of G.count for root node for the following string? _____
Input: int a = b = c; float d = e = h;
[Assume terminal „x‟ can be any small letter of English alphabets (e.g., a, b, c, d
….z)].
Q43. Consider the following SDT
S  NTS1 { S.val = concat(T.val, S1.val); T.rep = N.rep; }
S  NT { S.val = T.val; }
N  1 { N.rep = 1; }
N  2 { N.rep = 2; }
T  A { T.val = A.val; A.rep = T.rep; }
T  B { T.val = B.val; B.rep = T.rep; }
A  a { A.val = (A.rep == 1) ? "a" : "aa"; }
A  Ta { A.val = (A.rep == 1) ? concat(T.val, "a") :concat(T.val, "aa");T.rep = A.rep;
}
B  b { B.val = (B.rep == 1) ? "b" : "bb"; }
B Tb { B.val = (B.rep == 1)?concat(T.val, "b") :concat(T.val, "bb"); T.rep= B.rep; }
What is the value of S.val attribute for the string “2aba1abba2ab1bbb”? ______
Q44. Assuming that the following SDT is implemented by a bottom-up parser, S → aS
{print "x"}
S → {print "y"} bS
S → {print "w"} a
S → b {print "z"}
What will be the printed output “string” when the input string is “baba” ?
(a) yywz (b) yywx (c) yyxx (d)xyxy

ADVANCED COMPILER - 2023 15


Q45. Consider the following SDD:
PRODUCTION SEMATIC RULES
dnum  num1.num2 dnum.val:= num1.val + num2.val/10num2.count
num  num1 digit num.val:= 10  num1.val + digit.val
num.count := num1.count + 1
num  digit num.val:= digit.val
num.count := 1
digit  0 digit.val := 0
digit  1 digit.val:= 1
digit  2 digit.val := 2
... ...
digit  8 digit.val := 8
digit  9 digit.val := 9
What is the value of attribute dnum.val for the string 12.3456? ________

Q46. Attribute grammars are typically used to…


(a) Handle left-recursion.
(b) Handle language features which context-free grammars cannot.
(c) Prove program correctness.
(d) Compile grammars into efficient tables.

Q47. [MSQ]
Consider the following attributed grammar G:
SXT T. a: = X. b S. b: = T. b
T  X T1 T. b: = T1. b T1. a: = T. a + X. b
T  T. b: = T. a
Xa X. b: = 1
Xb X. b: = 2
Which of the following statement is/are true?
(a) The attribute „a‟ is inherited but „b‟ is synthesized.
(b) The grammar G is L-attributed.
(c) The grammar is S-attributed.
(d) For string “bba”, the value of attributes of root node S i.e., S.b is 5.

ADVANCED COMPILER - 2023 16


Q48. Consider the following SDD:

For the string 1247_8 value attribute for root node evaluates to_________

Q49. [MSQ]
Consider the following SDD to which generates Abstract syntax tree as an
intermediate code.

Assume Create_Node (a, b, c) creates the root node „a‟ with „b‟ as left-child and „c‟
as right-child. If the given SDD generates abstract syntax tree T for the string
6+7*2, then which of the following statement is/are true ?

ADVANCED COMPILER - 2023 17


(a) The depth of the tree T is 3, if root node is at depth 1.
(b) The preorder traversal of T is “+6*72”.
(c) The post order traversal of T is “67+2*”.
(d) The level order traversal of T is “+6*72” .
Q50. Consider the following grammar G

Out of the following four properties which property/properties are not satisfied
by G?
(a) It is unambiguous.
(b) It is LL(1).
(c) It enforces left-associativity of + and *.
(d) It enforces precedence of * over +.
Q51. Arrange the following grammars in ascending order of their power to represent
different syntax and semantics rules of programming languages.
1. Syntax directed translations
2. Context free grammar
3. Context sensitive grammar
4. Attributed grammar
(a)1, 2, 3, 4
(b) 2, 3, 4, 1
(c) 3, 4, 1, 2
(d) 2, 3, 1, 4

ADVANCED COMPILER - 2023 18


Q52. Inherited attributes of syntax-directed definitions (SDDs) are most easily used to:
(a) Determine a course of action at the bottom of the tree based on information
only available above it.
(b) Calculate a value at some point in the tree based on values passed up from
below.
(c) Separate the semantic checking of a piece of code from the parsing of that
piece of code.
(d) Tokenize the input stream.
Q53. Synthesized attributes of syntax-directed definitions (SDD) are most easily used
to:
(a) Determine a course of action at the bottom of the tree based on information
only available above it.
(b) Calculate a value at some point in the tree based on values passed up from
below.
(c) Separate the semantic checking of a piece of code from the parsing of that
piece of code.
(d) Tokenize the input stream.
Q54. Consider the following chunk of intermediate code:
t1 = 60
t2 = id3 + t1
t3 = id2 * t2
id1 = t3
id1, id2, id3 are tokens and t1, t2, t3 are intermediate variables introduced by
the compiler. Which of the following line of code is equivalent to the given
intermediate code?
(a) id1 = id2 * (id3 + t1)
(b) d3 = id2 *(id1 + 60)
(c) id1 = id3 *(id2 + 60)
(d)Id1 = id2 * (id3 + 60)

ADVANCED COMPILER - 2023 19


Q55. [MSQ]
Which of the following grammars is an unambiguous grammar for the language
of arithmetic expressions that is/are correct according to precedence rules
defined by C language?
(a) E  E + T | T, T  T * F | F, F  N | (E), N  0N | 1N | 0 | 1
(b) E  E * F | F, F  F + T | T, T  N | (E), N  0N | 1N | 0 | 1
(c) E  T + T | T, T  F * F | F, F  N | (E), N  0N | 1N | 0 | 1
(d) E  F * F | F, F  T + T | T, T  N | (E), N  0N | 1N | 0 |1

Q56. Minimum number of instructions in the three addresses code corresponding to


following HLL statement______. a = b[i] + c[j]

Q57. How many basic blocks are there in the following 3 address code?
1. read a
2. read b
3. t1 = a + b // line3
4. t2 = a * 2
5. c = t1 + t2
6. if a < c goto 8
7. t2 = a + b
8. b = 25 // line8
9. c = b + c
10. d = a - b
11. if t2 = 0 goto 17
12. d = a + b
13. t1 = b - c
14. c = d - t1
15. if c < d goto 3
16. c = a + b
17. print c // line 17
18. print d

ADVANCED COMPILER - 2023 20


Q58. Consider the following CFG for program P1
Which of the following is/are true about P1?
(a) P1 has one conditional branch instruction.
(b) P1 has no return (ret) instructions.
(c) P1 program has three call instructions.
(d) P1 as a unique entry block.

Q59. After applying which technique of code optimization Code I is transformed into
Code II?

(a) Dead code elimination (b) Strength reduction


(c) Copy propagation (d) Loop fusion

ADVANCED COMPILER - 2023 21


Q60. Consider the following code:
{
x = a + b;
y = a + b + c;
L0: if x > y goto L1
y ++;
L1: goto L2;
x = y + 3;
L2: goto L0
}
Which of the following optimization technique will not make any change in the
above code?
(a) Common sub-expression elimination (b) Loop fusion
(c) Loop invariant code motion (d) Copy propagation
Q61. Consider a new problem of dataflow analysis known as range analysis, in which,
for every variable at every program point, a range [a, b] is decided such that at
the run-time value of variable is guaranteed to be in the interval [a, b].
In the following CFG the range is determined after each statement.

The range analysis problem is the problem of


(a) Forward flow analysis (b)Backward flow analysis
(c) Can be done in either direction (d)Information is not sufficient

ADVANCED COMPILER - 2023 22


For next two questions: Consider the following CFG

Q62. The Number of available expressions at the end of statement 6 is _______.

Q63. How many live variables are there at the beginning of statement 5?

Q64. Consider the following code


a = 42
b=3
c= a + b
d= b*f
e= b+c
Constant folding is code optimization technique which computes all the
expressions involving constant during compile time. If constant folding is applied
on code given above how many expressions are evaluated by compiler?
____________

ADVANCED COMPILER - 2023 23


Q65. Consider the following code
a= 0
b= 1
c= 2
d= a+ b
e= a+c
f = b+ d
And f is live after this. If dead code elimination is applied on this code total
number of statements in the code will be? ___________
Q66. Consider an IR that has the following three address instructions:
 binary statement (e.g., x = y op z),
 copy (e.g., x = y),
 copy positive constants (e.g., x = +1),
 unconditional branch (goto L),
 Conditional branch (if (x) goto L).
Consider the following C code:
z = 10;
while (z != 0) {
b = b * 2;
z = z + 1;
}
y = 20;
The minimum number of IR instructions (don‟t count the labels) required to
encode the above code is?
Q67. Minimum number of variables required to convert the following code into SSA?
__________________

ADVANCED COMPILER - 2023 24


Q68. [MSQ]
Construct an abstract syntax tree (say T) for the following expression in the
postfix notation:
abc*d/+efg-+hi-*/
Which of the following statement is/are true?
(a) If root is at level 1 then number of level in T is 5.
(b) Root node is /.
(c) Last level contains four nodes.
(d) Second last level contains five nodes.
Q69. If in the following CFG Global common sub-expression elimination technique is
applied, then how many common sub-expressions can be eliminated?
_______________

Q70. In the following CFG how many loop invariants can be moved out of the loop?

ADVANCED COMPILER - 2023 25


Q71. Match the dataflow analysis /es (on the left) with the optimizations (on the right)
that require or can benefit from the information provided by the analysis/es.
Data Flow Analysis Code optimization
1. Live-variables i. Commonsub-expression
2. Use-def chains elimination
3. Available expression ii. Dead code elimination
iii. Register-allocation
Which of the following is true?
(a) 1-iii, 1- i, 2 – ii, 2- iii, 3 –i
(b) 1-iii, 2 – ii, 3 –i
(c) 1-iii, 2 – ii, 3 –i
(d) 1-iii, 2 – ii, 2- iii, 3 –i

Q72. Consider a stack-based intermediate code representation in which each


instruction pops zero or values from the operand stack, performs an operation,
and then pushes the result (if any) onto the stack.
push n push the integer constant n
push x push the value stored in local variable x
pop pop a value from the stack
store x pop the top of stack and store the value into local variable x
add pop two operands from the stack and push their sum
sub pop two operands from the stack and push their difference
goto L jump to label L
label L place label L in the instruction stream
If the following HLL statements are converted into IR how many instructions will
be there in IR? ____________
i = 0;
while (true)
i = i + 1;

ADVANCED COMPILER - 2023 26


Q73. If only two registers are available then minimum number of spilling needed to
run the following code, is _____________

Q74. Minimum number of nodes in the DAG for the following three addresses code, is
_____________

ADVANCED COMPILER - 2023 27


Q75. Consider the following code fragment and its possible translation into assembly
language:

What is wrong with this translation?


(a) If A = 0 and B = 1 then assembly code will give different output.
(b) If A = B then assembly code will give different output.
(c) If B is equal to the maximum value of integer then it will go for infinite loop.
(d) Translation is correct.
Data for next two questions: Given the following C code:

#include<stdio.h>
struct s1 {int b; int i; float f;};
struct s2 {int b; int i; float f;};
struct s3 {int b; int i; float f;};
int main(){
struct s1 a;
struct s1 b;
struct s2 c;
struct s2 d;
struct s3 e;
struct s3 f;
__________// L1
return 0;
}

ADVANCED COMPILER - 2023 28


Q76. [MSQ]
Which of the following statement(s) has compile time error?
(a) a = (s1)f; (b) a = * (s1*) &f;
(c) a = b; (d) c = d;

Q77. [MSQ]
Which of the following statement(s) has no error?
(a) (S2) b = d; (b) * (S2*) &b = e;
(c) a. i = = f. i; (d) a = f;

Q78. [MSQ]
Which of the following statement(s) has compile time error?
(a) * (S1*) &e = b = a; (b) a.i = c.i = e.i;
(c) d.f = f. i; (d) b.i = a.f;

Q79. Match each term on the left with the most appropriate phrase on the right:
Left Column Right Column
i. Token 1. Can be found at compile time
ii. Type error 2. Beneficial transformation of IR
iii. Machine-dependent 3. Can produce empty string
optimization 4. Contains regular expression in
iv. Machine – independent productions
optimization 5. Transformation of IR to native code
v. Null-able non – terminal 6. Unite of input text meaningful for
vi. Extended context – free parsing
grammar

(a) i-6, ii-1, iii – 2, iv – 5, v - 3 (b) i-6, ii-1, iii – 5, iv – 2, v - 3


(c) i-1, ii-6, iii – 5, iv – 2, v - 3 (d) i-6, ii-1, iii – 5, iv – 2, v - 3

ADVANCED COMPILER - 2023 29


Q80. [MSQ]
Consider the following two functions (fun1() and fun2()) in C:
int fun1 (int n, int [] a, int [] b){
int i, sum = 0;
for (i = 0; i<=n; i++){
a[i] = a[i] + b[2];
sum = sum + a[i];
}
return sum;
}
Now consider the other function fun2, which tries to hoist the expression b[2]
outside of the loop.
int fun2 (int n, int [] a, int [] b){
int sum = 0, t = b[2];
for (i = 0; i<=n; i++){
a[i] = a[i] + t;
sum = sum + a[i];
}
return sum;
}
Which of the following statement is/are true?
(a) The function fun2 () is optimized version of fun1 ().
(b) For the following set of inputs the function fun1 () and fun2 () have same
behaviour.
int a[] = {0, 1, 2, 3};
int n = 4;
int b = a;
(c) For the following set of inputs the function fun1 () and fun2 () have different
behaviour.
int a[] = {0, 1, 2, 3};
int n = 0;
int b[] = {};
(d) For the following set of inputs the function fun1 () and fun2 () have different
behaviour.
int a[] = {0, 1, 2, 3};
int n = 4;
int b = a;

ADVANCED COMPILER - 2023 30


ADVANCED COMPILER - 2023 31

You might also like