[antlr-interest] Performing an action based on alternative result

Martin Probst mail at martin-probst.com
Wed Sep 14 23:32:14 PDT 2005


Hi,

> foobar :
> (
> 	t:"function" |
> 	t:"subroutine" |
> 	t:"program"
> )
> { doSomethingWith(t) ; }
> 
> But ANTLR gives me an error "Label 't' has already been defined". Is
> there an easy way to do this so that I don't have to specify different
> label for each alternative and the same action for each as well?

Not as far as I know. The standard way of doing this is:

foobar { AST t = null }:
	t1:"function" { t = t1; } |
	t2:"subroutine" { t = t2; } |
	...

Albeit the ability to re-declare AST identifiers would be nice to have,
this is a very common annoyance. It can also help you avoid nasty errors
though ...

Martin



More information about the antlr-interest mailing list