[antlr-interest] Sync lexer/parser state?

Andrew Bell andrew.bell.ia at gmail.com
Mon Apr 3 14:48:53 PDT 2006


Hi,

I am trying to extract the input text that matched an parser rule. 
For instance:

rule : subrule
  {
  // I want the text that matched subrule here.
  }
  ;

I know that I can return a string from subrule, but if subrule has
lots of other complicated subrules, this method is a real pain -- you
have to build up the text at each step.

What I'm trying to do is something like:

rule :
  {
  lexer.mark();
  }
  subrule
  {
  stringIWant = lexer.getMarkedChars();
  lexer.commit();
  }

You can do this with the lexer, but the lexer state is not necessarily
synced with the parser state -- sometimes there are characters in the
lexer input buffer that haven't been "consumed" by the parser at the
time that mark() is called, so the "wrong" position is marked.  Is
there any way that I can force the parser to sync the lexer to its
current state so that something like the above will work?

Thanks for any thoughts,

--
Andrew Bell
andrew.bell.ia at gmail.com


More information about the antlr-interest mailing list