0

I have some problem in transforming the following non LL(1) grammar into LL(1) grammar. Is it possible to be transformed?

> A ::= B | A ; B 

> B ::= C | [ A ]

> C ::= D | C , D

> D ::= x | (C)

where ;, x, (, ), [,] are terminals.

backtrack
  • 7,996
  • 5
  • 52
  • 99
jeffi95
  • 1
  • 1

1 Answers1

0

The main problems here are the productions

A → A ; B

and

C → C, D

which are left-recursive. In both cases, these productions will generate a string of objects separated by some kind of delimeter (semicolon in the first case, comma in the second), so you can rewrite them like this:

A → B ; A

C → D, C

This gives the grammar

A → B | B; A

B → C | [A]

C → D | D, C

D → x | (C)

The problem now is that there are productions for A and C that have a common prefix. But that's nothing to worry about: we can left-factor them like this:

A → B H

H → ε | ; A

B → C | [A]

C → D I

I → ε | C

D → x | (C)

I believe that this grammar is now LL(1).

Community
  • 1
  • 1
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065