[antlr-interest] How to skip to end of line on error?

Rick Schumeyer rschumeyer at gmail.com
Sat Nov 28 17:20:22 PST 2009


I've read the section on error reporting and recovery from "the book" but
still can't figure out what may be a simple problem.

I want to parse a file that consists of bibliographic entries.  Each entry
is on one line (so each record ends with \n).

If a record does not match, I just want to print an error message, and skip
to the end of line and start again with the next record.

If I understand chapter 10 correctly, then '\n' should be in the
resynchronization set, and the parser will consume tokens until it finds
one.

This isn't happening.  Once I get an error, the parser never recovers.  I
get a bunch of NoViableAlt exceptions.  I'm hoping someone can explain what
I'm doing wrong.

Here is a sample input file.  The 1st and 3rd lines are ok, the 2nd line is
an error.

Name. "Title," Periodical, 2005, v41(3,Oct), 217-240.
Name. "Title," Periodical, 2005, v41(3,Oct), Article 2.
Name. "Title," Periodical, 2005, v41(3,Oct), 217-240.

Here is the grammar:

grammar Periodical;

article_list
    :    (article NL)* article NL?
    ;

article
    :    a=authors PERIOD SPACE QUOTE t=title COMMA QUOTE SPACE j=journal
COMMA SPACE y=year COMMA SPACE v=volume COMMA SPACE p=pages PERIOD SPACE*
    ;

authors    :    (~QUOTE)+;

title    :    (~QUOTE)+;

journal    :    (LETTER|SPACE|COMMA|DASH)+;

volume    :    (LETTER|DIGIT)+
    |    (LETTER|DIGIT)+ '(' (LETTER|DIGIT|SLASH|COMMA)+ ')'
    ;

year    :    DIGIT DIGIT DIGIT DIGIT;

pages    :    DIGIT+ DASH DIGIT+;



PERIOD    :    '.';
QUOTE    :    '"';
COMMA    :    ',';
SPACE    :    ' ';
DIGIT    :    '0'..'9';
LETTER  :    ('a'..'z')|('A'..'Z');
DASH    :    '-';
SLASH    :    '/';
NL    :    '\r'? '\n';
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091128/e5154136/attachment.html 


More information about the antlr-interest mailing list