[antlr-interest] token context information

Pete Forman pete.forman at westerngeco.com
Fri Jun 18 08:25:20 PDT 2004


At 2004-06-18 07:59 -0700, Mr Torsten Curdt wrote:
> > If not then it may be better for the
> > lexer to only return LITERALs and to add actions
> > like setType(KEY) and setType(VALUE) in the parser rules.
> > The parser should run checks
> > for valid keys and values over and above the
> > constraints of literal.
>
>That's interesting! Can I still use the ANTLR
>machinery to check whether the LITERAL is a KEY
>or a VALUE?

Sure.  At its simplest

pair: k:LITERAL COLON v:LITERAL
   {#k.setType(KEY); #v:setType(VALUE);};

> > A question to guide your choice is what happens when
> > you read
> >
> >    foo : bar
> >
> > where foo is okay as a literal but fails to meet the
> > rules for a key.
> > Can that be interpreted as some other grammatical
> > construct or is that
> > an error that should issue a diagnostic and bail
> > out?
>
>Well, it should give me a VALUE COLON VALUE instead
>of a KEY COLON VALUE and the parser should bail out.
>
>Or what do you mean?

In the above rewrite the first action clause.

if (isKey(#k.getText()) {
   #k.setType(KEY);
} else {
   handleError();
}

My question was whether VALUE COLON VALUE might mean, say, assign
values if it turned out that the first VALUE was not a key.  But you
say that you want to bail out, so the question is answered.  Were you
to go the other way then a semantic predicate would have allowed you
to handle the grammar.

The example code above is untested.

-- 
Pete Forman                -./\.-  Disclaimer: This post is originated
WesternGeco                  -./\.-   by myself and does not represent
pete.forman at westerngeco.com    -./\.-   opinion of Schlumberger, Baker
http://petef.port5.com           -./\.-   Hughes or their divisions.



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list