I am trying to develop a new language with Antlr. Here is my grammar file :
grammar test;
program : vr'.' to'.' e
;
e: be
| be'.' top'.' be
;
be: 'fg'
| 'fs'
| 'mc'
;
to: 'n'
| 'a'
| 'ev'
;
vr: 'er'
| 'fp'
;
top: 'b'
| 'af'
;
Whitespace : [ \t\r\n]+ ->skip
;
Main.java
String expression = "fp.n.fss";
//String expression = "fp.n.fs.fs";
ANTLRInputStream input = new ANTLRInputStream(expression);
testLexer lexer = new testLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
testParser parser = new testParser(tokens);
//remove listener and add listener does not work
ParseTree parseTree = parser.program();
Everything is good for valid sentences. But I want to catch unrecognized tokens and invalid sentences in order to return meaningful messages. Here are two test cases for my problem.
fp.n.fss => anltr gives this error token recognition error at: 's' but i could not handle this error. There are same example error handler class which use BaseErrorListener but in my case it does not work.
fp.n.fs.fs => this sentence is invalid for my grammar but i could not catch. How can i catch invalidations like this sentence?