Consuming Error tokens in antlr4

22 views
Skip to first unread message

Harichandra Harmalkar

unread,
Oct 26, 2017, 7:26:05 AM10/26/17
to antlr-discussion
 i have following grammar in antlr4
grammar Hello;

start : compilation;
compilation : sql*;
sql : altercommand;
altercommand : ALTER TABLE SEMICOLON;
ALTER: 'alter';
TABLE: 'table';
SEMICOLON : ';';


 i am trying to give input as

alter table ;

everything works fine but when i give

altasder table; alter table ;

it gives me an error on first string as expected but i want is to parse the second command ignoring the first statement 'altasder table;'


So i will have one error statement :

altasder table;

and 


one correct statment :

alter table;


I have tried using default error strategy


public void recover(Parser recognizer, RecognitionException e) {
  
        int ttype = recognizer.getInputStream().LA(1);
        while (ttype != Token.EOF && ttype != HelloParser.SEMICOLON) {
            recognizer.consume();
            ttype = recognizer.getInputStream().LA(1);
        }
    
}


This skips the first query statement till first semicolon found.

But it does not process the second statement.


Can some one help me on this. I don't want to handle the errors in another error rule. Please guide.


 

Reply all
Reply to author
Forward
0 new messages