Question

I have written following grammar

Model:
package = PackageDec?
greetings+=Greeting*
usage+=Usage* ;

PackageDec:
   'package' name=QualifiedName ;

Greeting:
'greet' name=ID '{' ops += Operation* '}' ;

Operation:
'op' name=ID ('(' ')' '{' '}')? ;

QualifiedName:
ID ('.' ID)*;

Usage: 
   'use';

With above i can write following script.

package p1.p2
  greet G1 {op f1 op f2 }

Now i need to write something like this:

  package p1.p2
  greet G1 {op f1 op f2 op f3}
  use p1.p2.G1.f1
  use p1.p2.G1
  use p1.p2.G1.f3

To support that i changed Usage RULE like this

Usage:
'use' head=[Greet|QualifiedName] =>('.' tail=[Operation])?

However when i generate xtext artifacts it is complaining about multiple alternatives.

Please let me know how to write correct grammar rule for this.

Was it helpful?

Solution

This is because QualifiedName consumes dots (.). Adding ('.' ...)? makes two alternatives. Consider input

a.b.c

This could be parsed as

  • head="a" tail = "b.c"
  • head="a.b" tail = "c"

If I understand your intention of using predicate => right, than you just have to replace

head=[Greet|QualifiedName]

with

head=[Greet]

In this case however you will not be able to parse references with dots.

As a solution I would recommend to substitute your dot with some other character. For example with colon:

Usage:
'use' head=[Greet|QualifiedName] (':' tail=[Operation])?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top