[antlr-interest] newbie question

Barry Kelly barry.j.kelly at gmail.com
Tue Oct 26 08:31:33 PDT 2004


I've removed the actions and some of the parentheses to improve
readability here:

> class CSVParser extends Parser;
> options { k=1; }
> line 
>        : (record )+ NEWLINE 
>        | EOF
>        ;
> 
> record
>        : RECORD (COMMA)?
>        | COMMA
>        ;

You'll see that, when reading in multiple records on a line, there are
two ways to parse a comma. It could be eaten by

1) the first alternative of the record rule, 

or

2) it could be ignored until the next iteration of the (record)+ part
of the line rule and read as the second alternative of the record
rule.

One way to get rid of this is to use options { greedy = true; } just
before the (COMMA)? part of the record rule. Another way would be to
rewrite line as something like:

line 
       : (RECORD)? (COMMA (RECORD)?)* NEWLINE 
       | EOF
       ;
 
-- Barry


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





More information about the antlr-interest mailing list