Chapter 4:Syntax Analysis

Dangling else problem again

Dangling else problem can be handled by left factoring

stmt if expr then stmt else stmt

| if expr then stmt

can be transformed to

stmt if expr then stmt S'

S' else stmt | ε

We can also take care of the dangling else problem by left factoring. This can be done by left factoring the original grammar thus transforming it to the left factored form.

stmt if expr then stmt else stmt

| if expr then stmt

is transformed to

stmt if expr then stmt S'

S' else stmt | ε