Compiler Design
Compiler Design
v=7ICf62fp_4I&t=6099s
1. Structure of a Compiler Which of the following is the first phase of a compiler? a)
Semantic Analysis b) Code Generation c) Lexical Analysis d) Syntax Analysis
Answer: c) Lexical Analysis
2. The output of the lexical analyzer is a stream of: a) Parse trees b) Tokens c)
Three-address code d) Machine code
Answer: b) Tokens
3. Which phase of a compiler groups characters into lexemes and then into tokens? a)
Syntax Analysis b) Semantic Analysis c) Lexical Analysis d) Intermediate Code
Generation
Answer: c) Lexical Analysis
4. The phase of a compiler that checks for grammatical errors in the source code is: a)
Lexical Analysis b) Syntax Analysis c) Semantic Analysis d) Code Optimization
Answer: b) Syntax Analysis
5. Which phase of a compiler generates intermediate code? a) Lexical Analysis b)
Syntax Analysis c) Semantic Analysis d) Intermediate Code Generation
Answer: d) Intermediate Code Generation
6. The symbol table is used by: a) Only Lexical Analyzer b) Only Syntax Analyzer c)
Only Code Generator d) All phases of the compiler
Answer: d) All phases of the compiler
7. Which phase of a compiler attempts to improve the intermediate code to generate
better target code? a) Lexical Analysis b) Syntax Analysis c) Code Optimization d)
Code Generation
Answer: c) Code Optimization
8. The final phase of a compiler is: a) Intermediate Code Generation b) Code
Optimization c) Code Generation d) Semantic Analysis
Answer: c) Code Generation
9. Error detection and reporting typically occur in: a) Only Lexical Analysis b) Only
Syntax Analysis c) Only Semantic Analysis d) All phases of the compiler
Answer: d) All phases of the compiler
10.A front end of a compiler typically includes: a) Lexical analysis, syntax analysis,
semantic analysis, and intermediate code generation. b) Code optimization and code
generation. c) Symbol table management and error handling only. d) Only lexical
analysis and syntax analysis.
Answer: a) Lexical analysis, syntax analysis, semantic analysis, and
intermediate code generation.
11.Lexical Analysis Lexical analysis is also known as: a) Parsing b) Scanning c)
Semantic analysis d) Code generation
Answer: b) Scanning
12.Which of the following is a tool used to generate lexical analyzers? a) Yacc b) Lex c)
Bison d) ANTLR (can be used for both)
Answer: b) Lex
13.Regular expressions are used to specify: a) Syntax of the language b) Lexical
structure of tokens c) Semantic rules d) Machine code instructions
Answer: b) Lexical structure of tokens
14.The primary task of a lexical analyzer is to: a) Build a parse tree. b) Check for type
compatibility. c) Recognize tokens based on regular expressions. d) Generate
machine code.
Answer: c) Recognize tokens based on regular expressions.
15.What happens when the lexical analyzer encounters an unrecognizable sequence of
characters? a) It generates a syntax error. b) It generates a semantic error. c) It
generates a lexical error. d) It skips the sequence.
Answer: c) It generates a lexical error.
16.Which of the following is NOT a typical token category? a) Keywords b) Identifiers c)
Operators d) Parse Trees
Answer: d) Parse Trees
17.A finite automaton is commonly used to implement a: a) Parser b) Lexical Analyzer c)
Code Optimizer d) Symbol Table
Answer: b) Lexical Analyzer
18.What is the role of the buffer in lexical analysis? a) To store the entire source code. b)
To read a portion of the input stream efficiently. c) To store generated tokens. d) To
hold the symbol table.
Answer: b) To read a portion of the input stream efficiently.
19.The "lookahead" in lexical analysis refers to: a) Checking the next token before
processing the current one. b) Reading characters beyond the current lexeme to
decide its type. c) Analyzing the entire program before starting tokenization. d)
Predicting the next keyword.
Answer: b) Reading characters beyond the current lexeme to decide its type.
20.A "lexeme" is: a) A category of tokens. b) A sequence of characters in the source
program that matches the pattern for a token. c) The internal representation of a
token. d) A syntax error.
Answer: b) A sequence of characters in the source program that matches the
pattern for a token.
21.Syntax Analysis Syntax analysis is also known as: a) Scanning b) Parsing c)
Semantic Analysis d) Code Generation
Answer: b) Parsing
22.The output of the syntax analyzer is typically a: a) Stream of tokens b) Symbol table
c) Parse tree or syntax tree d) Intermediate code
Answer: c) Parse tree or syntax tree
23.Which of the following parsers reads the input from left to right and produces a
rightmost derivation in reverse? a) LL parser b) Top-down parser c) LR parser d)
Predictive parser
Answer: c) LR parser
24.A grammar that produces more than one parse tree for the same input string is
called: a) Regular grammar b) Context-free grammar c) Ambiguous grammar d)
Unambiguous grammar
Answer: c) Ambiguous grammar
25.Shift-Reduce Parsing Shift-reduce parsing is a type of: a) Top-down parsing b)
Bottom-up parsing c) Predictive parsing d) Recursive descent parsing
Answer: b) Bottom-up parsing
26.The two main actions in a shift-reduce parser are: a) Match and Predict b) Shift and
Reduce c) Expand and Collapse d) Read and Write
Answer: b) Shift and Reduce
27.In shift-reduce parsing, a "handle" is: a) The entire input string. b) A substring that
matches the right side of a production and whose reduction represents one step
along a rightmost derivation in reverse. c) The start symbol of the grammar. d) A
token from the input stream.
Answer: b) A substring that matches the right side of a production and whose
reduction represents one step along a rightmost derivation in reverse.
28.A "reduce" action in shift-reduce parsing involves: a) Moving the current input symbol
to the stack. b) Replacing a handle on the stack with the left-hand side non-terminal
of a production. c) Advancing the input pointer. d) Checking for syntax errors.
Answer: b) Replacing a handle on the stack with the left-hand side
non-terminal of a production.
29.A "shift/reduce conflict" in shift-reduce parsing occurs when: a) The parser can
neither shift nor reduce. b) The parser can either shift the current input symbol or
reduce by a grammar production. c) The parser has no valid move. d) Two
productions have the same right-hand side.
Answer: b) The parser can either shift the current input symbol or reduce by a
grammar production.
30.A "reduce/reduce conflict" in shift-reduce parsing occurs when: a) The parser can
shift two different symbols. b) The parser can reduce by two different grammar
productions. c) The stack is empty. d) The input stream is empty.
Answer: b) The parser can reduce by two different grammar productions.
31.Predictive Parsing Predictive parsing is a type of: a) Bottom-up parsing b)
Shift-reduce parsing c) Top-down parsing d) Operator precedence parsing
Answer: c) Top-down parsing
32.Predictive parsers are also known as: a) LR parsers b) LL(1) parsers c) SLR parsers
d) LALR parsers
Answer: b) LL(1) parsers
33.To construct a predictive parser, we need to eliminate: a) Right recursion b) Left
recursion c) Both left and right recursion d) Ambiguity
Answer: b) Left recursion
34.The FIRST and FOLLOW sets are used in the construction of: a) LR parsing tables b)
Shift-reduce parsers c) Predictive parsing tables d) Lexical analyzers
Answer: c) Predictive parsing tables
35.A grammar is LL(1) if: a) It is left-recursive. b) It has no common prefixes. c) Its
parsing table has no multiple entries. d) It is right-recursive.
Answer: c) Its parsing table has no multiple entries.
36.The LL in LL(1) stands for: a) Left-to-right scan, Leftmost derivation. b) Left-to-right
scan, Rightmost derivation. c) Lookahead Left, Lookahead Left. d) Lexical Left,
Language Left.
Answer: a) Left-to-right scan, Leftmost derivation.
37.What is the lookahead symbol for a predictive parser? a) The entire input string. b)
The next token in the input stream. c) The top of the stack. d) The start symbol.
Answer: b) The next token in the input stream.
38.Predictive parsing typically uses a parse table and a: a) Queue b) Stack c) Hash
table d) Binary tree
Answer: b) Stack
39.The primary disadvantage of recursive descent parsing (a form of predictive parsing)
is: a) It cannot handle left recursion. b) It is very slow. c) It requires a large stack. d) It
cannot handle ambiguous grammars.
Answer: a) It cannot handle left recursion.
40.If a grammar has a production A→Aα, it exhibits: a) Right recursion b) Left recursion
c) Ambiguity d) Epsilon production
Answer: b) Left recursion
41.LR Parsers LR parsers are a type of: a) Top-down parser b) Bottom-up parser c)
Recursive descent parser d) Predictive parser
Answer: b) Bottom-up parser
42.The L in LR stands for: a) Left-to-right scan b) Lookahead c) Lexical d) Leftmost
Answer: a) Left-to-right scan
43.The R in LR stands for: a) Rightmost derivation in reverse b) Right-to-left scan c)
Recursive d) Reduce
Answer: a) Rightmost derivation in reverse
44.Which type of LR parser is the most powerful? a) SLR b) LALR c) Canonical LR d) LL
Answer: c) Canonical LR
45.The state of an LR parser is represented by: a) A stack symbol. b) A set of LR(0)
items. c) The current input token. d) A production rule.
Answer: b) A set of LR(0) items.
46.Canonical Collection of LR(0) Items An LR(0) item is a production with a dot at
some position on its right-hand side, e.g., A→α⋅β. The dot indicates: a) The start of
the production. b) The end of the production. c) The position of the input pointer. d)
How much of the right-hand side has been seen so far.
Answer: d) How much of the right-hand side has been seen so far.
47.The closure operation on a set of LR(0) items adds: a) All productions to the set. b)
All items of the form B→⋅γ for every production B→γ whose left-hand side B appears
immediately after a dot in an existing item. c) All items with a dot at the end. d) All
terminal symbols.
Answer: b) All items of the form B→⋅γ for every production B→γ whose
left-hand side B appears immediately after a dot in an existing item.
48.The goto function in LR parsing calculates: a) The next state given a current state
and a grammar symbol. b) The next input token. c) The next production to use. d)
The next item to add to the closure.
Answer: a) The next state given a current state and a grammar symbol.
49.The canonical collection of LR(0) items is the set of all: a) Possible productions. b)
Valid states of the LR(0) parser. c) Terminal symbols. d) Non-terminal symbols.
Answer: b) Valid states of the LR(0) parser.
50.If a state in an LR(0) automaton contains both an item A→α⋅aβ (where a is a
terminal) and an item B→γ⋅ (a reduce item), it indicates a: a) Shift/reduce conflict in
SLR. b) Reduce/reduce conflict in SLR. c) No conflict. d) LL(1) grammar.
Answer: a) Shift/reduce conflict in SLR.
51.Construction of Canonical LR Parsing Tables Canonical LR (CLR) parsers resolve
conflicts by using a lookahead of: a) 0 symbols b) 1 symbol c) Multiple symbols d)
Follow set of the production
Answer: c) Multiple symbols (specifically, the lookahead set associated with
each reduce item)
52.In CLR parsing, a reduce item A→α⋅,a means: a) Reduce by A→α if the next input
symbol is a. b) Shift a onto the stack. c) Accept the input. d) Error.
Answer: a) Reduce by A→α if the next input symbol is a.
53.The number of states in a CLR parser is generally: a) Less than or equal to SLR. b)
Less than or equal to LALR. c) Greater than or equal to SLR and LALR. d) Always
equal to SLR.
Answer: c) Greater than or equal to SLR and LALR.
54.The main disadvantage of CLR parsers is: a) They are less powerful than SLR. b)
They generate very large parsing tables. c) They cannot handle ambiguous
grammars. d) They are top-down.
Answer: b) They generate very large parsing tables.
55.Construction LALR and SLR Parsing Tables SLR (Simple LR) parsers resolve
shift/reduce conflicts using the FOLLOW set of the non-terminal on the left-hand side
of the reduction. If A→α⋅ is a reduce item, and a is in FOLLOW(A), then: a) Reduce
by A→α if the lookahead is a. b) Shift a. c) This indicates a conflict. d) Accept.
Answer: a) Reduce by A→α if the lookahead is a.
56.SLR parsers are: a) More powerful than CLR. b) Less powerful than LALR. c) The
simplest type of LR parser. d) Only for unambiguous grammars.
Answer: c) The simplest type of LR parser.
57.LALR (Lookahead LR) parsers are formed by: a) Merging states of an SLR parser. b)
Merging states of a CLR parser that have identical LR(0) core items. c) Adding more
lookahead symbols to an SLR parser. d) Eliminating left recursion.
Answer: b) Merging states of a CLR parser that have identical LR(0) core items.
58.The number of states in an LALR parser is typically: a) Equal to CLR. b) Equal to
SLR. c) Greater than CLR. d) Less than CLR but equal to SLR.
Answer: d) Less than CLR but equal to SLR.
59.Which of the following parsers are most commonly used in practice due to their
balance of power and table size? a) SLR b) CLR c) LALR d) LL(1)
Answer: c) LALR
60.LALR parsers can handle all grammars that SLR parsers can handle, and more, but
still fewer than CLR. The difference lies in: a) How lookahead symbols are associated
with reduce items. b) The method of handling left recursion. c) Their ability to detect
errors. d) Their space complexity.
Answer: a) How lookahead symbols are associated with reduce items.
61.Using Ambiguous Grammars with LR Parsers Ambiguous grammars can cause
________ in LR parsing tables. a) No conflicts b) Shift/reduce conflicts c)
Reduce/reduce conflicts d) Both shift/reduce and reduce/reduce conflicts
Answer: d) Both shift/reduce and reduce/reduce conflicts
62.What is a common technique to handle ambiguity (e.g., in expressions like
if-then-else) in an LR parser generator like Yacc/Bison? a) Rewrite the grammar
to be unambiguous. b) Use operator precedence and associativity rules. c) Switch to
a top-down parser. d) Ignore the conflicts.
Answer: b) Use operator precedence and associativity rules.
63.Yacc (Yet Another Compiler Compiler) is a tool for generating: a) Lexical analyzers b)
LR parsers c) Intermediate code d) Optimized code
Answer: b) LR parsers
64.Bison is the GNU version of: a) Lex b) Yacc c) Flex d) GCC
Answer: b) Yacc
65.An LR parsing table consists of two parts: a) Action table and Goto table. b) Shift
table and Reduce table. c) Terminal table and Non-terminal table. d) Production table
and Item table.
Answer: a) Action table and Goto table.
66.Implementation of LR Parsing Tables The ACTION table entry ACTION[s, a]
(where s is state, a is terminal) can contain: a) Shift, Reduce, Accept, or Error. b)
Only Shift or Reduce. c) Only Accept or Error. d) Only the next state number.
Answer: a) Shift, Reduce, Accept, or Error.
67.The GOTO table entry GOTO[s, A] (where s is state, A is non-terminal) contains: a)
The next state number. b) A reduce action. c) An error entry. d) A shift action.
Answer: a) The next state number.
68.When an LR parser performs a "shift" action: a) It pops symbols from the stack. b) It
pushes the current state and the current input symbol onto the stack. c) It replaces
symbols on the stack. d) It generates a new state.
Answer: b) It pushes the current state and the current input symbol onto the
stack.
69.When an LR parser performs a "reduce by A→β" action: a) It shifts β onto the stack.
b) It pops ∣β∣ symbols from the stack, then pushes A and the new state onto the
stack. c) It consults the GOTO table using the lookahead. d) It enters an error state.
Answer: b) It pops ∣β∣ symbols from the stack, then pushes A and the new
state onto the stack.
70.If an LR parser reaches an "accept" state, it means: a) A syntax error has been
found. b) The input string has been successfully parsed. c) The parser needs more
input. d) A deadlock occurred.
Answer: b) The input string has been successfully parsed.
71.Construction LALR Sets of Items The process of constructing LALR(1) states
involves: a) Building SLR states and adding lookaheads. b) Building CLR states and
merging states with identical cores. c) Directly constructing states using a different
algorithm than CLR. d) Using recursive descent.
Answer: b) Building CLR states and merging states with identical cores.
72.An LALR parser is generally preferred over a CLR parser because: a) It is more
powerful. b) It has smaller parsing tables. c) It is easier to implement. d) It avoids all
conflicts.
Answer: b) It has smaller parsing tables.
73.The merging of states in LALR parser construction can introduce: a) New shift/reduce
conflicts that were not present in CLR. b) New reduce/reduce conflicts that were not
present in CLR. c) More powerful parsing capabilities. d) Simpler grammar rules.
Answer: b) New reduce/reduce conflicts that were not present in CLR.
74.If two CLR states have the same LR(0) core but different lookahead sets for their
reduce items, what happens when they are merged into an LALR state? a) The
lookahead sets are unioned. b) A reduce/reduce conflict occurs. c) A shift/reduce
conflict occurs. d) One of the lookahead sets is discarded.
Answer: a) The lookahead sets are unioned.
75.LALR(1) grammars are a proper subset of: a) SLR(1) grammars b) CLR(1) grammars
c) LL(1) grammars d) Regular grammars
Answer: b) CLR(1) grammars