Bottom Up Parser
Bottom Up Parser
Bottom-up Parsers
• Bottom-up Parsers / Shift Reduce Parsers Build the
parse tree from leaves to root.
• Bottom-up parsing can be defined as an attempt to
reduce the input string w to the start symbol of
grammar.
• It is done by tracing out the rightmost derivations of w in
reverse.
A general shift reduce parsing is LR parsing.
The L stands for scanning the input from left to right and R stands
for constructing a rightmost derivation in reverse.
• Augmented Grammar: If G is a grammar with start symbol S
then G’, the augmented grammar for G, is the grammar with
new start symbol S’ and a production S’ -> S.
Let a grammar be S -> AA
A -> aA | b,
The augmented grammar for the above grammar will be
S’ -> S
S -> AA
A -> aA | b.
• LR(0) Items: An LR(0) is the item of a grammar G is a production of
G with a dot at some position in the right side.
S -> ABC yields four items
S -> .ABC
S -> A.BC
S -> AB.C
S -> ABC. The production A -> ? generates only one item A -> .?
• Closure Operation: If I is a set of items for a grammar G, then
closure(I) is the set of items constructed from I by the two rules:
1. Initially every item in I is added to closure(I).
2. If A -> ?.B? is in closure(I) and B -> ? is a production then add
the item B -> .? to I, If it is not already there.
We apply this rule until no more items can be added to
closure(I).
Goto Operation : Goto(I, X) =
Add I by moving dot after X.
Apply closure to first step.
Construction of GOTO graph-
• State I0 – closure of augmented LR(0) item
• Using I0 find all collection of sets of LR(0) items with the help of DFA
• Convert DFA to LR(0) parsing table
• Construction of LR(0) parsing table:
• The action function takes as arguments a state i and a
terminal a (or $ , the input end marker). The value of
ACTION[i, a] can have one of four forms:
• Shift j, where j is a state.
• Reduce A -> ?.
• Accept
• Error
Step 1:
• Augmented grammar
• S’ -> S
• S -> AA
• A -> aA | b