[antlr-interest] Different rules for same char

Vivi vivipham85 at yahoo.com
Tue Sep 18 12:09:14 PDT 2012


I have the input as:
This & bird is user for if(dog & cat)
I want to separate them as 3 tokens as:
This
Bird is used for
If(dog & cat)

But i have a grammar file that produces the following tokens:
This
Bird is used for
If(dog
Cat)
Please help me to fix the following grammar definition:
grammar CFG;

line returns [List<String> result]
@init {
    result = new ArrayList<String>();
}
    : (NEWLINE) => NEWLINE
    | (
        fieldResult=field { result.add(fieldResult); }
        (  BREAK fieldResult=field {result.add(fieldResult);} )*    
        NEWLINE
      )
    ;
 
field returns [String parsedItem]
@init {
    parsedItem = "";
}
    : f=FIELD {$parsedItem=$f.text;}
    | // nothing
    ;
    
 NEWLINE :   '\r'? '\n';
 BREAK :    '&';
 
 FIELD:  NONBREAKING+;
 
// Anything except a line-breaking character or break character is allowed.
fragment NONBREAKING
    :   ~('\r' | '\n' | BREAK );

Thanks,

Tp



More information about the antlr-interest mailing list