简体   繁体   中英

XText cross referencing

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.

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

a.b.c

This could be parsed as

  • head="a" tail = "bc"
  • head="ab" 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])?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM