0% found this document useful (0 votes)
23 views5 pages

Solution Assignment 5 Compiler Design

This document contains an assignment for a Compiler Design course, focusing on shift-reduce parsing and operator precedence parsing. It includes multiple-choice questions (MCQs) with detailed explanations of concepts such as handles, viable prefixes, and the Follow set in grammar. The assignment aims to test understanding of bottom-up parsing techniques and their applications in compiler design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

Solution Assignment 5 Compiler Design

This document contains an assignment for a Compiler Design course, focusing on shift-reduce parsing and operator precedence parsing. It includes multiple-choice questions (MCQs) with detailed explanations of concepts such as handles, viable prefixes, and the Follow set in grammar. The assignment aims to test understanding of bottom-up parsing techniques and their applications in compiler design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

NPTEL Online Certification Courses Indian

Institute of Technology Kharagpur

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.

• In the stack-based implementation of shift-reduce parsing, the handle will always be


located at the top of the stack because the parser shifts symbols onto the stack and
then reduces them when a handle is identified.
• Reduction replaces the handle at the top of the stack with the corresponding non-
terminal.

2. Ans: c)

Solution: In shift-reduce parsing, two types of conflicts can arise:

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.

Explanation of Other Options:

• 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:

Step 1: Understanding Follow(A)

The Follow(A) set consists of symbols that can appear immediately after A in some
derivation.

Rules for computing FOLLOW:

1. If A → αBβ, then everything in FIRST(β) (except ε) is in FOLLOW(B).


2. If A → αB or A → αBβ and FIRST(β) contains ε, then everything in FOLLOW(A)
is in FOLLOW(B).
3. The start symbol includes $ (end of input marker) in its FOLLOW set.

Step 2: Finding Follow(A)

• From the production:


S → AB
o Since B is after A, we check FIRST(B).
o B → abbS | bS | ε → FIRST(B) = {a, b, ε}.
o Since B can be ε, FOLLOW(S) is added to FOLLOW(A).
o FOLLOW(S) includes $ (since S is the start symbol).
o Thus, FOLLOW(A) = FIRST(B) ∪FOLLOW(S) = {a, b} ∪{$} = {a, b, $}.
5.
Answer: b)
Solution:

Shift-reduce parsers are a type of bottom-up parser. Here’s why:

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.

Common Examples of Shift-Reduce Parsers:

• LR Parsers (LR(0), SLR(1), LALR(1), CLR(1))


• Operator-Precedence Parsers

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).

Consider the string and precedence relations:

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)

Solution: In Operator Precedence Parsing, the symbol ≐ indicates "equal precedence"


between two terminals, meaning they belong to the same handle and should be reduced
together.

For the rule:


B → abbS

• 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.

Thus, the correct set of precedence relations is:


a ≐ b and b ≐ b.

9.

Ans: d)

Solution: Explanation:

An operator-precedence parser is a type of bottom-up parser that is also a shift-


reduce parser and constructs derivations in reverse order.
Here's how each option is correct:

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

You might also like