After Antlr4 recognizes error, how to ask the application to automatically fix it?

41 views
Skip to first unread message

Lin Zhang

unread,
Jun 18, 2017, 4:01:04 PM6/18/17
to antlr-discussion

We know Antlr4 is using the sync-and-return recovery mechanism. For example, I have the following simple grammar:


grammar Hello;
r  : prefix body ;
prefix: 'hello' ':';
body: INT ID ;
INT: [0-9]+ ;
ID : [a-z]+ ; 
WS : [ \t\r\n]+ -> skip ;
 

I use the following listener to grab the input:


public class HelloLoader extends HelloBaseListener {
    String input;
    public void exitR(HelloParser.RContext ctx) {
        input = ctx.getText();
    }
}
 

The main method in my HelloRunner looks like this:


public static void main(String[] args) throws IOException {
    CharStream input = CharStreams.fromStream(System.in);
    HelloLexer lexer = new HelloLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    HelloParser parser = new HelloParser(tokens);
    ParseTree tree = parser.r();
    ParseTreeWalker walker = new ParseTreeWalker();
    HelloLoader loader = new HelloLoader();
    walker.walk(loader, tree);
    System.out.println(loader.input); 
}
 

Now if I enter a correct input "hello : 1 morning", I will get hello:1morninghello:1morning, as expected.


What if an incorrect input "hello ; 1 morning"? I will get the following output:


line 1:6 token recognition error at: ';'
line 1:8 missing ':' at '1'
hello<missing ':'>1morning
 

It seems that Antlr4 automatically recognized a wrong token ";" and delete it; however, it will not smartly add ":" in the corresponding place, but just claim <missing ':'>.


My question is: is there some way to solve this problem so that when Antlr found an error it will automatically fix it? How to achieve this coding? Do we need other tools?

Eric Vergnaud

unread,
Jun 20, 2017, 12:48:13 PM6/20/17
to antlr-discussion
You can install your own error handler, and produce the desired behaviour.
Antlr did not 'delete' the token, it simply ignored it.
Antlr also skipped the missing token, but it will not 'insert' it.
Antlr can't do that for you because depending on the error, you might have different strategies: ask the user, auto-fix...
Reply all
Reply to author
Forward
0 new messages