Test 1 Compiler Design | Computer Science

GATE Previous year Topic Wise Questions and Answers | Compiler Design

29 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

Which of the following suffices to convert an arbitrary CFG to an LL(1) grammar?

  1. Removing left recursion alone
  2. Factoring the grammar alone
  3. Removing left recursion and factoring the grammar
  4. None of the above
Question 2 Multiple Choice (Single Answer)

In a bottom-up evaluation of a syntax directed definition, inherited attributes can

  1. always be evaluated
  2. be evaluated only if the definition is L-attributed
  3. be evaluated only if the definition has synthesized attributes
  4. never be evaluated
Question 3 Multiple Choice (Single Answer)

Which of the following statements is FALSE?

  1. In statically typed languages, each variable in a program has a fixed type
  2. In un-typed languages, values do not have any types
  3. In dynamically typed languages, variables have no types
  4. In all statically typed languages, each variable in a program is associated with values of only a single type during the execution of the program
Question 4 Multiple Choice (Single Answer)

Consider the grammar shown below.
S $\rightarrow$ C C
C $\rightarrow$ c C | d
This grammar is

  1. LL(1)
  2. SLR(1) but not LL(1)
  3. LALR(1) but not SLR(1)
  4. LR(l) but not LALR(1)
Question 5 Multiple Choice (Single Answer)

Assume that the SLR parser for a grammar G has n1 states and the LALR parser for that grammar has n2 states. Which of the following statements about the relationship between n1 and n2 is true?

  1. n1 is necessarily less than n2.
  2. n1 is necessarily equal to n2.
  3. n1 is necessarily greater than n2.
  4. none of these
Question 6 Multiple Choice (Single Answer)

The following program fragment is written in a programming language that allows global
variables and does not allow nested declarations of functions.
global int i = 100, j = 5;
void P(x) {
int i = 10;
print(x + 10);
i = 200;
j = 20;
print (x);
}
main() {P(i + j);}

If the programming language uses static scoping and call by need parameter passing mechanism, the values printed by the above program are

  1. 115, 220
  2. 25, 220
  3. 25, 15
  4. 115, 105
Question 7 Multiple Choice (Single Answer)

The following program fragment is written in a programming language that allows global
variables and does not allow nested declarations of functions.
global int i = 100, j = 5;
void P(x) {
int i = 10;
print(x + 10);
i = 200;
j = 20;
print (x);
}
main() {P(i + j);}

If the programming language uses dynamic scoping and call by name parameter passing mechanism, the values printed by the above program are

  1. 115, 220
  2. 25, 220
  3. 25, 15
  4. 115, 105
Question 8 Multiple Choice (Single Answer)

Consider the translation scheme shown below.
S $\rightarrow$T R
R $\rightarrow$ + T {print('+');} R|$\epsilon$
T $\rightarrow$ num {print (num.val);}
Here num is a token that represents an integer and num.val represents the corresponding integer value.
For an input string '9 + 5 + 2', this translation scheme will print

  1. 9 + 5 + 2
  2. 9 5 + 2 +
  3. 9 5 2 + +
  4. + + 9 5 2
Question 9 Multiple Choice (Single Answer)

Which of the following is NOT an advantage of using shared, dynamically linked libraries as opposed to using statically linked libraries?

  1. Smaller sizes of executable files
  2. Lesser overall page fault rate in the system
  3. Faster program startup
  4. Existing programs need not be re-linked to take advantage of newer versions of libraries
Question 10 Multiple Choice (Single Answer)

Consider the following translation scheme.
S$\rightarrow$ER
R$\rightarrow$E {print (''); R}$\epsilon$
E $\rightarrow$F + E {print ('+');} F
F$\rightarrow$ (S) | id {print (id.value);}
Here id is a token that represents an integer and id.value represents the corresponding integer value.
For an input '2 * 3 + 4', this translation scheme prints

  1. 2 * 3 + 4
  2. 2 * +3 4
  3. 2 3 * 4 +
  4. 2 3 4+*
Question 11 Multiple Choice (Single Answer)

Consider the following grammar.
S $\rightarrow$S * E
S $\rightarrow$E
E $\rightarrow$F + E
E $\rightarrow$F
F $\rightarrow$id
Consider the following LR (0) items corresponding to the grammar above.
(i) S $\rightarrow$ S *.E
(ii) E $\rightarrow$ F. + E
(iii) E $\rightarrow$ F + .E
Given the items above, which two of them will appear in the same set in the canonical sets-of-items for the grammar?

  1. (i) and (ii)
  2. (ii) and (iii)
  3. (i) and (iii)
  4. None of the above
