Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Viable Prefix in Bottom-up Parsing

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Viable Prefix is a prefix of a right-sentential form that does not continue past the right end of the rightmost handle of that sentential form.

This clearly means that a viable prefix has a handle at its rightmost end.Not all prefixes of right sentential form can appear on the stack of a shift reduce parser.

This can be shown with the help of following example.

Example –

A=>B+id=>(E)+id  
(rightmost derivation, sentential form) 

Bottom-up parser gives reverse rightmost derivation,so in the above example if we get the string (E)+id during the reverse rightmost derivation then the following operations are performed:

Operation performedStackComments
(.E)+id(shift (
(E.)+id( Eshift E
(E).+id( E )shift )
B.+idBreduce (E) to B
B+.idB +shift +
B+id.B + idshift id
AAreduce B + id to A

As we can see in the above table before shifting + to the stack we have reduced (E) to B. So we can only have (, (E, (E) on stack but we cannot have (E)+ on stack because (E) is a handle and the items in the stack cannot exceed beyond the handle.So here (, (E, (E) are all viable prefixes for the handle (E) and only these prefixes are present in stack of shift reduce parser.

So we keep on shifting the items until we reach the handle or an error occurs. Once a handle is reached we reduce it with a non-terminal using the suitable production. Thus viable prefixes help in taking appropriate shift-reduce decisions.As long as stack contains these prefixes there cannot be any error.

All viable prefixes can be recognized by the LR(0) automaton.Therefore the set of viable prefixes for a given SLR(1) parser is a regular language.This combination of stack with finite state machine is in fact a push-down automaton which is actually used to recognize a context-free language.

The following example illustrates all viable prefixes for the given grammar.

Example –
Given grammar:

S -> AA
A -> bA | a 

Given string –

bbbaa 

Solution :
As we know that bottom-up parsing is the reverse of rightmost derivation of a string for a given grammar.So we use the reverse rightmost derivation of the string to demonstrate this example.

S.No.Reverse Rightmost Derivation with HandlesViable PrefixComments
1.S -> bbbaab, bb, bbb, bbbaHere, a is the handle so viable prefix cannot exceed beyond a.
2.S -> bbbAab, bb, bbb, bbbAHere, bA is the handle so viable prefix cannot exceed beyond bA.
3.S -> bbAab, bb, bbAHere also, bA is the handle so viable prefix cannot exceed beyond bA.
4.S -> bAab, bAHere also, bA is the handle so viable prefix cannot exceed beyond bA.
5.S -> AaA, AaHere, a is the handle so viable prefix cannot exceed beyond a.
6.S -> AAA, AAHere, AA is the handle so viable prefix cannot exceed beyond AA.

My Personal Notes arrow_drop_up
Last Updated : 14 Jul, 2020
Like Article
Save Article
Similar Reads