Professional Documents
Culture Documents
BOTTOM-UP PARSING:
Bottom-up parser builds a derivation by working from the input sentence
back towards the start symbol S.
For Example, Consider the following grammar:
SaABe
AAbc/b
Bd
The sentence “ abbcde “ can be reduced to S by the following steps :
abbcde
aAbcde
aAde
aABe
S
Similarly, the Right most derivation in reverse order is done in bottom-up
parsing.
In the reductions, in fact trace out the following right most derivation in
reverse order.
SaABe
aAde
aAbcde
abbcde
Types of Bottom up parsing
Basic Operations:
o Shift: This involves moving of symbols from input buffer onto the stack.
o Reduce: If the handle appears on top of the stack then, its reduction by
using appropriate production rule is done i.e. RHS of production rule is
popped out of stack and LHS of production rule is pushed onto the stack.
o Accept: If only start symbol is present in the stack and the input buffer is
empty then, the parsing action is called accept. When accept action is
obtained, it is means successful parsing is done.
o Error: This is the situation in which the parser can neither perform shift
action nor reduce action and not even accept action.
Example 1 – Consider the grammar
S –> S + S
S –> S * S
S –> id
Perform Shift Reduce parsing for input string “id + id + id”.
Example 2 –
$E + E * id3$ Shift
$E + E * id3$ Shift
$E + E * id3 $ Reduce by E → id
$E + E * E $ Reduce by E → E * E
$E + E $ Reduce by E → E + E
$E $ Accept
Problem-4:
Solution-
$ id – id x id $ Shift
$ id – id x id $ Reduce E → id
$E – id x id $ Shift
$E– id x id $ Shift
$ E – id x id $ Reduce E → id
$E–E x id $ Shift
$E–Ex id $ Shift
$ E – E x id $ Reduce E → id
$E–ExE $ Reduce E → E x E
$E–E $ Reduce E → E – E
$E $ Accept
Problem-5:
Solution-
$ (a,(a,a))$ Shift
$( a,(a,a))$ Shift
$(L,S )$ Reduce L → L , S
$(L )$ Shift
$S $ Accept
Problem-6:
Solution-
$ int id , id ; $ Shift
$T id , id ; $ Shift
$ T id , id ; $ Reduce L → id
$TL , id ; $ Shift
$TL, id ; $ Shift
$ T L , id ;$ Reduce L → L , id
$TL ;$ Shift
$TL; $ Reduce S → T L
$S $ Accept
Problem-7:
Considering the string “10201”, design a shift-reduce parser for the following
grammar-
S → 0S0 | 1S1 | 2
Solution-
$ 10201$ Shift
$1 0201$ Shift
$10S0 1$ Reduce S → 0 S 0
$1S 1$ Shift
$1S1 $ Reduce S → 1 S 1
$S $ Accept
Example 8: Consider the following grammar-
S → S+S
S → S-S
S → (S)
S→a
Parsing table:
HANDLE PRUNING:
HANDLE PRUNING is the general approach used in shift-and-reduce parsing.
Removing the children of the left-hand side non-terminal from the parse
tree is called Handle Pruning. A rightmost derivation in reverse can be
obtained by handle pruning.
A Handle is a substring that matches the body of a production. Handle
reduction is a step in the reverse of rightmost derivation. A rightmost
derivation in reverse can be obtained by handle pruning.(OR)
Keep removing handles, replacing them with corresponding LHS of
production, until we reach the start symbol S.
Example:
EE+E/E*E/(E)/id
POSSIBLE CONFLICTS:
1. Shift-reduce: Both a shift action and a reduce action are possible in the same
state (should we shift or reduce)
2. Reduce-reduce: Two or more distinct reduce actions are possible in the same
state. (Which production should we reduce with 2).
Ambiguous grammars lead to parsing conflicts.
2. Operator precedence parsing
Operator Precedence Parsing is also a type of Bottom-Up Parsing that can be used
to a class of Grammars known as Operator Grammar.
A Grammar G is Operator Grammar if it has the following properties −
Production should not contain ϵ on its right side.
There should not be two adjacent non-terminals at the right side of
production.
Example1:
Verify whether the following Grammar is operator Grammar or not.
E → E A E |(E)|id
A → +| − | ∗
Solution:
No, it is not an operator Grammar as it does not satisfy property 2 of operator
Grammar.
As it contains two adjacent Non-terminals on R.H.S of production E → E A E.
We can convert it into the operator Grammar by substituting the value of A in
E → E A E.
E → E + E |E − E |E * E |(E) | id.
Relation Meaning
Depending upon these precedence Relations, we can decide which operations will
be executed or parsed first.
Association and Precedence Rules
Rule 1 : If operators have different precedence
Since * has higher precedence than +
Example:
o In a statement a + b * c
∴ + <. *
o In statement a * b + c
∴∗ . > +
Rule 2:If operators have Equal precedence.
Example:
o In statement (a + b) + c and here + operators are having equal
precedence.
As '+' is left Associative in (a + b) + c
∴ (a + b) will be computed first, and then it will be added to c.
i.e., (a + b) + c
+ .> +
o Similarly, '*' is left Associative in a * b * c
* .> *
Operator-Precedence Relations
Id + * $
+ − * / ↑ id ( ) $
+ * ( ) id $
Solution-
Step-01:
Step-02:
Step-01:
Step-02:
Solution:We scan and parse the string as-
Id + id x id
$ Id + id x id $
$ < id > + < id > x < id > $
$ E + < id > x < id > $
$ E + E x < id > $
$E+ExE$
$+x$
$<+<x>$
$<+$
$<+>$
$$
Accept.
Problem-02:
Solution-
a ( ) , $
Example:
Grammar:
1. E → E+T/T
2. T → T*F/F
3. F → id
Given string:
1. w = id + id * id
On the basis of above tree, we can design following operator precedence table:
Now let us process the string with the help of the above precedence table: