Recognizing Multiple Language Dialects

For our first lesson, we’re going to learn how to use semantic predicates to deactivate parts of a Java grammar. The effect will be to recognize different dialects, according to results of evaluating Boolean expressions on-the-fly. In particular, we’re going to see how the same parser can switch between allowing and disallowing enumerated types.

The Java language has been extended over the years to include new constructs. For example, prior to Java 5, the following declaration was invalid:

 enum​ Temp { HOT, COLD }

Rather than building separate compilers for the slightly different dialects, the Java compiler javac has a -source option. Here’s what happens when we try to compile ...

Get The Definitive ANTLR 4 Reference, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.