Question 12 Multiple Choice (Single Answer)

In the correct grammar above, what is the length of the derivation (number of steps starring from S) to generate the string al bm with l $\ne$ m?

  1. max (l, m) + 2
  2. l + m + 2
  3. l + m + 3
  4. max (l, m) + 3
Question 13 Multiple Choice (Single Answer)

Which one of the following is a top-down parser?

  1. Recursive descent parser.
  2. Operator precedence parser
  3. An LR(k) parser.
  4. An LALR(k) parser.
Question 14 Multiple Choice (Single Answer)

Some code optimizations are carried out on the intermediate code because

  1. They enhance the portability of the compiler to other target processors
  2. Program analysis is more accurate on intermediate code than on machine code
  3. The information from dataflow analysis cannot otherwise be used for optimization
  4. The information from the front end cannot otherwise be used for optimization
Question 15 Multiple Choice (Single Answer)

Consider the following grammar:
S $\rightarrow$FR
R$\rightarrow$* S|$\epsilon$
F$\rightarrow$id
In the predictive parser table, M, of the grammar the entries
M [S, id] and M [R,$] respectively.

  1. {S $\rightarrow$ FR} and {R $\rightarrow$ $\epsilon$ }
  2. {S $\rightarrow$FR} and { }
  3. {S $\rightarrow$FR} and {R $\rightarrow$ *S}
  4. {F $\rightarrow$ id} and {R $\rightarrow$ $\epsilon$ }
Question 16 Multiple Choice (Single Answer)

Consider the following two statements:
P: Every regular grammar is LL(1)
Q: Every regular set has a LR(1) grammar
Which of the following is TRUE?

  1. Both P and Q are true
  2. P is true and Q is false
  3. P is false and Q is true
  4. Both P and Q are false
Question 17 Multiple Choice (Single Answer)

Consider the syntax directed definition shown below,
S $\rightarrow$ id : = E {gen (id.place = E.place;);}
E $\rightarrow$ E1 + E2 {t = newtemp( );
gen(t = E1.place + E2.place;);
E.place = t;}
E $\rightarrow$ id {E.place = id.place;}

Here, gen is a function that generates the output code, and newtemp is a function that returns the name of
a new temporary variable on every call.
Assume that ti's are the temporary variable names generated by newtemp. For the statement 'X : = Y + Z',
the 3-address code sequence generated by this definition is

  1. X = Y + Z
  2. t1 = Y + Z; X = t1
  3. t1 = Y; t2 = t1 + Z; X = t2
  4. t1 = Y; t2 = Z; t3 = t1 + t2; X = t3
Question 18 Multiple Choice (Single Answer)

An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if and only if

  1. The SLR(1) parser for G has S-R conflicts
  2. The LR(1) parser for G has S-R conflicts
  3. The LR(0) parser for G has S-R conflicts
  4. The LALR(1) parser for G has reduce-reduce conflicts
Question 19 Multiple Choice (Single Answer)

Consider the grammar with non-terminals N = { S, C, S1), terminals T = {a, b, i, t, e}, with S as the start symbol, and the following set of rules:

S $\rightarrow$iCtSS1 | a
S1 $\rightarrow$ eS |$\epsilon$
C $\rightarrow$ b

The grammar is NOT LL(1) because:

  1. it is left recursive
  2. it is right recursive
  3. it is ambiguous
  4. it is not context-free.
Question 20 Multiple Choice (Single Answer)

Which of the following are true?
I. A programming language which does not permit global variables of any kind and has no nesting of
procedures/functions, but permits recursion can be implemented with static storage allocation
II. Multi-level access link (or display) arrangement is needed to arrange activation records only if the
programming language being implemented has nesting of procedures/functions
III. Recursion in programming languages cannot be implemented with dynamic storage allocation
IV. Nesting procedures/functions and recursion require a dynamic heap allocation scheme and cannot
be implemented with a stack-based allocation scheme for activation records
V. Programming languages which permit a function to return a function as its result cannot be
implemented with a stack-based storage allocation scheme for activation records

  1. II and V only
  2. I, III and IV only
  3. I, II and V only
  4. II, III and V only
Question 21 Multiple Choice (Single Answer)

Consider the following class definitions in a hypothetical Object Oriented language that supports inheritance and uses dynamic binding. The language should not be assumed to be either Java or C++, though the syntax is similar.

Class P {
  Class Q subclass of P {
    void f(int i) {
      void f(int i) {
        print(i);
        print(2 * i);
      }
    }
  }
}

