250+ TOP MCQs on Predictive Top-Down Parsing and Answers

Compilers Multiple Choice Questions & Answers (MCQs) on “Predictive Top-Down Parsing”.

1. What is the grammar for the below equations?

S → C C
C → c C | d

a) LL(1)
b) SLR(1) but not LL(1)
c) LALR(1) but not SLR(1)
d) LR(1) but not LALR(1)

Answer: a
Clarification: Since there is no conflict, the grammar is LL (1) hence a predictive parse table with no conflicts can be constructed.

2. Which of the following statements is false?
a) Unambiguous grammar has both kind of derivations
b) An LL(1) parser is a top-down parser
c) LALR is more powerful than SLR
d) Ambiguous grammar can’t be LR(k)

Answer: a
Clarification: If a grammar has more than one leftmost (or rightmost) derivation the grammar is ambiguous.

3. Given the following expression grammar:

E -> E * F | F + E | F
F -> F - F | id 

Which of the following is true?
a) * has higher precedence than +
b) – has higher precedence than *
c) + and — have same precedence
d) + has higher precedence than *

Answer: b
Clarification: e.g. input is 3*4-5 r

         E
     /  |  
     E   *   F
  |     / | 
  F    F - F
  |    |     |
id (3) id (4) id (5)

First ‘- ‘ is be evaluated then ‘ *’

4. Which one of the following is true at any valid state in shift-reduce parsing?
a) At the bottom we find the prefixes
b) None of the mentioned
c) Stack contains only viable prefixes
d) Stack consists of viable prefixes

Answer: c
Clarification: The prefixes on the stack of a shift-reduce parser are called viable prefixes.

5. In the context of abstract-syntax-tree and control-flow-graph. Which one of the following is true?
a) In both AST and CFG if node N2 be the successor of node N1
b) For any input program, neither AST nor CFG will contain a cycle
c) The max number of successors of a node in an AST and a CFG depends on the input program
d) None of the mentioned

Answer: c
Clarification: Successors depends on input .

6. Match the following.

      List-I                  List-II
A. Lexical analysis       1. Graph coloring
B. Parsing                2. DFA minimization
C. Register allocation    3. Post-order traversal
D. Expression evaluation  4. Production tree

a) A – 2, B – 3, C – 1, D – 4
b) A – 2, B – 1, C – 4, D – 3
c) A – 2, B – 4, C – 1, D – 3
d) A – 2, B – 3, C – 4, D – 1

Answer: c
Clarification: The entire column an items matches the Column B items in a certain way.

7. Which of the following pairs is the most powerful?
a) SLR, LALR
b) Canonical LR ,LALR
c) SLR canonical LR
d) LALR canonical LR

Answer: c
Explanation parser algorithm is simple.

8. Consider the following grammar G.

  S → F ⎪ H
  F → p ⎪ c
  H → d ⎪ c

Which one is true?
S1: All strings generated by G can be parsed with help of LL (1).
S2: All strings generated by G can be parsed with help of LR (1).
a) Only S1
b) Only S2
c) Both S1 & S2
d) None of the mentioned

Answer: d
Clarification: There is ambiguity as the string can be derived in 2 possible ways.
First Leftmost Derivation
S → F
F → c
Second Leftmost Derivation
S → H
H → c.

9. What is the maximum number of reduce moves that can be taken by a bottom-up parser for a grammar with no epsilon- and unit-production to parse a string with n tokens?
a) n/2
b) n-1
c) 2n-1
d) 2^n

Answer: b
Clarification: The moves are n-1.

10. Consider the following two sets of LR (1) items of an LR (1) grammar.

   X -> c.X, c/d
   X -> .cX, c/d
   X -> .d, c/d
   X -> c.X, $
   X -> .cX, $
   X -> .d, $

Which one is false?

1. Cannot be merged since look ahead’s are different.
2. Can be merged but will result in S-R conflict.
3. Can be merged but will result in R-R conflict.
4. Cannot be merged since goto on c will lead to two different sets.

a) 1 only
b) 2 only
c) 1 and 4 only
d) 1, 2, 3 and 4 only

Answer: d
Clarification: All these are valid reasons.

250+ TOP MCQs on Intermediate Code-Generation and Answers

