I have a little interrogation regarding my grammar. I want to parse strings, for example:
"(ICOM LIKE '%bridge%' or ICOM LIKE '%Munich%')"
I ended up with the following grammar (a little harder than I need):
// Guidance when parsing the full generated BQS request
grammar Logic;
options {
output=AST;
}
tokens {
NOT_LIKE;
}
parse
: expression EOF -> expression
;
expression
: query
;
query
: term (OR^ term)*
;
term
: factor (AND^ factor)*
;
factor
: (notexp -> notexp) ( NOT LIKE e=notexp -> ^(NOT_LIKE $factor $e))?
;
notexp
: NOT^ like
| like
;
like
: atom (LIKE^ atom)*
;
atom
: ID
| | '(' expression ')' -> expression
;
LIKE : 'like' | 'LIKE';
OR : 'or' | 'OR';
AND : 'and' | 'AND';
NOT : 'not' | 'NOT';
CONSTANT_EXPRESSION : DATE | NUMBER | QUOTED_STRING;
ID : (CHARACTER|DIGIT)+;
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; } ;
fragment DATE : '\'' YEAR '/' MONTH '/' DAY (' ' HOUR ':' MINUTE ':' SECOND)? '\'';
fragment QUOTED_STRING : '\'' (CHARACTER)+ '\'' ;
fragment CHARACTER : ('a'..'z' | 'A'..'Z'|'.'|'\''|'%');
fragment DIGIT : '0'..'9' ;
fragment DIGIT_SEQ :(DIGIT)+;
fragment DEL : SPACE ',' SPACE ;
fragment NUMBER : (SIGN)? DIGIT_SEQ ('.' (DIGIT_SEQ)?)?;
fragment SIGN : '+' | '-';
fragment YEAR : DIGIT DIGIT DIGIT DIGIT;
fragment MONTH : DIGIT DIGIT;
fragment DAY : DIGIT DIGIT;
fragment HOUR : DIGIT DIGIT;
fragment MINUTE : DIGIT DIGIT;
fragment SECOND : DIGIT (DIGIT)? ('.' (DIGIT)+)?;
fragment SPACE : (' ')?;
There is a thing, I have this message when creating AST:
line 1:11 no viable alternative at input ''%bridge%''
line 1:35 no viable alternative at input ''%Munich%''
Although the generated tree is true (as far as I know):

So, can someone give me a hint about what's wrong there? I think the character contains all the extra characters needed to match this expression.,
Thank!
As usual, some Java code for quick grammar testing:
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;
public class Main {
public static void main(String[] args) throws Exception {
String src = "(ICOM LIKE '%bridge%' or ICOM LIKE '%Munich%')";
LogicLexer lexer = new LogicLexer(new ANTLRStringStream(src));
LogicParser parser = new LogicParser(new CommonTokenStream(lexer));
CommonTree tree = (CommonTree)parser.parse().getTree();
DOTTreeGenerator gen = new DOTTreeGenerator();
StringTemplate st = gen.toDOT(tree);
System.out.println(st);
}
}
source
share