/    /  Compiler Design-Example for LALR (1) Parser

Example for LALR (1) Parser

 

Let us take an example for LALR(1) Parser and understand it better

 

Example:

Consider the grammar S → AA

    A → aA/b

    And construct LALR (1) parser 

 

Sol: 1. Construction of a canonical set of items

→ Construct augmented grammar as:-

S1 → S

S → AA

A → aA/b

I : S1 → .S

 

Closure (I) = S1 → .S, $

  S → .AA, $

  A → .aA, a/b

  A → .b, a/b

 

Goto (I0, $) = I1 

S1 → S., $

Goto (I0, A) = I2

  S → A•A, $

  A → •aA,$

  A → •b, $

Goto (I0, a) = I3

  A → A•A, a/b

  A → •aA, a/b

  A → •b, a/b

Goto (I3, b) = I4 

  A → b•, a/b

Goto (I0, b) = I4

  A → b•, a/b

Goto (I4, A) = I5

  S → AA• $

Goto (I2, a) = I6

  S → A•A, $

  A → •aA,$

  A → •b, $

Goto (I0, A) = I6

  S → A•A, $

  A → •aA,$

  A → •b, $

Goto (I0, A) = I2

  S → A•A, $

  A → •aA,$

  A → •b, $

Goto (I2, b) = I7

  A → b, $

Goto (I0, b) = I7

  A → b•, $

Goto (I0, b) = I4

  A → b•, a/b

Goto (I3, A) = I8

  A → aA•, a/b

Goto (I3, a) = I3

  S → A•A, a/b

  A → •A,a/b

  A → •b, a/b

 

Reference Link

Example for LALR (1) Parser