0% found this document useful (0 votes)
15 views

Compiler Design - Sample Questions

The document contains a series of multiple-choice questions (MCQs) and short/long answer questions related to compiler design, parsing techniques, and grammar analysis. Topics covered include lexical analysis, parsing tables, error detection, and syntax-directed translation. It also includes practical exercises on constructing parse trees, eliminating left recursion, and generating three-address code.

Uploaded by

memer28082003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Compiler Design - Sample Questions

The document contains a series of multiple-choice questions (MCQs) and short/long answer questions related to compiler design, parsing techniques, and grammar analysis. Topics covered include lexical analysis, parsing tables, error detection, and syntax-directed translation. It also includes practical exercises on constructing parse trees, eliminating left recursion, and generating three-address code.

Uploaded by

memer28082003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MCQ Type Questions:

1. From X → YZ production rule, FIRST(X) is


a) FIRST(Y)
b) FIRST(Y) ∪ FIRST(Z)
c) FIRST(Y) ∪ FIRST(Z) if FIRST(X) contains ε else FIRST(Y)
d) FIRST(Z)

2. Which of the following error will not be detected by the compiler?


a) Lexical error
b) Syntactic error
c) Semantic error
d) Logical error

3. firstpos of concatenate node with left child c1 and right child c2 is


a) firstpos(c1) ∪ firstpos(c2)
b) firstpos(c1) ∩ firstpos(c2)
c) if (nullable(c1)) firstpos(c1) ∪ firstpos(c2) else firstpos(c1)
d) if (nullable(c2)) firstpos(c1) ∪ firstpos(c2) else firstpos(c1)

4. lastpos of concatenate node with left child c1 and right child c2 is


a) lastpos(c1) ∪ lastpos(c2)
b) lastpos(c1) ∩ lastpos(c2)
c) if (nullable(c1)) lastpos(c1) ∪ lastpos(c2) else lastpos(c2)
d) if (nullable(c2)) lastpos(c1) ∪ lastpos(c2) else lastpos(c2)

5. A given grammar is not LL(1) if the parsing table of a grammar may contain
a) any blank field
b) duplicate entry of same production
c) more than one production rule in a cell
d) any field mentioned as error

6. In a programming language, an identifier is permitted to be a letter followed by any number of letter or


digits. If L and D denote the set of letters and digits respectively, then which of the following expressions
defines an identifier?
a) (L | D) +
b) L. (L | D)*
c) (L . D)*
d) L (L . D)*

7. Which of the following software tool is a lexical analyzer generator?


a) Lex
b) Yacc
c) Both Lex and Yacc
d) Neither Lex nor Yacc

1
8. Which of the following software tool is a LALR Parser generator?
a) Lex
b) Yacc
c) Both Lex and Yacc
d) Neither Lex nor Yacc

9. YACC builds up
a) SLR Parsing Table
b) LALR Parsing Table
c) CLR Parsing Table
d) LL Parsing Table

10. Which of the following derivations does a top-down parser use while parsing an input string?
a) Leftmost derivation
b) Leftmost derivation in reverse
c) Rightmost derivation,
d) Rightmost derivation in reverse

11. Which of the following derivations does a bottom-up parser use while parsing an input string?
a) Leftmost derivation
b) Leftmost derivation in reverse
c) Rightmost derivation,
d) Rightmost derivation in reverse

12. Which of the following statements is false?


a) An unambiguous grammar has same left most and right most derivation
b) An LL(1) parser is a top-down parser
c) An LR(1) parser is a bottom-up parser
d) An ambiguous grammar can never be LR(k) for any k

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


a) Recursive descent parser
b) Operator precedence parser
c) An LR(k) parser
d) An LALR(k) parser

14. Which of the following grammar rules violate the requirements of an operator grammar? P, Q, R are non-
terminals and r, s, t are terminals.
1. P → Q R
2. P → Q s R
3. P → ε
4. P → Q t R r

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

2
15. Parse tree is generated in the phase of
a) Syntax Analysis
b) Semantic Analysis
c) Code Optimization
d) Intermediate Code Generation

16. Annotated Parse tree is generated in the phase of


a) Syntax Analysis
b) Semantic Analysis
c) Code Optimization
d) Intermediate Code Generation

17. Type checking is normally done during


a) Syntax Analysis
b) Semantic Analysis
c) Lexical Analysis
d) Intermediate Code Generation

18. Consider the given below SDT:

P1: S → MN {S.val= M.val + N.val}


P2: M → PQ {M.val = P.val * Q.val and Q.val =P.val}

Select the correct option:

a) Both P1 and P2 are S attributed


b) P1 is not S attributed and P2 is L-attributed
c) P1 is L attributed but P2 is not L-attributed
d) Both P1 and P2 are L attributed

19. Consider the given below SDT:

P1: S → MN {S.val= M.val + N.val}


P2: M → PQ {M.val = P.val * Q.val and P.val =Q.val}

Select the correct option:

a) Both P1 and P2 are S attributed


b) P1 is S attributed and P2 is L-attributed
c) P1 is L attributed but P2 is not L-attributed
d) Both P1 and P2 are L attributed

20. Three Address code involves