Compilers Questions and Answers for Entrance exams on “Intermediate Code – Generation”.

1. Consider the following two statements:
P: Every regular grammar is LL(1)
Q: Every regular set has LR(1) grammar
Which of the following is TRUE?
a) Both P and Q are true
b) P is true and Q is false
c) P is false and Q is true
d) Both P and Q are false

Answer: a
Clarification: LL(1) parsers can recognize the regular grammars also LL(1) is subset of LR(1) or CLR grammar so it also recognizes regular sets. So both accept regular grammar.

2. In a simplified computer the instructions are:

OP R j, Ri − Performs Rj OP Ri and stores the result in register Ri 
OP m, Ri − Performs val OP Ri abd stores the result in Ri. value denotes the content of memory location m. 
MCVm, Ri −Moves the content off memory loction m to register Ri.
MCVm, Ri, m −Moves the content of register Ri to memory location m.

The computer has only two registers, and OP is either ADD or SUB. Consider the following basic block:

t1 = a + b 
t2 = c + d 
t3 = e − t2
t4 = t 1 − t2

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?
a) 2
b) 3
c) 5
d) 6

Answer: b
Clarification: The operation sequence would be
MOV a, R1
ADD b , R1 {R 1 = t1
MOV c , R2
ADD d, R2 {R 2 = t2
SUB e , R2 {t 3 = e − R 2 = R2
SUB R 1, R2 {R 2 = t4
MOV R 2, t4 {finally in memory
Totally no. of move operation is 3.

3. Which of the following strings is generated by the grammar?

S->bA       S->aB
A->a          B->b
A->aS           B->bS
A->bAA          B->aBB

a) aaaabb
b) aabbbb
c) aabbab
d) abbbba

Answer: c
Clarification: aabbab S ” aB ” aaBB ” aabSB ” aabbAB ” aabbab

4. How many derivation trees are there?

S->bA       S->aB
A->a          B->b
A->aS           B->bS
A->bAA          B->aBB

a) 1
b) 2
c) 3
d) 4

Answer: b
Clarification: For the derivation two trees are possible So due to ambiguity 2 trees are possible.

5. Which of the following describes a handle (as applicable to LR-parsing) appropriately?
a) It is the position in a sentential form where the next shift or reduce operation will occur
b) It is a non-terminal whose production will be used for reduction in the next step
c) 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.
d) 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

Answer: d
Clarification: Handles are the part of sentential form, & they are identified as the right side of any given production which will be used for reduction in the next step.

6. Some code optimizations are carried out on the intermediate code because _______________
a) They enhance the portability of the complier to other target processors
b) Program analysis is name accurate on intermediate code than on machine code
c) The information from data flow analysis cannot otherwise be used for optimization
d) The information from the front end cannot otherwise be used for optimization

Answer: b
Clarification: Code optimizations are carried out on the intermediate code because program analysis is more accurate on intermediate code than on machine code.

7. Which of the following are true?
(i) A programming language option does not permit global variables of any king 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/function
(iii) Recursion in programming languages cannot be implemented with dynamic storage allocation
(iv) Nesting of procedures/functions and recursion require a dynamic heap allocation scheme and cannot be implemented with a stack-based allocation scheme for activation records
(v) 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.
a) (ii) and (v) only
b) (i), (iii) and (iv) only
c) (i), (ii) and (v)
d) (ii), (iii) and (v) only

Answer: a
Clarification: I. Statement is false since global variables are required for recursions with static storage. This is due to unavailability of stack in static storage. II. This is true III. In dynamic allocation heap structure is used, so it is false. IV. False since recursion can be implemented. V. Statement is completely true. So only II & V are true.

8. An LALR(1) parser for a grammar can have shift-reduce (S-R) conflicts if and only if ___________
a) The SLR(1) parser for G has S-R conflicts
b) The LR(1) parser for G has S-R conflicts
c) The LR(0) parser for G has S-R conflicts
d) The LALR(1) parser for G has reduce-reduce conflicts

Answer: b
Clarification: LALR parser is reduced form of CLR or LR(1) parser, LALR parser uses the LR(1) items of CLR parser & of any shift reduce conflicts are there then it is due to LR(1) parser.

