Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Compilers

Bottom-up parsing
Bottom-up parsing
• Begins parsing at the leaves (the bottom) and works up towards the
root (the top)
• The key decisions in bottom-up parsing are
• When to reduce
• What production to apply, as the parse proceeds
• At each reduction step a particular substring matching the right side
of a production (body) is replaced by the symbol of the left of that
production
• If the substring is chosen correctly at each step, a rightmost
derivation is traced out in reverse.
Operator precedence parsing
• Operator grammer
• E-> E + E / E * E / id (is an operator grammer)
• E-> EAE / id
• A-> + / * (is not operator grammar)
• In operator grammar no two variables should come side by side.
Example
• E-> E + E / E * E / id
• Id+id*id$
Id + * $ g
Id - > > >
+ < > < >
* < > > >
f
$ < < < -

Id + * $
F 4 2 4 0
G 5 1 3 0
Handle prunning
• A “Handle” is a substring that matches the body of a production, and
whose reduction represents one step along the reverse of a rightmost
derivation
• Removing the children of left hand side non terminal from the parser
tree is called handle pruning.
Right sentential Handle Reducing
form production
Id1 * id2 Id1 F-> id
F * id2 F T->F
T * id2 Id2 F->id
T*F T*F T -> T * F
T T E-> T
E
Shift reduce parsing
• Use two unique steps for bottom-up parsing
• Shift step:
• The shift step refers to the advancement of the input pointer to the next input
symbol (the shifted symbol)
• This symbol is pushed onto the stack. The shifted symbol is treated as a single
node of the parse tree.
• 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.
Cont.
• A stack holds grammar symbols
• An input buffer holds the rest of the string to be parsed
• The handle always appears at the top of the stack just before it is
identified as the handle
• $ is used to mark the bottom of the stack and also the right end of the
input
• Initially, the stack is empty, and the string
Example
LR parser
• Slide 112

You might also like