Solution Assignment 5 Compiler Design
Solution Assignment 5 Compiler Design
Compiler Design
Assignment- Week 5
TYPE OF QUESTION:MCQ
Number ofquestions:10 Total mark: 10 X 1 = 10
1. Ans: a)
Solution: In shift-reduce parsing, a handle is the substring of the input that matches the right
side of a production rule and whose reduction to the non-terminal on the left side of the rule
represents one step in the reverse of a rightmost derivation.
2. Ans: c)
1. Shift-Reduce Conflict:
o This occurs when the parser cannot decide whether to shift the next input
symbol onto the stack or reduce the handle on top of the stack.
o Common in ambiguous grammars.
2. Reduce-Reduce Conflict:
o This happens when the parser finds two or more possible reductions at the
same point, and it cannot decide which production to use.
3. Shift-Shift Conflict (Not Possible):
o This is not possible because shifting means reading the next input symbol, and
there is no ambiguity about which symbol to shift. The parser always shifts if
shifting is allowed, with no competing shift operations.
3. Ans: c)
Explanation:
In shift-reduce parsing, a viable prefix is a string of symbols that can appear at the top
of the stack and still be a part of a valid rightmost derivation of the input.
• The stack in shift-reduce parsing maintains a viable prefix of the input string.
• A viable prefix is a prefix of a right-sentential form that does not extend
beyond a handle (the substring that can be reduced).
• This ensures that every step taken in shift-reduce parsing leads to a valid
derivation.
• a) At the bottom we find the prefixes ❌ (Incorrect, because the bottom of the
stack holds the start symbol or intermediate derivations, not necessarily a
prefix.)
• b) None of the mentioned ❌ (Incorrect, since option c is correct.)
• d) Stack consists of viable prefixes ❌ (Incorrect wording— the stack does not
consist of multiple viable prefixes, it contains only the current viable prefix.)
4. Answer: b)
Solution: Solution:
The Follow(A) set consists of symbols that can appear immediately after A in some
derivation.
1. Bottom-up Parsing
o Shift-reduce parsing begins with the input string and attempts to build the parse tree
by reducing the input symbols to the start symbol of the grammar.
o It works in reverse order of a rightmost derivation, meaning it traces the derivation
backwards from leaves (input symbols) to the root (start symbol).
2. How Shift-Reduce Parsing Works:
o Shift: Move the next input symbol onto the stack.
o Reduce: If a handle (substring that matches the right side of a production rule) is on
top of the stack, replace it with the corresponding non-terminal.
o Accept: If the entire input is reduced to the start symbol, the parser accepts the
input.
o Error: If no valid move exists (shift or reduce), the parser reports an error.
6.
Answer: a)
Solution: In shift-reduce parsing, a handle is a substring that matches the right-hand side of a
grammar production and whose reduction represents one step in the reverse of a rightmost
derivation.
• During parsing, symbols are pushed onto the stack as part of the shift operation.
• When the parser identifies a handle at the top of the stack, it performs a reduce
operation, replacing the handle with the corresponding non-terminal.
Thus, the handle is always located at the top of the stack when it's ready for reduction.
7.
Answer: c)
Solution: In Operator Precedence Parsing, special symbols like <• and •> are used to
denote the precedence relationship between terminals.
• <• (Left Precedence) indicates that the terminal on the left has lower precedence than
the one on the right.
• •> (Right Precedence) indicates that the terminal on the left has higher precedence
than the one on the right.
The handle is the substring located between <• and •>, as this represents the part of the
input that corresponds to a reducible substring (i.e., it matches the right-hand side of a
production and can be replaced by the corresponding non-terminal).
id <• + •> id
• The handle is located between <• and •>, which corresponds to id in this case. This
is the part that can be reduced based on a grammar rule.
Thus, the handle in operator precedence parsing is always between <• and •>.
8.
Ans: b)
• a ≐ b: Since a and b are adjacent in the production abbS, it implies that a and b have
equal precedence (≐).
• b ≐ b: The two consecutive b symbols in abbS indicate that b has equal precedence
with another b, as they form part of the same handle.
9.
Ans: d)
Solution: Explanation:
1. Shift-Reduce Parser ✅
o Operator-precedence parsers use shift (to push operators and operands
onto the stack) and reduce (to apply grammar rules).
o This confirms that it follows a shift-reduce parsing strategy.
2. Bottom-Up Parser ✅
o It starts from the input (tokens) and constructs the parse tree from
leaves to root, making it a bottom-up parser.
o Unlike top-down parsers, it does not predict rules but instead reduces
input based on precedence.
3. Constructs Derivation in Reverse ✅
o Bottom-up parsers construct rightmost derivations in reverse.
o That means the derivation is built from leaf nodes to the start symbol.
o Since operator-precedence parsers fall under bottom-up parsing, they
also construct derivations in reverse order.
10.
Ans: d)
Solution: Bottom-up parsing is a method that constructs the parse tree starting from the input
symbols and works its way up to the start symbol of the grammar. It involves the following key
techniques:
1. Shift-Reduce Parsing:
o This technique shifts symbols onto a stack and reduces them when a handle (a
substring that matches the right-hand side of a production) is found.
o Shift: Move the next input symbol onto the stack.
o Reduce: Replace the handle on the stack with the corresponding non-terminal.
2. Handle Pruning:
o Handle pruning is the process of identifying and reducing handles during bottom-up
parsing.
o It involves recognizing the substring that corresponds to the right-hand side of a
grammar rule and replacing it with the left-hand side (a non-terminal). This builds
the parse tree in reverse order.
END of Assignment