[antlr-interest] Look-ahead problem parsing phrase?

Sean O'Dell sean at celsoft.com
Sun Jun 28 10:54:39 PDT 2009


Hi everyone,

I'm new to the mailing list and am just getting starting with ANTLR (day 2)
and I've run into an issue I'm having some trouble wrapping my head around.
I think it's related to how ANTLR looks-ahead to predict tokens, but I think
I'm overlaying my familiarity with regular expressions onto ANTLR in a way
that is clouding up my understanding of what ANTLR does and needs in order
to do this right.

I'm trying to parse out a collection of "words" on a single line as a
"phrase", ignoring whitespace at the beginning and end of the lines, but
I'm getting an error while parsing what I think is a line that should match
the grammar.

My grammar:

    grammar Phrase;

    WS : (' '|'\t')+;
    DIGIT : ('0'..'9');
    LETTER : ('a'..'z'|'A'..'Z');
    NEWLINE : '\r'? '\n';

    WORD : (LETTER|DIGIT)+;

    EOL : WS? NEWLINE?;

    PHRASE : WORD (WS WORD)*;

    line : WS? PHRASE EOL?;

The line of text I am parsing (note whitespace at the ends): " This is a
phrase "

Error I get during parse:

    line 1:18 required (...)+ loop did not match anything at character
'<EOF>'
    line 0:-1 missing PHRASE at '<EOF>'

What might be causing this error, and what might be a good, clean way to
parse out the "phrase" in the input text?

Sean O'Dell
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090628/a49b1473/attachment.html 


More information about the antlr-interest mailing list