You are on page 1of 31

CHAPTER 1 (Part 1) - UNIT 2

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:
 SaABe
AAbc/b
Bd
 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.
SaABe
aAde
aAbcde
abbcde
Types of Bottom up parsing

1. Shift Reduce parser


2. Operator precedence parser
3. SLR Parser
4. CLR Parser
5. LALR Parser
1. Shift Reduce Parser
o Shift Reduce parser attempts for the construction of parse in a similar
manner as done in bottom-up parsing i.e. the parse tree is constructed
from leaves (bottom) to the root (up).
o Shift reduce parsing is a process of reducing a string to the start symbol of a
grammar.
o Shift reduce parsing uses a stack to hold the grammar and an input tape to
hold the string.

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 –

Consider the grammar


E –> 2E2
E –> 3E3
E –> 4
Perform Shift Reduce parsing for input string “32423”.
Example 3 − To stack implementation of shift-reduce parsing is done, consider the
grammar −
E→E+E
E→E∗E
E → (E)
E → id
And parse the given input string id1+id2∗id3.

Stack Input String Action

$ id1 + id2 * id3$ Shift

$ id1 +id2 * id3$ Reduce by E → id

$E +id2 * id3$ Shift

$E+ id2 * id3$ Shift

$ E + id2 * id3$ Reduce by E → id

$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:

Consider the following grammar-


E→E–E
E→ExE
E → id
Parse the input string id – id x id using a shift-reduce parser.

Solution-

The priority order is: id > x > –

Stack Input Buffer Parsing Action

$ 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:

Consider the following grammar-


S→(L)|a
L→L,S|S
Parse the input string ( a , ( a , a ) ) using a shift-reduce parser.

Solution-

Stack Input Buffer Parsing Action

$ (a,(a,a))$ Shift

$( a,(a,a))$ Shift

$(a ,(a,a))$ Reduce S → a

$(S ,(a,a))$ Reduce L → S

$(L ,(a,a))$ Shift

$(L, (a,a))$ Shift

$(L,( a,a))$ Shift

$(L,(a ,a))$ Reduce S → a

$(L,(S ,a))$ Reduce L → S

$(L,(L ,a))$ Shift

$(L,(L, a))$ Shift

$(L,(L,a ))$ Reduce S → a

$(L,(L,S) ))$ Reduce L → L , S


$(L,(L ))$ Shift

$(L,(L) )$ Reduce S → (L)

$(L,S )$ Reduce L → L , S

$(L )$ Shift

$(L) $ Reduce S → (L)

$S $ Accept
Problem-6:

Consider the following grammar-


S → T L;
T → int | float
L → L , id | id
Parse the input string int id , id ; using a shift-reduce parser.

Solution-

Stack Input Buffer Parsing Action

$ int id , id ; $ Shift

$ int id , id ; $ Reduce T → int

$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-

Stack Input Buffer Parsing Action

$ 10201$ Shift

$1 0201$ Shift

$10 201$ Shift

$102 01$ Reduce S → 2

$10S 01$ 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

And parsing the following input string is: a1-(a2+a3)

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:
EE+E/E*E/(E)/id

Right-sentential form Handle Reducing production


Id + id * id id E id
E+ id * id Id Eid
E+E* id id Eid
E+E*E E*E EE*E
E+E E+E EE+E
E
HANDLES:
A Handle of a string is a substring that matches the right side of a production,
and whose reduction to the non-terminal on the left side of the production
represents one step along the reverse of a right most derivation.

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.

Operator Precedence Relations


Three precedence relations exist between the pair of terminals.

Relation Meaning

p <. q p has less precedence than q.

p >. q p has more precedence than q.

p =. q p has equal precedence than q.

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
* .> *

Example: In a statement a ↑ (b ↑ c) here, ↑ is the Right Associative operator


∴ It will become a ↑ (b ↑ c)
∴ (b ↑ c) will be computed first.
∴↑<. ↑
 Rule 3:Identifier has more precedence then all operators and symbols.
