0% found this document useful (0 votes)
11 views

bottom down parse

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

bottom down parse

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Bottom up parse

In this article, we are discussing the Bottom Up parser. Bottom-up Parsers / Shift
Reduce Parsers Build the parse tree from leaves to root. Bottom-up parsing can
be defined as an attempt to reduce the input string w to the start symbol of
grammar by tracing out the rightmost derivations of w in reverse. Eg.

Classification of Bottom-up Parsers:

A general shift reduce parsing is LR parsing. The L stands for scanning the input
from left to right and R stands for constructing a rightmost derivation in reverse.
Benefits of LR parsing:
1. Many programming languages using some variations of an LR parser. It should
be noted that C++ and Perl are exceptions to it.
2. LR Parser can be implemented very efficiently.
3. Of all the Parsers that scan their symbols from left to right, LR Parsers detect
syntactic errors, as soon as possible.
In this discussion, we will explore the construction of the GOTO graph for a
grammar using all four LR parsing techniques. The GOTO graph is particularly
useful in solving questions in the GATE exam as it allows for a more efficient
analysis of the given grammar.
To construct the GOTO graph using LR(0) parsing, we rely on two essential
functions: Closure() and Goto().
Firstly, we introduce the concept of an augmented grammar. In the augmented
grammar, a new start symbol, S’, is added, along with a production S’ -> S. This
addition helps the parser determine when to stop parsing and signal the
acceptance of input. For example, if we have a grammar S -> AA and A -> aA | b,
the augmented grammar will be S’ -> S and S -> AA.
Next, we define LR(0) items. An LR(0) item of a grammar G is a production of G
with a dot (.) positioned at some point on the right-hand side. For instance, given
the production S -> ABC, we obtain four LR(0) items: S -> .ABC, S -> A.BC, S ->
AB.C, and S -> ABC. It is worth noting that the production A -> ε generates only
one item: A -> .ε.
By utilizing the Closure() function, we can calculate the closure of a set of LR(0)
items. The closure operation involves expanding the items by considering the
productions that have the dot right before the non-terminal symbol. This step helps
us identify all the possible items that can be derived from the current set.
The Goto() function is employed to construct the transitions between LR(0) items in
the GOTO graph. It determines the next set of items by shifting the dot one
position to the right. This process allows us to navigate through the graph and
track the parsing progress.
Augmented Grammar: If G is a grammar with start symbol S then G’, the
augmented grammar for G, is the grammar with new start symbol S’ and a
production S’ -> S. The purpose of this new starting production is to indicate to the
parser when it should stop parsing and announce acceptance of input. Let a
grammar be S -> AA A -> aA | b, The augmented grammar for the above grammar
will be S’ -> S S -> AA A -> aA | b.

Canonical Collection of LR(0) items


An LR (0) item is a production G with dot at some position on the right side of the
production.
LR(0) items is useful to indicate that how much of the input has been scanned up to a
given point in the process of parsing.

In the LR (0), we place the reduce node in the entire row.

Example
Given grammar:

1. S → AA
2. A → aA | b

Add Augment Production and insert '•' symbol at the first position for every production in
G

1. S` → •S
2. S → •AA
3. A → •aA
4. A → •b

I0 State:
Add Augment production to the I0 State and Compute the Closure

I0 = Closure (S` → •S)


Add all productions starting with S in to I0 State because "•" is followed by the non-
terminal. So, the I0 State becomes

I0 = S` → •S
S → •AA

Add all productions starting with "A" in modified I0 State because "•" is followed by the
non-terminal. So, the I0 State becomes.

I0= S` → •S
S → •AA
A → •aA
A → •b

I1= Go to (I0, S) = closure (S` → S•) = S` → S•

Here, the Production is reduced so close the State.

I1= S` → S•

I2= Go to (I0, A) = closure (S → A•A)

Add all productions starting with A in to I2 State because "•" is followed by the non-
terminal. So, the I2 State becomes

I2 =S→A•A
A → •aA
A → •b

Go to (I2,a) = Closure (A → a•A) = (same as I3)

Go to (I2, b) = Closure (A → b•) = (same as I4)

I3= Go to (I0,a) = Closure (A → a•A)

Add productions starting with A in I3.

A → a•A
A → •aA
A → •b

Go to (I3, a) = Closure (A → a•A) = (same as I3)


Go to (I3, b) = Closure (A → b•) = (same as I4)

I4= Go to (I0, b) = closure (A → b•) = A → b•


I5= Go to (I2, A) = Closure (S → AA•) = SA → A•
I6= Go to (I3, A) = Closure (A → aA•) = A → aA•

Drawing DFA:
The DFA contains the 7 states I0 to I6.
LR(0) Table
o If a state is going to some other state on a terminal then it correspond to a shift
move.
o If a state is going to some other state on a variable then it correspond to go to
move.
o If a state contain the final item in the particular row then write the reduce node
completely.

Explanation:
o I0 on S is going to I1 so write it as 1.
o I0 on A is going to I2 so write it as 2.
o I2 on A is going to I5 so write it as 5.
o I3 on A is going to I6 so write it as 6.
o I0, I2and I3on a are going to I3 so write it as S3 which means that shift 3.
o I0, I2 and I3 on b are going to I4 so write it as S4 which means that shift 4.
o I4, I5 and I6 all states contains the final item because they contain • in the right
most end. So rate the production as production number.

Productions are numbered as follows:

1. S → AA ... (1)
2. A → aA ... (2)
3. A → b ... (3)
o I1 contains the final item which drives(S` → S•), so action {I1, $} = Accept.
o I4 contains the final item which drives A → b• and that production corresponds to
the production number 3 so write it as r3 in the entire row.
o I5 contains the final item which drives S → AA• and that production corresponds
to the production number 1 so write it as r1 in the entire row.
o I6 contains the final item which drives A → aA• and that production corresponds
to the production number 2 so write it as r2 in the entire row.

