[antlr-interest] check validity of single rule
Philippe Faes
philippe.faes at gmail.com
Mon Dec 31 02:55:52 PST 2007
Dear Johannes,
Thanks for your response.
I am indeed checking rules buried deep in the grammar, and I want to
test individual generation rules.
I have a very large grammar, and it would be painful to create at
foobarTest rule for each existing rule foobar. It's great that antlr
does graceful recovery after errors, but there must be a way to check
if a string confoms to a rule, right?
I plan on checking a lot of generation rules with a lot of examples,
so it should be as simple as possible.
thanks
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071231/8462092c/attachment.html
More information about the antlr-interest
mailing list