Now consider the following program fragment:
Px = new Q()
Qy = new Q();
Pz = new Q();
x.f(1); ((P)y).f(1); z.f(1);
Here ((P)y) denotes a typecast of y to P. The output produced by executing the above program fragment
will be

  1. 1 2 1
  2. 2 1 1
  3. 2 1 2
  4. 2 2 2
Question 22 Multiple Choice (Single Answer)

Consider the CFG with {S, A, B} as the non-terminal alphabet, {a, b} as the terminal alphabet, S as the start symbol and the following set of production rules:

S$\rightarrow$ a B S $\rightarrow$bA
B $\rightarrow$b A$\rightarrow$ a
B $\rightarrow$bS A $\rightarrow$aS
B $\rightarrow$aBB S$\rightarrow$ bAA

Which of the following strings is generated by the grammar?

  1. aaaabb
  2. aabbbb
  3. aabbab
  4. abbbba
Question 23 Multiple Choice (Single Answer)

Which of the following describes a handle (as applicable to LR-parsing) appropriately?

  1. It is the position in a sentential form where the next shift or reduce operation will occur
  2. It is non-terminal whose production will be used for reduction in the next step
  3. It is a production that may be used for reduction in a future step along with a position in the sentential form where the next shift or reduce operation will occur
  4. It is the production p that will be used for reduction in the next step along with a position in the sentential form where the right hand side of the production may be found
Question 24 Multiple Choice (Single Answer)

Which of the following statements are TRUE?
I. There exist parsing algorithms for some programming languages whose complexities are less than $\theta$(n3).
II. A programming language which allows recursion can be
implemented with static storage allocation.
III. No L-attributed definition can be evaluated in the framework of bottom-up parsing.
IV. Code improving transformations can be performed at both source language and intermediate code level.

  1. I and II
  2. I and IV
  3. III and IV
  4. I, III and IV
Question 25 Multiple Choice (Single Answer)

Which one of the following grammars generates the language L = {ai bj i ¹ j}?

Question 26 Multiple Choice (Single Answer)

In a simplified computer the instructions are:
OP Rj , Ri - Performs Rj OP Ri and stores the result in register . Ri
OP m, Ri - Performs val i OP Ri and stores the result in Ri. val denotes the content of memory location m.
MOV m, Ri - Moves the content of memory location m to register Ri
MOV Ri , m - Moves the content of register Ri to memory location m.
The computer has only to registers, and OP is either ADD or SUB. Consider the following basic block:
t1 = a + b
t2 = c + d
t3 = e - t2
t4 = t1 - b
Assume that all operands are initially in memory. The final value of the computation should be in memory. What is the minimum number of MOV instructions in the code generated for this basic block?

  1. 2
  2. 3
  3. 5
  4. 6
Question 27 Multiple Choice (Single Answer)

Consider the following C code segment.

for (i - 0, i < n; i++) {
  for (j = 0; j < n; j++) {
    if (i % 2) {
      x += (4 * j + 5 * i);
      y += (7 + 4 * j);
    }
  }
}

Which one of the following is false?

  1. The code contains loop invariant computation
  2. There is scope of common sub-expression elimination in this code
  3. There is scope of strength reduction in this code
  4. There is scope of dead code elimination in this code
Question 28 Multiple Choice (Single Answer)

Consider the CFG with {S, A, B} as the non-terminal alphabet, {a, b} as the terminal alphabet, S as the start symbol and the following set of production rules:

S $\rightarrow$ aB S $\rightarrow$ bA
B $\rightarrow$ b A $\rightarrow$ a
B $\rightarrow$ bS A $\rightarrow$ aS
B $\rightarrow$ aBB S $\rightarrow$ bAA

How many derivation trees are there?

  1. 1
  2. 2
  3. 3
  4. 4
Question 29 Multiple Choice (Single Answer)

Consider the grammar shown below
S $\rightarrow$i E t S S' | a
S' $\rightarrow$ e S |$\epsilon$
E $\rightarrow$ b
In the predictive parse table, M, of this grammar, the entries M[S' , e] and
M[ S' ,$] respectively area

  1. {S' $\rightarrow$ e S} and{ S'$\rightarrow$$\epsilon$}
  2. { S' $\rightarrow$e S}and { }
  3. {S'$\rightarrow$ $\epsilon$} and { S'$\rightarrow$$\epsilon$}
  4. { S' $\rightarrow$e S, S'$\rightarrow$$\epsilon$} and { S'$\rightarrow$ $\epsilon$}