9. Which of the following statements are TRUE?
I There exist parsing algorithms for some programming languages hose complex are less than θ(n 3)
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
a) I and II
b) I and IV
c) III and IV
d) I, III and IV

Answer: b
Clarification: I. Statement is true since there are some parsers which take 0 (n log2n) times for parsing. II. Completely false, since there is no use of stack which is required for recursion. III. False IV. True since both types of optimizations are applied.

10. What data structure in a complier is used for managing information about variables and their attributes?
a) Abstract syntax tree
b) Symbol table
c) Semantic stack
d) Parse table

Answer: b
Clarification: Symbol table is used for storing the information about variables and their attributes by compiler.

250+ TOP MCQs on Storage Allocation and Answers

Compilers Multiple Choice Questions & Answers (MCQs) on “Storage Allocation”.

1. Suppose that a bus has 16 data lines and requires 4 cycles of 250 nests each to transfer data. The bandwidth of this bus would be 2 Megabytes/sec. If the cycle time of the bus was reduced to 125 nsecs and the number of cycles required for transfer stayed the same what would the bandwidth of the bus?
a) 1 Megabyte/sec
b) 4 Megabytes/sec
c) 8 Megabytes/sec
d) 2 Megabytes/sec

Answer: d
Clarification: The bandwidth is 2 mb/s.

2. Floating point representation is used to store ____________
a) Boolean values
b) Whole numbers
c) Real integers
d) Integers

Answer: c
Clarification: They are real Integers.

3. SIMD represents an organization that ______________
a) Refers to a computer system capable of processing several programs at the same time
b) Represents organization of single computer containing a control unit, processor unit and a memory unit
c) Includes many processing units under the supervision of a common control unit
d) None of the mentioned

Answer: c
Clarification: SIMD includes processing units under the super vision of a common control.

4. In Reverse Polish notation, expression A*B+C*D is written as __________
a) AB*CD*+
b) A*BCD*+
c) AB*CD+*
d) A*B*CD+

Answer: a
Clarification: RPN is AB*CD*+.

5. In computers, subtraction is generally carried out by ______
a) 9’s complement
b) 10’s complement
c) 1’s complement
d) 2’s complement

Answer: d
Clarification: Subtraction is done by 2’s complement.

6. Assembly language ________
a) Uses alphabetic codes in place of binary numbers used in machine language
b) Is the easiest language to write programs
c) Need not be translated into machine language
d) None of the mentioned

Answer: a
Clarification: Uses binary numbers in machine language.

250+ TOP MCQs on Lexical Analysis and Answers

Compilers Multiple Choice Questions & Answers (MCQs) on “Lexical Analysis”.

1. What constitutes the stages of the compilation process?
a) Feasibility study, system, design, and testing
b) Implementation and documentation
c) Lexical analysis, syntax, Analysis and code generation
d) None of the mentioned

Answer: c
Clarification: As defined in the compilation process.

2. The lexical analyzer takes _________ as input and produces a stream of _______ as output.
a) Source program, tokens
b) Token, source program
c) Either of the two
d) None of the mentioned

Answer: a
Clarification: As per the definition of Lexical Analyser which states that lexical analysis is the process of converting a sequence of characters into tokens.

3. Parsing is also known as ________
a) Lexical Analysis
b) Syntax Analysis
c) Semantic Analysis
d) Code Generation

Answer: b
Clarification: Parsing or syntactic analysis is the process of analysing a string of symbols and conforming to the rules of grammar.

4. A compiler program written in a high level language is called ________
a) Source Program
b) Object Program
c) Machine Language Program
d) None of the mentioned

Answer: a
Clarification: The input that we give in high level language is also known as the source language.

5. System program such a compiler are designed so that they are ________
a) Re-enterable
b) Non-Usable
c) Serially usable
d) None of the mentioned

Answer: a
Clarification: For the convince of the user compilers are made re-enterable.

6. Which of the following is not a feature of compiler?
a) Scan the entire program first and translate into machine code
b) To remove syntax errors
c) Slow for debugging
d) Execution time is more

Answer: d
Clarification: The objective of the compiler is clearly not to increase the execution time of the program.

7. A system program that brings together separately compiled modules of a program into a form language that is suitable for execution.
a) Assembler
b) Linking loader
c) Cross compiler
d) None of the mentioned

