How to Fill the Entries in Parsing Table?

Compiler DesignProgramming LanguagesComputer Programming

A parser is the second phase of compilation. The parser takes as its input tokens generated from the previous phase, i.e., the Lexical Analyzer phase, and groups them in such a way that their syntax can be recognized.

For example,

Consider I0

I0 − E′ → ∙ E

      E → ∙ E + T

     E → ∙ T

     T → ∙ T ∗ F

     T → ∙ F

     F → ∙ (E)

     F → ∙ id

Filling of Shifting Entries

Applying Rule (2a) of the algorithm of construction of SLR Parsing Table on a set of a production of I0. Rule (2a) will be applied only when there is a terminal symbol after the dot. Consider F → ∙ (E)

Compare A → α ∙ a β with F → ∙ (E)

F →ε(E )
A →α.aB

∴ A = F, α = ε, a = (, β = E)

Applying Rule, goto (Ii, a) = Ij

∵     goto (I0, ( ) = I4

∴    Action [0, ( ] = Shift 4

∴ Write s4 in front of Row 0 and column (

Similarly, compare F → ∙ id

Applying Rule (2a)

Compare A → α ∙ a β with F → ∙ id

F →εidE
A →α.aB

∴ A = F, α = ε, a = id, β = ε

∴ goto (I0, id) = I5

∴ Action [0, id] = shift 5

Write s5 in front of Row 0 and column id

Similarly, the complete table will be filled with shift actions by processing all states from I0 to I11 in a similar manner.

Filling of Reduce Entries (r) − For filling reduce (r) entries, we have to apply rule (2b) of the construction of the SLR Parsing Table.

From I0 to I11 we have to see productions of the form A → α ∙

Consider

I2 − E → T ∙

T → T ∙ * F

Here, we have the production E → T ∙ of form A → α ∙ in which dot appears at last position. So, compare A → α ∙ with E → T ∙

E →T
A →α.

Applying Rule, we have to find FOLLOW (E)

FOLLOW (E) = { +, ), $}

The production number of E → T in Question is (2)

Write r2 in front of the row of state 2 and column of +,), $.

action [2, +] = r2, action [2, )] = r2, action [2, $] = r2.

Here, r2 refers to reduce using production number 2

Similarly, consider another state I3

I3 − T → F ∙

Apply Rule (2b)

Compare T → F ∙ with A → α ∙

T →F
A →α.

∴ A = T, α = F

FOLLOW (T) = { +,*, ), $}

Since T → F is production number (4) in Question

Write r4 in front of the row of state 3 and column of +,*, ), $.

Action [3, +] = r4                 Action [3, $] = r4

Action [3, )] = r4                  Action [3, )] = r4

Similarly, fill up the Action table by reduce (r) entries.

Filling of goto Entries − goto Table can be filled for the non-terminals using Rule (3) of construction of Parsing Table.

Since,             I1 = goto(I0, E)

Applying Rule (3)

goto[0, E] = 1

Write 1 in front of Row 0 and Column E.

Similarly,          goto (I0, T) = I2

Applying Rule (3)

goto [0, T] = 2

Write 2 in front of Row 0 and Column T.

Similarly, we can fill other entries in goto Table

Filling of "accept" entries

Applying Rule (2C), there is a production.

E′ → E ∙ in I1.

Therefore, write "accept" in front of Row 1 and Column $.

∴ action [1, $] = "accept. "

By filling all shift, reduce, accept, goto entries, we get the following SLR parsing table.

In table s means shift, r means to reduce.

raja
Updated on 02-Nov-2021 10:51:02

Advertisements