3

Standard methods are readily available to transform a context-free grammar which is not LL(1) into an equivalent grammar which is. Are there any tools available which can automate this process?

In the examples below I use upper-case lettering for non-terminals, and lower-case for terminals.

The following left-recursive non-terminal:

A  -> A a | b

can be transformed into a right-recursive form:

A  -> b A'
A' -> NIL | a A'

Note though that left-recursive production rules ensure that expressions associate to the left, and similarly for right recursive productions; and so a grammar modification will also change expression associativity.

Another issue is indirect left-recursion, such as the following:

A -> B a
B -> A b

Left-factoring is also used to ensure that only one look-ahead token is required by the parser. The following production must look ahead by two tokens:

A  -> a b | a c

This can also be refactored; to:

A  -> a (b | c)

Are there any software tools which can automate these grammar transformations; and so produce an equivalent grammar suitable for a LL(1) parser?

user2023370
  • 10,488
  • 6
  • 50
  • 83

1 Answers1

1

The Haskell grammar-combinators library here allows a grammar to be transformed into a non-left-recursive form. The input grammar must though be a parsing expression grammar.

Community
  • 1
  • 1
user2023370
  • 10,488
  • 6
  • 50
  • 83