[antlr-interest] Matching the * character

Johannes Luber jaluber at gmx.de
Thu Mar 27 16:59:05 PDT 2008


Steve O'Hara schrieb:
> I'm new to Antlr so forgive me if this is a stupid question...
> I'm trying to match a database search criteria that looks like the 
> following;
> 
> * *
> 
> *'some text'*, *'more criteria' & 'more criteria'**
> 
> 
> My grammar is;
> 
> * *
> 
> expression
> 
>     : subExpression (WS? (COMMA | AMPERSAND) WS? subExpression)*
> 
>     ;
> 
>  
> 
> subExpression
> 
>     : WS? (Identifier | subExpressionText) WS?
> 
>     ;
> 
>  
> 
> subExpressionText
> 
>     :  (STAR | QUESTION*)? QuotedString  (STAR | QUESTION*)? WS?
> 
>     ;
> 
>  
> 
> Letter : 'a'..'z' | 'A'..'Z' | '_' | '\u0080'..'\ufffe' ;
> 
> Digit : '0'..'9' ;
> 
> Identifier : Letter (Letter | Digit)* ;
> 
> QuotedString :'\'' (~'\'')* '\'' ;
> 
> WS : (' ' | '\t' | '\n' | '\r')+ ;
> 
>  
> 
> COMMA : ',' ;
> 
> AMPERSAND : '&' ;
> 
> QUESTION : '?' ;
> 
> STAR : '*' ;
> 
> 
> The parser finds the first quoted string (some text) but fails after 
> that with a NoViableAltException. If I change the * to ? it works fine. 
> I'm waiting for delivery of the reference manual so maybe I'm breaking 
> some sort of golden rule here, but any help would be greatly appreciated.
> 
> Steve

For rules like Letter use the following:

fragment LETTER : 'a'..'z' | 'A'..'Z' | '_' | '\u0080'..'\ufffe' ;

Otherwise I don't see another mistake by scanning alone.

Johannes


More information about the antlr-interest mailing list