Search code examples
antlrgrammardslxtext

Correct use of Syntactic Predicates in XText


I have a grammar with some ambuguities I need to resolve. One of the rules takes the following form:

TArg:
    anys=Anys
    | rnumb1=PNumb ".." (rnumb2=PNumb)?
;

Or this image, if you prefer

The rule Anys has the potential to start with a PNumb. I can see where the ambiguity is, but how to I tell XText to take the second path if it sees a PNumb followed by the double dot?

Presumably, if I use

TArg:
    (=>  rnumb1=PNumb ".." (rnumb2=PNumb)?)
    |anys=Anys
;

Then it will always choose the first if it sees a number, regargless of if it sees the "..", and I will run into problems.

What is the correct usage/placement of the syntactic predicate here to allow Antlr to look ahead to see if the ".." is present?

Cheers in advance.


Solution

  • You need to also include the '..'

    TArg:
      =>(rnumb1=PNumb "..") (rnumb2=PNumb)?
      | anys=Anys
    ;