2
votes

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!

1

1 Answers

1
votes

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?