πŸŽ“ Computer Science & Engineering Portal

Master Engineering Disciplines with Structured Notes

Comprehensive academic lecture notes, exam-oriented unit summaries, laboratory manuals, and previous year question papers designed strictly for university students.

πŸ“‘

University Syllabi

AKTU & AICTE aligned semester credit guidelines.

πŸ“

Exam Question Papers

Previous 5 years solved university semester papers.

πŸ’‘

Lab Manuals & Viva

Practical codes with outputs and interview questions.

πŸ“š All Topics & Units Directory

Click on any subject tag to open its genuine notes directly

Loading your subjects directory...

SLR (1) Parsing

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:

  • For the given input string write a context free grammar
  • Check the ambiguity of the grammar
  • Add Augment production in the given grammar
  • Create Canonical collection of LR (0) items
  • Draw a data flow diagram (DFA)
  • 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.

SLR (1) Parsing

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.

SLR (1) Parsing 1

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

S -> β€’Aa   
A->Ξ±Ξ²β€’  

Follow(S) = {$}  
Follow (A) = {a}  

SLR (1) Parsing 2

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) Parsing 3

SLR (1) Table

SLR (1) Parsing 4

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) = {*, +, $}

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

Labels: ,

Discussion & Queries (<$I18NNumComments$>):

<$CommentPager$>
<$I18NAtCommentTimeWithPermalink$>, <$I18NCommentAuthorSaid$>

<$BlogCommentBody$>

<$BlogCommentDeleteIcon$>
<$CommentPager$>