3. Bottom-Up Parsing
• Bottom-up parsing starts with the input symbols and tries to construct
the parse tree up to the start symbol.
• Bottom-up parsing starts from the leaf nodes of a tree and works in
upward direction till it reaches the root node.
• It is convenient to describe parsing as the process of building parse
trees.
4. Bottom-Up Parsing
• There are three widely used algorithms available for constructing an
LR parser:
SLR(1) – Simple LR Parser:
o Works on smallest class of grammar
o Few number of states, hence very small table
o Simple and fast construction
5. • LR(1) – LR Parser:
o Works on complete set of LR(1) Grammar
o Generates large table and large number of states
o Slow construction
• LALR(1) – Look-Ahead LR Parser:
o Works on intermediate size of grammar
o Number of states are same as in SLR(1)
8. Shift-Reduce Parsing
• Shift-reduce parsing uses two unique steps for bottom-up parsing.
• These steps are known as shift-step and reduce-step.
• Shift step:
• The shift step refers to the advancement of the input pointer to the
next input symbol, which is called the shifted symbol.
• This symbol is pushed onto the stack. The shifted symbol is treated as
a single node of the parse tree.
9. Shift-Reduce Parsing
• Reduce step :
• When the parser finds a complete grammar rule (RHS) and replaces it
to (LHS), it is known as reduce-step.
• This occurs when the top of the stack contains a handle.
• To reduce, a POP function is performed on the stack which pops off the
handle and replaces it with LHS non-terminal symbol.
10. IMPLEMENTATION OF SHIFT-REDUCE
PARSING
• A convenient way to implement a shift-reduce parser is to use a stack to hold
grammar symbols and an input buffer to hold the string w to be parsed. The
symbol $ is used to mark the bottom of the stack and also the right-end of the
input.
• Notationally, the top of the stack is identified through a separator symbol |,
and the input string to be parsed appears on the right side of |. The stack
content appears on the left of |.
• For example, an intermediate stage of parsing can be shown as follows:
$id1 | + id2 * id3$ …. (1)
• Here “$id1” is in the stack, while the input yet to be seen is “+ id2 * id3$*
11. IMPLEMENTATION OF SHIFT-REDUCE
PARSING
• Shift operation:
• The next input symbol is shifted onto the top of the stack.
• After shifting + into the stack, the above state captured in (1) would
change into:
$id1 + | id2 * id3$ …….(2)
12. IMPLEMENTATION OF SHIFT-REDUCE
PARSING
• Reduce operation:
• Replaces a set of grammar symbols on the top of the stack with the
LHS of a production rule.
• After reducing id1 using E → id, the state (1) would change into:
$E | + id2 * id3$... (3)
13. Reductions
• We can think of bottom-up parsing as the process of "reducing" a string
w to the start symbol of the grammar.
• At each reduction step, a specific substring matching the body of a
production is replaced by the non-terminal at the head of that
production.
• The key decisions during bottom-up parsing are about when to reduce
and about what production to apply, as the parse proceeds.
14. Conflicts During Shift-Reduce
Parsing
• There are context-free grammars for which shift-reduce parsing cannot
be used.
• Every shift-reduce parser for such a grammar can reach a
configuration in which the parser, knowing the entire stack contents
and the next input symbol, cannot decide whether to shift or to reduce
(a shift/reduce conflict) Or cannot decide which of several reductions to
make (a reduce/reduce conflict).
15. Conflicts During Shift-Reduce
Parsing (Cont..)
• Note that shift-reduce parsing can be adapted to parse certain
ambiguous grammars, such as the if-then-else grammar.
• If we resolve the shift/reduce conflict on, the parser will behave as we
expect.
16. LR parser
• A general form of shift-reduce parsing is LR (scanning from Left to right and
using Right-most derivation in reverse) parsing, which is used in a number of
automatic parser generators .
• The LR parser is a non-recursive, shift-reduce, bottom-up parser.
• It uses a wide class of context-free grammar which makes it the most efficient
syntax analysis technique.
• LR parsers are also known as LR(k) parsers, where L stands for left-to-right
scanning of the input stream; R stands for the construction of right-most
derivation in reverse, and k denotes the number of lookahead symbols to
make decisions.
17. Conclusion
• What is Bottom -UP parsing
• How to shift reduce steps implement
• What are Conflicts exist during Shift-Reduce Parsing