Lec 03. Bottom Up Parsers Parsing Shift Reduce Parser
Lec 03. Bottom Up Parsers Parsing Shift Reduce Parser
Bottom Up Parsing:
Bottom Up Parsers
Operator Precedence Parser
L-R Parser
LR(0)
SLR(1)
CLR(1)
LALR(1)
Bottom Up Parsing Shift Reduce Parser
The process of constructing the parser tree in the Bottom up manner,
i.e. starting from the children & proceeding towards root
S → aABc
A → b / bc
B→d
W = abcdc
Bottom Up Parsing Shift Reduce Parser
E → aA / b a a a b $
LR(0)
Action Goto
a b $ A
1
Accept
4
Bottom Up Parsing Shift Reduce Parser
Stack i/p Action
$ aaab$ Shift
$a aab$ Shift
$aa ab$ Shift
$aaa b$ Shift
$aaab $ Reduced (A → b)
$aaaA $ Reduced (A → aA)
$aaA $ Reduced (A → aA)
$aA $ Reduced (A → aA)
$A $ Accept
Bottom Up Parsing Shift Reduce Parser
Handle: Substring of the i/p string that matches RHS of any
production, is called as Handle.
The process of finding the handle & replacing that handle by it’s
variable is called Handle.
Bottom-Up-Parser is also known as Shift-Reduced Parser.
BUP can be constructed for a both Ambiguous and Unambiguous.
Ambiguous → OPP
Unambiguous → LR(K)
Bottom Up Parsing Shift Reduce Parser
LR(K) Parser can be constructed for a Unambiguous grammar.
BUP simulates the reverse of Right Most Derivation.
BUP can be constructed for the grammar which has more complexity
Model of LR Parser
BUP is faster then TUP, such that BUP is more efficient.
the performance of BUP is very high, avg complexity is 0()
Handle pruning is the overhead for BOP.
Bottom Up Parser Consist of three component
i/p buffer
Parse Stack
Parse Table
Bottom Up Parsing Shift Reduce Parser
Operation in Shift / reduced passer:
Shift
Reduced Action Goto
Accept Terminals Non-Terminal
Error Shift / Reduced Shift
Bottom Up Parsing Shift Reduce Parser
Input Buffer: divide into cell’s and each cell contains only one i/p
symbol.
Stack: stack contains the grammar symbol, the grammar symbol are
push into stack or pop from the stack, using shift & reduced operation.
If handle occurs from the topmost symbol of the stack, then apply the
reduced operations & if handle does not occurs in the topmost of the
stack, then apply the shift operations
Bottom Up Parsing Shift Reduce Parser
Parse table: Parser table is constructed using terminals, non-
Terminals & LR(0) item, this parse table consist of two parts.
Action
Goto
Action part consist Shift and Reduced Operation over the terminals.
Goto part consist of only Shift operation over the Non-Terminals.
Bottom Up Parsing Shift Reduce Parser
Shift: Shift Operation can be used when handle does not occurs from
the topmost symbol of the stack using shift operation, will movig a
look ahead symbol in stack.
Reduced: Reduced operation can be whenever handle occurs from
the topmost symbol from the stack, using reduced operation, we
rename the topmost symbol of the stack that matches with look-ahead
symbol.
Bottom Up Parsing Shift Reduce Parser
Accept: After scanning the complete i/p string from the i/p buffer, if
the stack contains only the start symbol of the grammar as topmost
symbol, then the i/p string is accepted and the parsing is successful.
Error: After the complete i/p string, if the attack contains any symbol
which is different from start symbol as a topmost symbol, then the
parsing is unsuccessful and hence error.
LR(K)
First L is stand for left to right scanning.
Second R stand for reverse of right most derivation.
k is look-ahead symbol.
Bottom Up Parsing Shift Reduce Parser
Procedure for the construction of LR parser table:
Obtain the augmented grammar for the given grammar:
Create the canonical collection of LR items or compiler items.
Draw the DFA & prepare the table based on LR items.
Augmented Grammar
The grammar which is obtained by addition one more production that
generate the start symbol of the grammar, is known as Augmented
Grammar.
S → AB A→a B→ b
S→s S → AB A→a B→ b
LR(0)
The production, which has dot(.) anywhere on RHS is known as
LR(0)
A → abc
LR(0)
A→ .abc
A→ a.bc
A→ ab.c
A→ abc. Final / Complete
Any Question?