Bottomupparsingh
Bottomupparsingh
LR(0) LR(0)
LR Parser items
SLR(1)
Operator
precedence
parser
Bottom-up Parsing
o Bottom-Up Parser : In bottom up parsing we should constructs a
parse tree for an input string beginning at the leaves(the bottom)
and working up towards the root(the top)
o We can think of this process as one of “reducing” a string w to the
start symbol of a grammar.
o Bottom-up parsing is also known as shift-reduce parsing because
its two main actions are shift and reduce.
At each shift action, the current symbol in the input string is
pushed to a stack.
At each reduction step, the symbols at the top of the stack (this
symbol sequence is the right side of a production) will replaced
by the non-terminal at the left side of that production.
Shift-Reduce Parsing
o A shift-reduce parser tries to reduce the given input string into
the starting symbol.
rm rm
Shift-Reduce Parser finds: ... S
Shift–Reduce Parsing-Example
o Consider the grammar Input string : abbcde
S aABe aAbcde
A Abc | b aAde reduction
B d aABe
S
o These reductions in fact trace out the following right-most derivation in reverse
rm rm rm rm
▫ But not every substring matches the right side of a production rule is
handle.
S A
Input string
A Shift-Reduce Parser
E E+T | T Right-Most Derivation of id+id*id
T T*F | F E E+T E+T*F E+T*id E+F*id
F (E) | id E+id*id T+id*id F+id*id id+id*id
$E+T*i $
d $ Shift
$E+T*F $ F 1 F 4
id
$E+T $ Reduce by Fid
$E Reduce by TF
Shift
Shift id id
Reduce by Fid
LR Parser:
• The LR parsing method is the most general non-backtracking shift-reduce
parsing method known.
• LR parsers can parse a strictly larger class of grammars than (top-down)
predictive parsers.
• LR parsers can usually recognize all programming language construct that
can be specified by context-free grammars.
• LR parsers detect errors fast.
• Drawback: it is too much work to construct an LR parser by hand.
• Fortunately, we can use an LR parser generator such as YACC.
Introduction to bottom up parser:
LR(0) LR(0)
LR Parser items
SLR(1)
Operator
precedence
parser
An LR-Parser uses-
• states to memorize information during the parsing process,
• an action table to make decision (such as shift or reduce) and to compute states
• a goto table to compute states
• These states are embedded with grammar symbols in a stack. More, precisely, after
eahc iteration, the stack stores a word of the form s0X1s1X2 ... sm-1Xmsm where s0 is the
end-of-stack symbol and sm is the state on top of the stack.For a state s and a
terminal a, the entry action[s, a] has one of the four following forms
• shift s' where s' is a state,
• reduce A ,
• accept,
• error.
LR Parsing model:
Reasons for attractiveness of LR parser
• An LR parser can detect the syntax errors as soon as they can occur.
parser generator.