1

Here is my grammar 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 'altasder table;'

grammar Hello;

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

how can i achieve it???

I have used the DefualtError stategy but still its not wotking

import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.misc.IntervalSet;

public class CustomeErrorHandler extends DefaultErrorStrategy {


        @Override
        public void recover(Parser recognizer, RecognitionException e) {
        // TODO Auto-generated method stub
        super.recover(recognizer, e);


            TokenStream tokenStream = (TokenStream)recognizer.getInputStream();


            if (tokenStream.LA(1) == HelloParser.SEMICOLON  )
            {

                IntervalSet intervalSet = getErrorRecoverySet(recognizer);

                tokenStream.consume();

                consumeUntil(recognizer, intervalSet);
            }
        }
    }

main class : public class Main {

public static void main(String[] args) throws IOException {
    ANTLRInputStream ip = new ANTLRInputStream("altasdere table ; alter table ;");
    HelloLexer lex = new HelloLexer(ip); 
    CommonTokenStream token = new CommonTokenStream(lex);
    HelloParser parser = new HelloParser(token);
    parser.setErrorHandler(new CustomeErrorHandler());
    System.out.println(parser.start().toStringTree(parser));

}

}

myoutput :

line 1:0 token recognition error at: 'alta'
line 1:4 token recognition error at: 's'
line 1:5 token recognition error at: 'd'
line 1:6 token recognition error at: 'e'
line 1:7 token recognition error at: 'r'
line 1:8 token recognition error at: 'e'
line 1:9 token recognition error at: ' '
(start compilation)

why its not moving to second command ?

Adib Rajiwate
  • 405
  • 4
  • 19

1 Answers1

1

Need to use the DefaultErrorStrategy to control how the parser behaves in response to recognition errors. Extend as necessary, modifying the #recover method, to consume tokens up to the desired parsing restart point in the token stream.

A naive implementation of #recover would be:

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

Adjust the while condition as necessary to identify the next valid point to resume recognition.

Note, the error messages are due to the lexer being unable to match extraneous input characters. To remove the error messages, add as the last lexer rule:

ERR_TOKEN : . ;
GRosenberg
  • 5,843
  • 2
  • 19
  • 23
  • hi @GRosenberg thanks for the reply i have used the DefaultErrorStrategy but still i cant get the correct result i have edited with the error strategy class plz help. – Adib Rajiwate Oct 12 '17 at 05:57
  • thanks @GRosenberg i have added the code as you said but the control is not going at recover method i have given input as "altasdfer table ;alter table ;" but getting output as line 1:0 token recognition error at: 'alta' line 1:4 token recognition error at: 's' line 1:5 token recognition error at: 'd' line 1:6 token recognition error at: 'f' line 1:7 token recognition error at: 'e' line 1:8 token recognition error at: 'r' line 1:9 token recognition error at: ' ' (start compilation) please help i am stuck over it. – Adib Rajiwate Oct 13 '17 at 05:52
  • @GRosenberg when using recover method for query : altasder table; alter table ; It consumes token till first semicolon (;). It does not process the second statement which is correct. Can you plz guide – haripcce Oct 26 '17 at 11:56
  • @GRosenberg after procesing the error statment till semicolon, it should start from the first rule again.How to achive – haripcce Oct 26 '17 at 12:09