I have created an example grammar TestGrammar.g4 to illustrate my problem:
grammar TestGrammar;
Member_of : 'MEMBER OF';
Identifier : [a-z]+
;
WS: [ \n\t\r]+ -> channel(HIDDEN);
parseSimpleExpression : (Identifier '.')+ Identifier
;
collection_member_expression : parseSimpleExpression Member_of parseSimpleExpression
;
Following code shows the invocation of the parser:
String expression = "x.a MEMBER OF y.a";
TestGrammarLexer l = new TestGrammarLexer(new ANTLRInputStream(expression));
CommonTokenStream tokens = new CommonTokenStream(l);
TestGrammarParser p = new TestGrammarParser(tokens);
p.setErrorHandler(new BailErrorStrategy());
ParserRuleContext ctx = p.parseSimpleExpression();
My expectation is that ANTLR throws a syntax error on the input x.a MEMBER OF y.a
, however
it does not. Instead it consumes only part of the input (x.a MEMBER OF
) and finishes successfully.
Now, when I remove the last rule from the grammar, ANTLR throws a syntax error as expected. I don't understand this behaviour since the last grammar rule should not even be involved in the parsing since it is neither directly nor indirectly referenced by the start rule.