a) At least 3 addresses
b) At most 3 addresses
c) Exactly 3 addresses
d) Ternary operator

3
21. Which data structure is mainly used in shift-reduce parsing?
a) stack
b) queue
c) array
d) pointer

22. Consider the following Grammar:

E→E+n
E→E*n
E→n

For a sentence n + n × n, the handles in the right-sentential form of the reduction are

a) n, E+n and E+n*n


b) n, E+n and E+E*n
c) n, n+n and n+n*n
d) n, E+n and E*n

23. Consider the following Grammar:

S → ACB | CbB | Ba | BD
A → ae | BC
B→b|ε
C→c|ε
D→d

The FIRST(S) is
a) { a,b,c,d }
b) { a,b,c,d,ε }
c) { a,b,c,ε}
d) { a,b,d,ε }

24. A grammar that produces more than one parse tree for some sentence is called as
a) Ambiguous
b) Unambiguous
c) Regular
d) All of these

4
Short / Long Answer Type Questions:

1. Define Compiler, Interpreter, Preprocessor, Assembler and Linker.


2. What are the analysis phase and synthesis phase of a compiler?
3. Write down the output of each phase for the expression
a: = b + c *70
[Assume a, b and c are real numbers]
4. Express the output of each phase for the expression
a = b + c *d+50
[Assume a, b c and d are real numbers]
5. How does a lexical analyzer help in the process of compilation? Describe with a diagram.
6. Consider the following conditional statement:
void main()
{
int x;
x = 3;
}
Determine the number of tokens and prepare a chart of them from the above statement.
7. Consider the following conditional statement:
if(z>4) then y= z+1 else y=10;
From the above statements how many tokens are possible and what are they?
8. Construct an ε-NFA from the following regular expression
L = ( 0 | 1 )*011
Construct the equivalent DFA of that ε-NFA.
9. Construct a DFA (directly without generating NFA) from the following regular expression
L = ( 0 | 1 )*011
using algorithmic approach.
10.Consider the Grammar
E → E+E | E*E | id
Show that the Grammar is ambiguous.
11. Consider the following Grammar G:
S → aSbS | bSaS | ε
Show that the Grammar G is ambiguous with the help of the string “abab”
12.What is a handle?
13.Consider the Grammar
E → E+E | E*E | id
Find the handles of the right sentential forms of reduction for the string id*id+id
14. Consider the Grammar
E → E+E | E*E | a | b | c
Deduce the handles of the right sentential forms of reduction for the string a+b*c
15. Eliminate left-recursion from the following grammar:
E→E+T|T
T→T*F|F
F → (E) | id
16. Eliminate left-recursion from the following grammar:
S → Yb | a
Y → Yc | Sd | e

5
17. Consider the following grammar G:
S → iEtS | iEtSeS | a
E→b
Find an equivalent left-factored grammar of the above grammar.
18. Consider the following grammar G:
A → aAB | aBc | aAc
Find an equivalent left-factored grammar of the above grammar.
19. Construct a LL(1) Parsing Table for the following grammar:
E → E+T | T
T → T*F | F
F → (E) | id
Explain the LL(1) Parser’s action by describing the moves it would make on an input id*id+id
20. Prepare a LL(1) Parsing Table for the following grammar:
E → E+T | T
T → T*F | F
F → id
Explain the LL(1) Parser’s action by describing the moves it would make on an input id+id*id$
21. What is operator grammar? Explain with an example.
22. Compute the collection of LR(0) item the following grammar.
E → E+T | T
T → T*F | F
F → id
Construct the SLR parsing table using the SLR algorithm
23. Compute the collection of LR(0) item sets for the following grammar.
S → CC
C → cC | d
Construct the SLR parsing table using the SLR algorithm
24. Deduce the collection or set of LR(1) items for the following grammar.
S→ AA
A → aA | b
Prepare the CLR parsing table using the CLR algorithm.
25. What do you mean by Inherited Attribute and Synthesized Attribute for Syntax Directed Translation?
Explain with examples.
26. What do you mean by S-attributed and L-attributed Syntax Directed Translation? Explain with
examples.
27. Explain dependency graph with suitable example.
28. Generate an annotated parse tree for the string “3+5−2” using the grammar:
E → E+T | E−T | T
T → 0 | 1 | 2 | 3| 4 | 5| 6 | 7 | 8 | 9
29. What is syntax tree? Translate the expression a*(b+c/d) into syntax tree.
30. Translate the expression
a=–b*(c+d/b)−(e*f)
into
i) 3-address code ii) Quadruple iii) Triple iv) Indirect Triple
31. Translate the following expression
x=(a+b)*(c+d)+(a+b+c)
into
i) 3-address code ii) Quadruple iii) Triple iv) Indirect Triple

6
32. Construct the DAG for the following basic block
d:= b*c
e:= a+b
b:= b*c
a:= e-d
33. Sketch out the DAG for the following basic block
x=a+b*−c+b*-c
34. Write short notes on Context Free Grammar.
35. Write short notes on Ambiguous Grammar.
36. Write short notes on Lex.
37. Write short notes on Yacc
38. Write short notes on Parse Tree.
39. Write short notes on Annotated Parse Tree.
40. Write short notes on Peephole Optimization.

You might also like