tags:

views:

29

answers:

1

Hi all, I am trying to use semantic predicates in ANTLR for the following grammar rule

test[n]
       :({n==0}? => ~('a'))
       |({n==1}? => ~('b'))
       |({n==2}? => ~('c'))
       ;

However, ANTLR does not let me define the grammar in such a way, requiring that at least one of the alternatives be default. The exact error displayed is a reference error, for the parameter n.

My aim is that I want one, and only one of the alternative to be visible at any given instant of time. Any ideas as to how to go about doing this?

Thanks!

A: 

That can't be right: you forgot to specify what type n is, so compiling a generated parser will not work. It should be:

test[int n]
  :  ({n==0}? => ~('a'))
  |  ({n==1}? => ~('b'))
  |  ({n==2}? => ~('c'))
  ;

When I create a small grammar with the rule above, I get no error however.

Can you post a complete grammar and test input string that produces this error? Can you also copy and paste the exact error message?

Bart Kiers
Hi, Thanks for the reply.
Gunner4Life
In addition to the solution above, there was also a problem of context-sensitive follow sets, as the parameter was being treated as local variable. I am not using a scope to achieve the same, and it seems to be working fine. Thanks!
Gunner4Life
@Gunner4Life, you're welcome.
Bart Kiers