2

I am rewriting this post(same question though, less words).

I rewrote my grammar to just have at the top level this

statement: SELECT EOF!;
#removed all other rules and tokens and still not working
SELECT  :   ('S'|'s')('E'|'e')('L'|'l')('E'|'e')('C'|'c')('T'|'t');

I then have the following java code(There is no parse method on my Parser like I see on everyone else's posts :( :( )...

@Test
public void testGrammar() throws RecognitionException {
    String sql = "select p FROM TABLE p left join p.security s where p.numShares = :shares and s.securityType = :type";

    ANTLRStringStream stream = new ANTLRStringStream(sql);
    NoSqlLexer lexer = new NoSqlLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    NoSqlParser parser = new NoSqlParser(tokenStream);

    try {
        CommonTree tree = (CommonTree) parser.statement().getTree();
        Assert.fail("should fail parsing");
    } catch(Exception e) {
        log.info("Exception", e);
    }
}

I should get an Exception on parsing but I do not :( :(. This is a very simple grammar with an EOF but it just is not working. Any ideas why? Am I calling the wrong parsing statement?

NOTE: I am trying to make a much bigger problem simpler by cutting the problem down to this simple thing which seems not to work.

full (very small) grammar file is here

grammar NoSql;


options { 
    output = AST; 
    language = Java;
}

tokens {


}

@header {
package com.alvazan.orm.parser.antlr;
}

@lexer::header {
package com.alvazan.orm.parser.antlr;
}


@rulecatch {
    catch (RecognitionException e)
    {
        throw e;
    }
}

statement: SELECT EOF!;

SELECT  :   ('S'|'s')('E'|'e')('L'|'l')('E'|'e')('C'|'c')('T'|'t');

WS  :
    (   ' '
        |   '\t'
        |   '\r'
        |   '\n'
        )+
        { $channel=HIDDEN; }
        ;  

ESC :
    '\\' ('"'|'\''|'\\')
    ;

thanks, Dean

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212

1 Answers1

2

I should get an Exception on parsing but I do not :( :(. This is a very simple grammar with an EOF but it just is not working.

If you didn't "tell" ANTLR to throw an exception, then ANTLR might not throw an exception/error in case of invalid input. In many cases, ANTLR tries to recover from mismatched/invalid input and tries to continue parsing. You will see output being written on the stderr in your case, but you might not get an exception.

If you do want an exception in your case, see: ANTLR not throwing errors on invalid input

Community
  • 1
  • 1
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • interesting, I thought that was what the rulecatch was for but that must be in another part of the generated code then I guess. thanks!!! – Dean Hiller Aug 21 '12 at 14:06