∴θ<. id $ <. id
id .> θ id .> $
id .>)
(<. id.
 Rule 4:$ has less precedence than all other operators and symbols.
$ <. (
id .> $
$ <. +
). > $
$ <.*
Example2 − Construct the Precedence Relation table for the Grammar.
E → E + E | E ∗ E/id
Solution

Operator-Precedence Relations

Id + * $

Id .> .> .>

+ <. .> <. .>

* <. .> .> .>

$ <. <. <.

Advantages of Operator Precedence Parsing


 It is accessible to execute.
Disadvantages of Operator Precedence Parsing
 Operator Like minus can be unary or binary. So, this operator can have different
precedence’s in different statements.
 Operator Precedence Parsing applies to only a small class of Grammars.
Example1 − Construct the Precedence Relation table for the Grammar.
E → E + E|E − E|E ∗ E|E⁄E|E ↑ E|(E)| − E|id
Using the following Assumptions:

Operators Precedence Association

↑ Highest Right Associative

* and / Next Highest Left Associative

+ and − Lowest Left Associative


Solution
Operator Precedence Relations

+ − * / ↑ id ( ) $

+ .> .> <. <. <. <. <. .> .>

− .> .> <. <. <. <. <. .> .>

* .> .> .> .> <. <. <. .> .>

/ .> .> .> .> <. <. <. .> .>

↑ .> .> .> .> <. <. <. .> .>

id .> .> .> .> .> .> .>

( <. <. <. <. <. <. <. =

) .> .> .> .> .> .> .>

$ <. <. <. <. <. <. <.


Example2 − Find out all precedence relations between various operators & symbols in the
following Grammar & show them using the precedence table.
E → E + T|T
T → T ∗ F|F
F → (E)|id
Solution:

+ * ( ) id $

+ .> .< <. .> <. .>

* .> .> <. .> <. .>

( <. <. <. =. <.

) .> .> .> .>

id .> .> .> .>

$ <. <. <. <.


The operator precedence parsing algorithm is as follows −
Input − The precedence relations from some operator precedence grammar and
an input string of terminals from that grammar.
Output − There is no output but it can construct a skeletal parse tree as we parse,
with one non-terminal labeling all interior nodes and the use of single productions
not shown. Alternatively, the sequence of shift-reduce steps can be considered
the output.
Method:
Let the input string be a1a2 … …. an.$initially, the stack contains $.
Repeat forever
If only $ is on the stack and only $ is on the input then accept and break
else
begin
let a be the topmost terminal symbol on the stack and let b be the current
input symbols.
If a <. b or a =. b then shift b onto the stack /*Shift*/
else if a .>b then /*reduce*/
repeat pop the stack
until the top stack terminal is related by <. to the terminal most recently
popped.
else call the error-correcting routine end.
Problem-01:

Consider the following grammar-


E → EAE | id
A→+|x
Construct the operator precedence parser table and parse the string id1 + id2 x id3.

Solution-

Step-01:

We convert the given grammar into operator precedence grammar.The


equivalent operator precedence grammar is-
E → E + E | E x E | id

Step-02:

The terminal symbols in the grammar are { id, + , x , $ }


We construct the operator precedence table as-
id + x $

id > > >

+ < > < >

x < > > >

$ < < <

Operator Precedence Table

Parsing Given String-

Given string to be parsed is id + id x id.


We follow the following steps to parse the given string-

Step-01:

We insert $ symbol at both ends of the string as-


$ id + id x id $

We insert precedence operators between the string symbols as-


$ < id > + < id > x < id > $

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:

Consider the following grammar-


S→(L)|a
L→L,S|S
Construct the operator precedence parser and parse the string ( a , ( a , a ) ).

Solution-

The terminal symbols in the grammar are { ( , ) , a , , }


We construct the operator precedence table as-

a ( ) , $

a > > > >

( < > > > >

) < > > > >

, < < > > >


$ < < < <

Operator Precedence Table


Parsing Given String-
Given string to be parsed is ( a , ( a , a ) ).
We follow the following steps to parse the given string-
Step-01:

We insert $ symbol at both ends of the string as-


$(a,(a,a))$

We insert precedence operators between the string symbols as-


$ <( < a > , < ( < a > , < a > ) > ) > $
Step-02:

We scan and parse the string as-


$ <( < a > , < ( < a > , < a > ) > ) > $
$ <( S , < ( < a > , < a > ) > ) > $
$ <( S , < ( S , < a > ) > ) > $
$ <( S , < ( S , S ) > ) > $
$ <( S , < ( L , S ) > ) > $
$ <( S , < ( L ) > ) > $
$ <( S , S ) > $
$ <( L , S ) > $
$ <( L ) > $
$<S>$
$$
Accept.

Example:

Grammar:

1. E → E+T/T
2. T → T*F/F
3. F → id

Given string:

1. w = id + id * id

Let us consider a parse tree for it as follows:

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:

You might also like