Consider the Grammar S → CC C → c C | d Construct the parsing table for LALR (1) parser.

Compiler DesignProgramming LanguagesComputer Programming

Solution

Step1 − Construct LR (1) Set of items. First of all, all the LR (1) set of items should be generated.

In these states, states I3 and I6 can be merged because they have the same core or first component but a different second component of Look Ahead.

Similarly, states I4 and I7 are the same.

Similarly, states I8 and I9 are the same.

So, I3 and I6 can be combined to make I36.

I4 and I7 combined to make I47.

I8 and I9 combined to make I89.

So, the states will be

∴ I3 = goto (I0, c)

But I3 , I6 combined to make I36

∴ I36 = goto (I0, c)

∴ I4 = goto (I0, d)

But I4 , I7 combined to make I47

∴ I47 = goto (I0, d)

∴ I6 = goto (I2, c)

∴ I36 = goto (I2, c)

∴ I7 = goto (I2, d)

∴ I47 = goto (I2, d)

∴ goto (I3, C) = I8

But I8 is now part of I89

∴ goto (I36, C) = I89

Similarly,goto (I3, d) = I4, goto (I6, d) = I7 ∴ goto (I36, d) = I47

Construction of LALR Parsing Table

Filling of "𝐬𝐡𝐢𝐟𝐭" Entries(s)

Consider goto(I0, c) = I36

∴ Action[0, c] = s36

∴ Write s36 in front of Row state 0 and column c.

Similarly, consider

goto(I2, d) = I47

∴ Action[2, d] = 47

∴ Write s47 in front of Row State 2 and column d.

Filling the "𝐫𝐞𝐝𝐮𝐜𝐞" Entries (r)

Consider productions of the form A → α ∙ ,

For example, Consider State

I47 = goto(I0, d)

C → d ∙, c |d |$

∴ C → d ∙, c |d |$ is of form A → α ∙ , a.

Since C → d is production number (3) in given Question.

∴ Write r3 in front of Row State 47 and column c, d, $.

Because c, d looks ahead symbols in production C → d ∙ , c | d.

Filling of goto Entries

It can found out only for Non-Terminal.

For example, Consider

goto(I0, S) = I1

∴ goto [0, S] = 1

Filling of "𝐀𝐜𝐜𝐞𝐩𝐭" Entry

Since, S′ → S ∙ , $ is in I1

∴ Write accept in front of Row state 1 & column $.

LALR Parsing table can also be obtained by merging the rows of combined states of CLR parsing, i.e., Merge Row corresponding to 3, 6, then 4, 7 and then 8, 9.

The resulting LALR Parsing table will be −

raja
Updated on 02-Nov-2021 11:56:06

Advertisements