Chapter 4:Syntax Analysis

Example

Consider the grammar

E' E

E E + T | T

T T * F | F

F ( E ) | id

If I is { E' .E } then closure(I) is

E' .E

E .E + T

E .T

T .T * F

T .F

F .id

F .(E)

Consider the example described here. Here I contains the LR(0) item E' .E . We seek further input which can be reduced to E. Now, we will add all the productions with E on the LHS. Here, such productions are E .E+T and E .T. Considering these two productions, we will need to add more productions which can reduce the input to E and T respectively. Since we have already added the productions for E, we will need those for T. Here these will be T .T+F and T .F. Now we will have to add productions for F, viz. F .id and F .(E).