Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixed grammar example

...

Now to the three real tools against non-determinism. The first is left-factoring. Look at the following rule:

Code Block
a : L+ bK
   | L+ cM
   ;

As you can see, the two L at the beginning prevent that ANTLR can decide which path is the right path. As a consequence, ANTLR excludes the second one. Left-factoring can solve this. The name of this technique stems from the fact that you factor out the common part, which is always on the left side:

Code Block
a : L+ (bK | cM)
  ;

Left-factoring can be done even between several rules. The starting point is then slightly different:

Code Block
a : b
   | c
   ;


b : L+ K
   ;

c : L+ M
   ;

are turned into

Code Block
a : L+ (b | c)
   ;

b : K
   ;

c : M
   ;

...