SLR (1) Parsing


SLR (1) refers to simple LR Parsing. It is same as LR(0) parsing. The only difference is
in the parsing table.To construct SLR (1) parsing table, we use canonical collection of
LR (0) item.

In the SLR (1) parsing, we place the reduce move only in the follow of left hand side.

Various steps involved in the SLR (1) Parsing:

o For the given input string write a context free grammar


o Check the ambiguity of the grammar
o Add Augment production in the given grammar
o Create Canonical collection of LR (0) items
o Draw a data flow diagram (DFA)
o Construct a SLR (1) parsing table

SLR (1) Table Construction


The steps which use to construct SLR (1) Table is given below:

If a state (Ii) is going to some other state (Ij) on a terminal then it corresponds to a shift
move in the action part.

If a state (Ii) is going to some other state (Ij) on a variable then it correspond to go to
move in the Go to part.

If a state (Ii) contains the final item like A → ab• which has no transitions to the next
state then the production is known as reduce production. For all terminals X in FOLLOW
(A), write the reduce entry along with their production numbers.
Example

1. S -> •Aa
2. A->αβ•

1. Follow(S) = {$}
2. Follow (A) = {a}

SLR ( 1 ) Grammar
S→E
E→E+T|T
T→T*F|F
F → id

Add Augment Production and insert '•' symbol at the first position for every production in
G

S` → •E
E → •E + T
E → •T
T → •T * F
T → •F
F → •id

I0 State:

Add Augment production to the I0 State and Compute the Closure


I0 = Closure (S` → •E)

Add all productions starting with E in to I0 State because "." is followed by the non-
terminal. So, the I0 State becomes

I0 = S` → •E
E → •E + T
E → •T

Add all productions starting with T and F in modified I0 State because "." is followed by
the non-terminal. So, the I0 State becomes.

I0= S` → •E
E → •E + T
E → •T
T → •T * F
T → •F
F → •id

I1= Go to (I0, E) = closure (S` → E•, E → E• + T)


I2= Go to (I0, T) = closure (E → T•T, T• → * F)
I3= Go to (I0, F) = Closure ( T → F• ) = T → F•
I4= Go to (I0, id) = closure ( F → id•) = F → id•
I5= Go to (I1, +) = Closure (E → E +•T)

Add all productions starting with T and F in I5 State because "." is followed by the non-
terminal. So, the I5 State becomes

I5 = E → E +•T
T → •T * F
T → •F
F → •id

Go to (I5, F) = Closure (T → F•) = (same as I3)


Go to (I5, id) = Closure (F → id•) = (same as I4)

I6= Go to (I2, *) = Closure (T → T * •F)

Add all productions starting with F in I6 State because "." is followed by the non-
terminal. So, the I6 State becomes

I6 = T → T * •F
F → •id

Go to (I6, id) = Closure (F → id•) = (same as I4)


I7= Go to (I5, T) = Closure (E → E + T•) = E → E + T•
I8= Go to (I6, F) = Closure (T → T * F•) = T → T * F•

Drawing DFA:

SLR (1) Table

Explanation:
First (E) = First (E + T) First (T)
First (T) = First (T * F) First (F)
First (F) = {id}
First (T) = {id}
First (E) = {id}
Follow (E) = First (+T) {$} = {+, $}
Follow (T) = First (*F) First (F)
= {*, +, $}
Follow (F) = {*, +, $}

o I1 contains the final item which drives S → E• and follow (S) = {$}, so action {I1,
$} = Accept
o I2 contains the final item which drives E → T• and follow (E) = {+, $}, so action
{I2, +} = R2, action {I2, $} = R2
o I3 contains the final item which drives T → F• and follow (T) = {+, *, $}, so action
{I3, +} = R4, action {I3, *} = R4, action {I3, $} = R4
o I4 contains the final item which drives F → id• and follow (F) = {+, *, $}, so action
{I4, +} = R5, action {I4, *} = R5, action {I4, $} = R5
o I7 contains the final item which drives E → E + T• and follow (E) = {+, $}, so
action {I7, +} = R1, action {I7, $} = R1
o I8 contains the final item which drives T → T * F• and follow (T) = {+, *, $}, so
action {I8, +} = R3, action {I8, *} = R3, action {I8, $} = R3.

You might also like