O'Reilly logo

The Definitive ANTLR 4 Reference, 2nd Edition by Terence Parr

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Parade of Errors

The best way to describe ANTLR’s error recovery strategy is to watch an ANTLR-generated parser respond to erroneous input. Let’s look at a grammar for a simple Java-like language containing class definitions with field and method members. The methods have simple statements and expressions. We’ll use it as the core of the examples in this section and the remainder of the chapter.

errors/Simple.g4
 
grammar​ Simple;
 
 
prog: classDef+ ; ​// match one or more class definitions
 
 
classDef
 
: ​'class'​ ID ​'{'​ member+ ​'}'​ ​// a class has one or more members
 
{System.out.println(​"class "​+$ID.text);}
 
;
 
 
member
 
: ​'int'​ ID ​';'​ ​// field definition
 
{System.out.println(​"var "​+$ID.text);}
 
| ​'int'​ f=ID ​'(' ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required