Answer: b
Clarification: A loader which brings together the functions of a relocating loader with the ability to combine a number of program segments that have been independently compiled into an executable program.

8. A programmer by mistakes writes a program to multiply two numbers instead of dividing them, how can this error be detected?
a) Compiler
b) Interpreter
c) Compiler or interpreter
d) None of the mentioned

Answer: d
Clarification: This is a logical error that can’t be detected by any compiler or interpreter.

250+ TOP MCQs on Minimization of DFA and Answers

Compilers Multiple Choice Questions & Answers (MCQs) on “Minimization of DFA”.

1. Which one of the following is TRUE?
a) The language L={a^n b^n |n>0 } is regular
b) The language L={a^n |n is prime } is regular
c) The language L={w|w has 3k+1 b’s for some k } is regular
d) None of the mentioned

Answer: c
Clarification: Only for this option we can build a FA.

2. Let L1 = {w ∈ {0,1}∗ | w has at least as many occurrences of (110)’s as (011)’s}.
Let L2 = { ∈ {0,1}∗ | w has at least as many occurrences of (000)’s as (111)’s}.
Which one of the following is TRUE?
a) L1 is regular but not L2
b) L2 is regular
c) L1 and L2 are regular
d) Neither of them are regular

Answer: a
Clarification: L1 is regular let us considering the string 011011011011 . Number of times 011 has occurred is 4 but also its occurrence is 3. Also if the string is ending with 011 we can make a 110 . Now the next string: 110110110110 in this 110 has occurred 4 times and 011 3 times which already satisfy the .

3. The length of the shortest string NOT in the language (over Σ = {a, b}) of the following regular expression is _____________
a*b*(ba)*a*
a) 2
b) 3
c) 4
d) 5

Answer: b
Clarification: baa is not regular so 3.

250+ TOP MCQs on Bottom-Up Parsing and Answers

Compilers Multiple Choice Questions & Answers (MCQs) on “Bottom-Up Parsing”.

1. Inherited attribute is a natural choice in ___________
a) Variable declarations record is maintained
b) L values and R values
c) All of the mentioned
d) None of the mentioned

Answer: a
Clarification: It keeps track of variable.

2. YACC builds up __________
a) SLR parsing table
b) Canonical LR parsing table
c) LALR parsing table
d) None of the mentioned

Answer: c
Clarification: It is a parser generator.

3. In an absolute loading scheme which loader function is accomplished by assembler?
a) Re-allocation
b) Allocation
c) Linking
d) Loading

Answer: a
Clarification: Large number variables onto a small number of CPU register.

4. A parser with the valid prefix property is advantageous because it __________
a) Detects errors
b) None of the mentioned
c) Errors are passed to the text phase
d) All of the mentioned

Answer: c
Clarification: Advantage for a valid prefix property.

5. The action of parsing the source program into proper syntactic classes is called __________
a) Syntax Analysis
b) Lexical Analysis
c) Interpretation analysis
d) General Syntax Analysis

Answer: b
Clarification: Conversion of characters to tokens.

6. Relocating bits used by relocating loader are specified by __________
a) Relocating loader itself
b) Linker
c) Assembler
d) Macro Processor

Answer: b
Clarification: Takes an object files and combines them into a single executable file, library file, or another object file.

7. What is the binary equivalent of the decimal number 368?
a) 10111000
b) 110110000
c) 111010000
d) 111100000

Answer: b
Clarification: 368 binary equivalents is
8=1000
6=0110
3=0011
So 1101101000.

8. AB+(A+B)’ is equivalent to __________
a) A?B
b) A+B
c) (A+B)A
d) (A+B)B

Answer: a
Clarification: It is equivalent to A? B.

9. A top down parser generates __________
a) Rightmost Derivation
b) Right most derivation in reverse
c) Left most derivation
d) Left most derivation in reverse

Answer: c
Clarification: Top-down parsing is a parsing strategy where one first looks at the highest level of the parse tree and works down the parse tree by using the rewriting rules of a formal grammar.

10. Running time of a program depends on __________
a) Addressing mode
b) Order of computations
c) The usage of machine idioms
d) All of the mentioned

Answer: d
Clarification: Run time, runtime or execution time is the time during which a program is running (executing).