简体   繁体   中英

Automatic grammar transformation for left-factoring; and left-recursion removal

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM