[antlr-interest] Stupid languages, and parsing them

Sam Barnett-Cormack s.barnett-cormack at lancaster.ac.uk
Sat Apr 11 11:53:18 PDT 2009


Sam Barnett-Cormack wrote:
> Hi all,
> 
> In my ongoing project, I need to parse a really crazy structure that 
> wants to change the lexing rules dependent on syntactic factors. I hate 
> this.
> 
<snip>
 >
> 1) Use member variables to track if the most recent non-WS, non-comment 
> token was WITH, SYNTAX, and { (a sort of look-behind implemented 
> kludgily by putting an action in *every* rule, or by overruling the emit 
> stuff to keep track of the last 2 things on the DEFAULT channel), use 
> these to switch into crazy-mode where much is different.
> 
<snip>
> 
> Anyone got any thoughts? Any ideas which would be less pain? Is there 
> already some way of tracking recently-emitted token on a specific channel?

Further to this, would it be sensible to override Token's "Token emit()" 
method (java runtime) to call super.emit() to "listen in" on what has 
been emitted, check its channel, and add it to the "lookbehind" if it is 
on the default channel? Have a little finite stack of recent tokens to 
use to decide to enter the "weird" state, and then a boolean member 
variable to remember that I'm in that state (and use in gated 
predicates). Does that sound remotely sane?

Sam


More information about the antlr-interest mailing list