[antlr-interest] Parser acessing lexer rules info for error recovery

Peggy Fieland madcapmaggie at yahoo.com
Wed Dec 7 10:13:36 PST 2005


assuming you meant 

  class somelexer extends lexer ...

in your parser you can say:
  if (LA(1) == T_DATATYPE) ...
or 
  if (LT(1).getText() == "FOO" ...

If you actually have to check something against the
keyword tokens you'll
need to be a bit trickier.


--- Thiago Silva <thiago.silva at kdemail.net> wrote:

> Yeah, thats something I've already done. But, still,
> I have to keep the 
> function up to date. Not that this is hard (they are
> all simple rules, at 
> most), but it's not good having to replicate
> something I've already defined 
> elsewhere. 
> 
> I was wondering if wouldn't be possible to acess the
> lexer and ask it if a 
> given token matches a given rule. For me, it seems
> natural to be able to do 
> such thing. But, so far, looking at the sources, it
> doesn't seem possible. 
> 
> thanks for your reply,
> Thiago
> 
> On Wednesday 07 December 2005 16:50, you wrote:
> > If you need to make the same test in multiple 
> catch
> > blocks, you can
> > write a function to do it and just call it in all
> the
> > catch blocks.
> >
> > --- Thiago Silva <thiago.silva at kdemail.net> wrote:
> > > Hello,
> > > I'm having a problem on parser error recovery.
> Some
> > > times I need lexer rules
> > > info to proceed with the recovery. But I'm not
> so
> > > sure the way to proceed,
> > > after reading the manual, the generated sources
> and
> > > antlr sources.
> > >
> > > As a simple example:
> > >
> > > ----------------------
> > > class SomeParser extends Parser
> > >
> > > somerule : (....);
> > >
> > > exception
> > > catch[...] {
> > >    //here I need to check, for instance, if
> LA(1) is
> > > a T_DATATYPE
> > >   //or if LA(1) belongs to the token section
> > > (testLiteralsTable()?)
> > > }
> > >
> > >
> > > class SomeParser extends Lexer;
> > >
> > > T_DATATYPE : T_FOO | T_BAR
> > >
> > > T_FOO: "foo";
> > > T_BAR: "bar"
> > > ----------------------
> > >
> > > Now, what I'm doing is checking the LA in the
> catch
> > > block, manually writing
> > > (again) the members of T_DATATYPE:
> > >
> > > catch[..]
> > >  if(la_token == T_FOO | la_token == T_BAR) {
> > >    //print a warning message
> > >  } else {
> > >    //print a different warning message
> > >  }
> > >
> > >
> > > So, if T_DATATYPE changes, I would have to
> update
> > > all the catch[] blocks that
> > > checks for T_DATATYPE.
> > >
> > > Did I miss something in the docs? Or is this the
> > > only way possible to do it?
> > > By the way, I'm using C++.
> > >
> > > Thanks in advance,
> > > Thiago
> 



More information about the antlr-interest mailing list