This is the first time I am working with ANTLR for a project at Imperial College London and until now it has been really useful. I have already defined a simple recursive grammar as follows
grammar Hello;
execution: workflow;
workflow : Task
| workflow OPERATOR workflow
|'(' workflow OPERATOR workflow ')'
|'(' workflow OPERATOR workflow ')' (OPERATOR workflow)*
;
Task : 'T' ('0'..'9')+ | 'WF' ('0'..'9')+;
OPERATOR: 'AND' | 'OR' | 'XOR' |';' ;
WS : [ \t\n\r]+ -> channel(HIDDEN) ;
to evaluate strings like :
T6 ; (T4 AND T7) ; T5 ; ( (WF23 OR WF2) OR (T3 AND WF4) AND T4) AND T5 OR T11
and it works perfectly, my problem comes when I try to evaluate an incorrect string like
T6 ; (T4 AND T7) ; T5 ; ( (WF23 OR WF2) OR (T3 AND WF4) AND T4) AND (T5;OR() T2)
according to my rules after the last AND the string "(T5;OR() T2) " is not valid since it doesn't fits with my grammar definition, but when testing this I get the tree for the string
T6 ; (T4 AND T7) ; T5 ; ( (WF23 OR WF2) OR (T3 AND WF4) AND T4)
and the last part "(T5;OR() T2) " which is incorrect is simply ignored.
My question is what am I missing, what should I do to get and error saying that "(T5;OR() T2) " doesn't fits with my grammar definition, can somebody knows??
Thanks a lot
The lexer happily tokenizes your malformed input, and you gave the parser the instruction to parse a (valid) execution
. This is what it did. If you want to force the parser to consume the entire token stream, place an EOF
at the end of the entry point of your grammar:
execution: workflow EOF;
If you now parse your input:
String source = "T6 ; (T4 AND T7) ; T5 ; ( (WF23 OR WF2) OR (T3 AND WF4) AND T4) AND (T5;OR() T2)";
HelloLexer lexer = new HelloLexer(new ANTLRInputStream(source));
HelloParser parser = new HelloParser(new CommonTokenStream(lexer));
parser.execution();
You will get the following output:
line 1:72 no viable alternative at input '(T4 AND T7) ; T5 ; ( (WF23 OR WF2) OR (T3 AND WF4) AND T4) AND (T5;OR' line 1:15 extraneous input ')' expecting {, OPERATOR} ...