• retagged by
28,097 views
79 79 votes

Which one of the following is TRUE at any valid state in shift-reduce parsing?

  1. Viable prefixes appear only at the bottom of the stack and not inside
  2. Viable prefixes appear only at the top of the stack and not inside
  3. The stack contains only a set of viable prefixes
  4. The stack never contains viable prefixes

6 Answers

94 94 votes

Answer - C

Explanation:

A handle is actually the one which is always on the top of the stack. A viable prefix(prefix of the Right-hand side of a production or productions), is actually a prefix of the handle and so can never extend past the right end of the handle(i.e. the top of the stack).

The structure of the stack can be considered as a set of viable prefixes - 

$Stack = \{Prefix_1 Prefix_2 Prefix_3 \ldots Prefix_{n-1} Prefix_{n} \}$  and so it is not wrong to say that the stack contains a set of viable prefixes.

• edited by
1 1 vote
Ans C.

The following statement is true at any valid state in shift-reduce parsing:

The stack contains only a set of viable prefixes

In shift-reduce parsing, the stack is used to store a set of viable prefixes of the input string, which are sequences of terminals and non-terminals that can potentially form a valid sentence in the grammar. At any valid state in the parsing process, the stack contains a set of viable prefixes that have been generated so far, and the top of the stack represents the currently active prefix. The parser uses a set of shift and reduce actions to transform the input and stack, until the entire input string has been processed and a complete sentence is formed on the stack.

The other statements are not necessarily true, as viable prefixes can appear inside the stack as well, not only at the top or bottom. The stack may contain not only a set of viable prefixes but also non-viable prefixes.
1 1 vote

First: What is a viable prefix?

A viable prefix is any prefix of a right sentential form that does not go beyond the handle.

In simpler shift-reduce parser terms:

👉 At any valid moment, the contents of the stack form a viable prefix.

That is the key theorem used in LR parsing.


Statement 1:

“Viable prefixes appear only at the bottom of the stack and not inside”

❌ Incorrect

Why?

If the whole stack is a viable prefix, then many prefixes of the stack are also viable prefixes.

Example stack:

 
id + id

Then:

  • id
  • id +
  • id + id

can all be viable prefixes (depending on grammar).

So viable prefixes are not only bottom portion.

They can occur as prefixes inside stack growth.

Hence false.


Statement 2:

“Viable prefixes appear only at the top of the stack and not inside”

❌ Incorrect

Same reason.

Viable prefixes are not only topmost suffixes.

They are related to prefixes from bottom of stack.

Top symbols alone may not form viable prefix.

Example:

Stack =

 
E + T

Top = T

But viable prefix usually refers to stack content from bottom, not isolated top portion.

Hence false.


Statement 3:

“The stack contains only a set of viable prefixes”

✅ Correct

This is the standard LR parsing property.

At every valid shift-reduce step:

  • stack content itself is a viable prefix
  • every prefix of stack is also viable prefix

Thus stack consists of symbols whose cumulative prefixes are viable prefixes.

This is exactly why LR automata recognize viable prefixes.

Hence TRUE.


Statement 4:

“The stack never contains viable prefixes”

❌ Incorrect

Completely opposite of LR theory.

In fact:

👉 LR parser stack always represents a viable prefix at any valid state.

So false.

0 0 votes
Answer: C

The stack of a shift-reduce parser always holds a viable prefix at any valid intermediate step. Thus, stack contains a set of viable prefixes.

0 0 votes
  • By definition in bottom-up compiler design, a viable prefix is a prefix of a right-sentential form that does not extend beyond the right end of the rightmost handle.

  • In a shift-reduce (LR) parser, the sequence of grammar symbols on the parser stack always forms a viable prefix at any valid state.

  • Since the stack contents from the bottom up to the current top always constitute a viable prefix, the entire stack holds a viable prefix (and any prefix of the stack contents is also a viable prefix).

  • Options A, B, and D are false because viable prefixes are not restricted to just the top or bottom, and they are definitely present in the stack.

Correct Option: C. The stack contains only a set of viable prefixes

Answer:
Position:
Show:

Related questions

2 2 votes
3 3 answers
2.4k
2.4k views
Ankit Chourasiya asked Sep 9, 2015
2,402 views
SET OF VIABLE PREFIXES FOR A GIVEN SLR(1) GRAMMAR IS REGULAR LANGUAGE ?
4 4 votes
1 1 answer
2.8k
2.8k views
Ruturaj Mohanty asked Dec 27, 2018
2,823 views
Which of the following statements on Viable Prefixes is incorrect?A viable prefix does not extend past the right end of the handleFor any context-free grammar, the set of...
37 37 votes
2 answers 2 answers
13.6k
13.6k views
go_editor asked Feb 14, 2015
13,589 views
Among simple LR (SLR), canonical LR, and look-ahead LR (LALR), which of the following pairs identify the method that is very easy to implement and the method that is the ...