[antlr-interest] Newbie question: How to tell a parse failed orsucceeded

Putrycz, Erik Erik.Putrycz at nrc-cnrc.gc.ca
Thu Sep 13 18:18:57 PDT 2007


Well lots of different things. I found a link on the antlr website for
gunit which is a grammar unit test. It will allow you to verify that
fragments you parse result in what you expect.
I have been writing lots of java unit tests and I have a small framework
for verifying my grammar.
In my unit tests, I test a rule with a correct input, verify that there
are no tokens left (see Terence answer to my post recently on how to do
this) and then I have some simple tests on the AST generated (e.g. count
of nodes and types of some specific nodes). 

Erik
 
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of smerzlia at optusnet.com.au
> Sent: Thursday, September 13, 2007 8:00 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Newbie question: How to tell a parse failed
> orsucceeded
> 
> Hi,
> I have built a grammar and I can get it to work using the various
> samples on the web (thanks to all!). The only problem i have is
> that the following code does not throw ANY exceptions on failed
> lexing or parsing (my grammar file is basic and identical to a lot
> of other samples) :
> 
> public static void main(String[] args)
> {
>   try
>   {
>      ANTLRStringStream fs = new ANTLRStringStream("weight[3,2] =
> Xsqrt(cos(12) + cos(12 - 34));"); //THIS IS WRONG FUNC NAME
>      dumb2Lexer lex = new dumb2Lexer(fs);
>      TokenRewriteStream tokens = new TokenRewriteStream(lex);
>      dumb2Parser g = new dumb2Parser(tokens);
> 
>      atom_return ar = g.atom();
>      statement_return sr = g.statement();   //correct start
> expression - no exceptions are ever thrown
>   }
>   catch (RecognitionException e)
>   {
>      e.printStackTrace(); // <----------- NEVER REACHED
>   }
> }
> 
> Also : I have added the following to grammar file (with no effect):
> 
> @lexer::members
> {
> @Override
> public void reportError(RecognitionException e)
> {
>    // do nothing - i could set my own success member flag here, but
would
> like see if there is any standard approach first
> }   }
> 
> There is no API on parser which registers failure or success that I
> can see ( dumb2Parser .failed member is always false). I have
> searched news groups and web to no avail.
> 
> So how can I SIMPLY (using ANTLR API, not my own code) tell whether a
> lexical scan + parse succeeded  or failed ?
> 
> 
> Serge


More information about the antlr-interest mailing list