Professional Documents
Culture Documents
Bottom-Up Parsing
o Bottom-Up Parser : 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.
a string ➔ the starting symbol
reduced to
o At each reduction step, a substring of the input matching to the right
side of a production rule is replaced by the non-terminal at the left side
of that production rule.
o If the substring is chosen correctly, the right most derivation of that
string is created in the reverse order.
Rightmost Derivation: S
rm
*
S
rm aABe
rm
aAde
rm
abbcde
aAbcde rm
o The string to the right of the handle contains only terminal symbols.
Handle Pruning
o The process of finding the handle and replacing that handle by its LHS
variable is called Handle pruning.
o A rightmost derivation in reverse can be obtained by “handle pruning”. That
is, we start with a string of terminals w that we wish to parse. If ω is a
sentence of grammar at hand, then ω = γ,where γn is the nth right-
sentential form of some as yet unknown rightmost derivation
rm rm rm rm rm
Input string
Handle Pruning
S = 0
rm
1
rm
2
rm
...
rm
n-1
rm
n=
E → E+T | T
T → T*F | F
F → (E) | id
5. Draw DFA
7. Based on the information from the Table, with help of Stack and
Once we have Created the canonical collection of LR (0) items, we need to follow the steps
mentioned below:
If there is a transaction from one state (Ii ) to another state(Ij ) on a terminal value then, we
should write the shift entry in the action part as shown below:
If there is a transaction from one state (Ii ) to anot her state (Ij ) on a Non terminal value then,
we should write the subscript value of Ii in the GO TO part as shown below:
If there is one state (Ii), where there is one production which has no transitions.
Then, the production is said to be a reduced production.
These productions should have reduced entry in the Action part along with their
production numbers.
If the Augment production is reducing then, write accept in the Action part.
Canonical-LR
The CLR parser stands for canonical LR parser. It is a more powerful LR
parser. It makes use of lookahead symbols. This method uses a large set of
items called LR(1) items. The main difference between LR(0) and LR(1)
items is that, in LR(1) items, it is possible to carry more information in a
state, which will rule out useless reduction states. This extra information is
incorporated into the state by the lookahead symbol.
Constructing LR(1) Sets of Items
LALR Parser