[antlr-interest] check validity of single rule

Johannes Luber jaluber at gmx.de
Sun Dec 30 10:35:29 PST 2007


Philippe Faes schrieb:
> Hi all,
> 
> I want to write tests for my grammar, but I can't figure out how to get
> the correct feedback from ANTLR.
> 
> e.g. I want this to pass:
> createParserFromString("a:=b;").assignment();
> 
> but this should fail:
> createParserFromString("a:=b").assignment(); // no semicolon
> createParserFromString("a:=b; #$%").assignment(); // junk after statement
> 
> From the return value I can see if an 'assignment' is matched from the
> text, but I cannot detect if there is junk after the valid assignment.
> 
> Any hints?

Do you use EOF at the end of an assignment? In case, assignment is
deeply buried in the grammar, I suppose that you aren't testing for the
right thing. assignment may be responsible for only one statement. So my
advice is to test several statements in a row at the appropriate rule.
Or you could create:

assignmentTest: assignment EOF;

which should reject example 2.

Johannes


More information about the antlr-interest mailing list