[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: stack for lexer states?



On Wed, Dec 11, 2002 at 04:44:53PM -0500, Jay Sachs wrote:
>   {a -> b} t1 = 'foo';
>   {c -> b} t3 = 'erp';
>   {b -> $pop$} t2 = 'bar';


How about declaring:

States
  a, b, c, pop;

Tokens
   {a} t1 = 'foo';
   {c} t3 = 'erp';
   {b -> pop} t2 = 'bar';


Then using a custom lexer overriding the filter() method, which would
implement your push/pop operations? Here's the pseudo-code:

void filter()
{
if (state == a && token instanceof TT1)
{
  push_state (state);
  state = b;
}
elseif (state == c && token instanceof TT3)
{
  push_state (state);                                                                                                                                                                             
  state = b;
}
elseif (state == pop)
{
  state = pop_state();
}
}


Custom lexers/parsers are very powerful tools, if you know how to take
advantage of them. :-)

Have fun!

Etienne
-- 
Etienne M. Gagnon                    http://www.info.uqam.ca/~egagnon/
SableVM:                                       http://www.sablevm.org/
SableCC:                                       http://www.sablecc.org/