[antlr-interest] Re: exception thrown in syntatic predicate c an't be caught!
martinkbraid
mbraid at sqlworks.com
Mon Apr 14 19:12:52 PDT 2003
It's tricky. A stmt could be invalid because there are (a) invalid
characters; (b) it is syntactically invalid (eg wrong sequence of
tokens), or (c) it is semantically invalid (eg things stmt refers to
doesn't exist). If it's invalid for any reason I want to skip to the
next stmt. Trouble is, if the lexer skips to the next stmt (ie
semicolon), the parser won't see the trailing invalid tokens and
there is a possibility the initial subset of valid tokens are
syntactically and semantically valid (ie I will have silently
truncated the stmt - not good).
The problem would be simplified if the filter=rule option didn't
necessarily require the rule to be protected, that way I could always
let the parser deal with invalid tokens. I'm basically coming round
to the idea I need a lexer rule that captures everything that isn't
lexacaly(?) valid (using the ~) and let the parser deal with these
tokens which will not fit any rule and correctly signify a
syntactcally invalid stmt.
Thanks for your help.
Martin
PS. I'm parsing user written stmts, so you never know what's coming
down the pike.
--- In antlr-interest at yahoogroups.com, mzukowski at y... wrote:
> Why do you have garbage characters in statements?
>
> You could handle this in your lexer too, by consuming characters
until you
> see a SEMICOLON, but you also have to handle comments and strings
properly.
> Then when you throw that exception, the next thing the parser sees
should
> always be a SEMICOLON.
>
> Monty
>
> -----Original Message-----
> From: martinkbraid [mailto:mbraid at s...]
> Sent: Monday, April 14, 2003 3:08 PM
> To: antlr-interest at yahoogroups.com
> Subject: [antlr-interest] Re: exception thrown in syntatic predicate
> can't be caught!
>
>
> The input to my lexer/parser is a series of statements delimited by
> semicolons. I was using the lexer exception to tell the parser to
> skip over the current statement and consumeUntil() the next
semicolon
> (ie skip over the remainder of the stmt). Since I posted my message
I
> made some progress by using the filter=rule option in the lexer and
> have that rule automatically throw a TokenStreamException which my
> parser captures. That works ... to a point. When the parser gets
that
> exception it does a consumeUntil(SEMICOLON), but if there is a 2nd
> garabage character the lexer throws another exception and the whole
> thing becomes a mess.
>
> Think I'll heed your advice and look at controlling the lexer more
> carefully.
>
>
> --- In antlr-interest at yahoogroups.com, mzukowski at y... wrote:
> > Syntactic predicates work by trying a piece of syntax and then
> checking if
> > an exception was thrown. When it finds a predicate that works it
> then calls
> > the parser for real, with actions turned back on. Syntactic
> predicates
> > won't work if you can catch their exceptions. Catch the
exception
> after the
> > predicate has decide where to go.
> >
> > Or just catch and handle the exception in the lexer. What
exactly
> are you
> > doing with the exceptions anyhow?
> >
> > Monty
> >
> > -----Original Message-----
> > From: martinkbraid [mailto:mbraid at s...]
> > Sent: Sunday, April 13, 2003 7:07 PM
> > To: antlr-interest at yahoogroups.com
> > Subject: [antlr-interest] exception thrown in syntatic predicate
> can't
> > be caught!
> >
> >
> > Because I need to be able to handle garbage characters passed to
my
> > parser/lexer, I'm doing my own error processing in a top level
rule
> > by catching ANTLRExceptions.
> >
> > By chance, during syntactic predicate processing, the lexer
> > encounters a garbage character and throws a TokenStreamException.
> But
> > because inputState.guessing > 0 (because it was in a syntactic
> > predicate), my exception handler does NOT get control and the
> > exception passes out of the parser. What do I do? It would be
> > impossible to avoid syntatic predicates?
> >
> > Thanks, Martin Braid
> >
> >
> >
> >
> > Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/
>
>
>
>
> Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list