[antlr-interest] validating semantic predicates
Ari Steinberg
ari.steinberg at embarcadero-ca.com
Wed Jun 18 11:53:16 PDT 2003
I had a similar problem in my grammar. To get around it, I caught the semantic exception and then throw a new one with a better error message. My parser has a reference to the lexer so I created getLine() and getColumn() functions which return the current line and column the lexer so that the exception returns valid data.
myRule:
...
;
exception
catch[SemanticException ex]
{
throw SemanticException( "Syntax error in Explain statement", getFilename(), getLine(), getColumn());
}
Cheers,
Ari
-----Original Message-----
From: Matt Benson [mailto:gudnabrsam at yahoo.com]
Sent: Wednesday, June 18, 2003 2:45 PM
To: Antlr List
Subject: [antlr-interest] validating semantic predicates
I have Tooled my grammar and it has generated a
parser. I have written the grammar to process the
different elements of the file by calling methods on a
registered implementation of a handler object. Thus,
I am currently concerned only with content. I am
providing the handler a Stack of relevant tokens in
case something farther away than the current context
is needed. So every time I enter a rule, I push the
primary token onto the stack (remember I'm dealing
with XML so this is pretty straightforward), and pop
it before I return from the rule. I figure while I'm
doing the pop, I might as well do a kind of sanity
check by way of a validating semantic predicate:
{t == tokenStack.pop()}?
One thing I noticed right away is that the
SemanticException thrown in the event that this is not
true contains the exact text. I don't see in the docs
whether and how to customize this text; I am currently
doing this:
{t == tokenStack.pop() /*unexpected token on the
stack*/}?
which is good enough, but I just wondered if there was
a better way.
TIA,
Matt
__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com
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