Q.1 Which of the following is the primary goal of syntax analysis in a compiler?
To check the grammatical structure of the source code
To generate machine code
To optimize memory usage
To perform lexical analysis
Explanation - Syntax analysis, also called parsing, ensures that the sequence of tokens produced by the lexical analyzer conforms to the grammar of the programming language.
Correct answer is: To check the grammatical structure of the source code
Q.2 Which data structure is commonly used by parsers to manage grammar symbols during parsing?
Queue
Stack
Array
Linked List
Explanation - Parsers often use a stack to keep track of grammar symbols and intermediate parsing states, especially in shift-reduce and recursive-descent parsing.
Correct answer is: Stack
Q.3 A grammar is said to be ambiguous if:
It generates more than one parse tree for some string
It has left recursion
It contains epsilon productions
It cannot be parsed by a parser
Explanation - Ambiguity in grammar occurs when a single input string can be derived in more than one way, resulting in multiple parse trees.
Correct answer is: It generates more than one parse tree for some string
Q.4 Which parsing technique reads input from left to right and constructs a rightmost derivation?
LR Parsing
LL Parsing
Recursive-Descent Parsing
Operator Precedence Parsing
Explanation - LR parsers scan input left-to-right and produce a rightmost derivation in reverse (hence 'LR'). LL parsers produce a leftmost derivation.
Correct answer is: LR Parsing
Q.5 Which of the following is an example of a top-down parser?
LR Parser
SLR Parser
Recursive-Descent Parser
LALR Parser
Explanation - Recursive-descent parsing is a top-down approach that expands the parse tree from the root using recursive procedure calls.
Correct answer is: Recursive-Descent Parser
Q.6 Left recursion in a grammar is problematic for which type of parser?
LR Parser
LL Parser
Operator Precedence Parser
CYK Parser
Explanation - LL parsers cannot handle left-recursive grammars directly because it leads to infinite recursion in top-down parsing.
Correct answer is: LL Parser
Q.7 Which parser type uses a parsing table to decide shift and reduce actions?
LL Parser
LR Parser
Recursive-Descent Parser
Predictive Parser
Explanation - LR parsers use a parsing table to determine when to shift input symbols onto the stack or reduce them using a production rule.
Correct answer is: LR Parser
Q.8 Which of the following is not a type of LR parser?
SLR
LALR
CLR
PLR
Explanation - SLR (Simple LR), LALR (Lookahead LR), and CLR (Canonical LR) are all LR parser types. PLR is not a recognized LR parser variant.
Correct answer is: PLR
Q.9 What does the FIRST set of a non-terminal in a grammar represent?
Set of terminals that can appear at the beginning of strings derived from the non-terminal
Set of all terminals in the grammar
Set of non-terminals that can appear after it
Set of epsilon productions
Explanation - FIRST(X) gives all terminals that can appear first in strings derived from X, helping in predictive parsing and constructing parsing tables.
Correct answer is: Set of terminals that can appear at the beginning of strings derived from the non-terminal
Q.10 The FOLLOW set of a non-terminal is used primarily in:
LR Parsing
LL Parsing
Operator Precedence Parsing
CYK Parsing
Explanation - FOLLOW sets are used in LL(1) parsers to determine which production to use when the FIRST set includes epsilon, by considering the next input symbol.
Correct answer is: LL Parsing
Q.11 Which of the following grammars can a predictive parser handle directly?
Left-recursive grammar
Ambiguous grammar
LL(1) grammar
Context-sensitive grammar
Explanation - Predictive parsers require LL(1) grammars (Left-to-right scan, Leftmost derivation, 1 lookahead symbol) and cannot handle left recursion or ambiguity directly.
Correct answer is: LL(1) grammar
Q.12 In bottom-up parsing, a 'handle' is:
A substring that matches the right side of a production and can be reduced
A terminal symbol
A non-terminal symbol
A recursive call
Explanation - The handle is the substring of symbols on the stack that matches the RHS of a production and can be replaced by the LHS during reduction.
Correct answer is: A substring that matches the right side of a production and can be reduced
Q.13 Which parsing strategy constructs the parse tree from leaves to root?
Top-down parsing
Bottom-up parsing
Recursive-descent parsing
Predictive parsing
Explanation - Bottom-up parsers start with the input symbols (leaves) and gradually reduce them to the start symbol (root) of the parse tree.
Correct answer is: Bottom-up parsing
Q.14 Which of the following is true for operator precedence parsing?
It can handle any grammar
It requires the grammar to be operator-precedence
It is a top-down parsing technique
It does not use a stack
Explanation - Operator-precedence parsers can handle only operator-precedence grammars and rely on precedence relations among operators to parse expressions.
Correct answer is: It requires the grammar to be operator-precedence
Q.15 Which problem does left factoring solve in grammars?
Eliminating left recursion
Resolving ambiguity
Making grammar suitable for predictive parsing
Reducing number of terminals
Explanation - Left factoring is used to remove common prefixes in productions so that the parser can make a choice using a single lookahead symbol.
Correct answer is: Making grammar suitable for predictive parsing
Q.16 What is the primary difference between LL and LR parsers?
LL is bottom-up, LR is top-down
LL is top-down, LR is bottom-up
LL can handle left recursion, LR cannot
LL requires parsing table, LR does not
Explanation - LL parsers are top-down and construct a leftmost derivation, while LR parsers are bottom-up and construct a rightmost derivation in reverse.
Correct answer is: LL is top-down, LR is bottom-up
Q.17 Which of the following is true for SLR parsing?
It uses FOLLOW sets to resolve conflicts
It is a type of top-down parser
It cannot handle shift-reduce conflicts
It is equivalent to recursive-descent parsing
Explanation - SLR (Simple LR) parsers use FOLLOW sets of non-terminals to resolve reduce actions in parsing tables and handle conflicts in a simple LR parser.
Correct answer is: It uses FOLLOW sets to resolve conflicts
Q.18 Which type of grammar is always suitable for LR parsing?
Left-recursive grammar
Regular grammar
Context-free grammar
Context-sensitive grammar
Explanation - LR parsers can handle all deterministic context-free grammars, including many left-recursive grammars, making them more powerful than LL parsers.
Correct answer is: Context-free grammar
Q.19 The action of reducing in bottom-up parsing corresponds to:
Replacing RHS of production with LHS
Replacing LHS with RHS
Scanning the next input symbol
Generating machine code
Explanation - Reduction in bottom-up parsing replaces a sequence of symbols that matches the right-hand side of a production with the corresponding left-hand side non-terminal.
Correct answer is: Replacing RHS of production with LHS
Q.20 What type of error is easiest to detect during syntax analysis?
Lexical errors
Syntax errors
Semantic errors
Logical errors
Explanation - Syntax errors violate the grammatical rules of the language and are detected during the parsing phase of compilation.
Correct answer is: Syntax errors
Q.21 Which parsing approach uses recursive procedures for each non-terminal?
LR Parsing
Recursive-Descent Parsing
Operator Precedence Parsing
CYK Parsing
Explanation - Recursive-descent parsing implements each non-terminal as a recursive procedure, calling other procedures corresponding to non-terminals on the RHS of productions.
Correct answer is: Recursive-Descent Parsing
Q.22 What is the key advantage of LL(1) parsers?
They can handle ambiguous grammars
They require no lookahead
They are simple and efficient for small grammars
They can parse all context-free grammars
Explanation - LL(1) parsers are easy to implement and efficient for grammars that can be parsed with a single lookahead symbol, though they cannot handle all context-free grammars.
Correct answer is: They are simple and efficient for small grammars
Q.23 Which of the following is true for LR(1) parsers compared to SLR parsers?
LR(1) is less powerful
LR(1) uses lookahead symbols and can handle more grammars
LR(1) is top-down
LR(1) cannot handle shift-reduce conflicts
Explanation - LR(1) parsers use one lookahead symbol and are more powerful than SLR parsers, resolving ambiguities that SLR cannot.
Correct answer is: LR(1) uses lookahead symbols and can handle more grammars
Q.24 Which parsing method uses a table-driven approach for top-down parsing?
LL(1) Parsing
LR Parsing
Operator Precedence Parsing
Recursive-Descent Parsing without backtracking
Explanation - LL(1) parsers often use a parsing table constructed from FIRST and FOLLOW sets to decide which production to apply based on the current input symbol.
Correct answer is: LL(1) Parsing
