Frage

I need a Xtext grammar rule (or multiple) working similar to the following:

1: CollectionGetElement:
2:   val=[VariableReference] '='
3:   (ref=[List] | ref=[Bytefield] | ref=[Map])
4:   '[' keys+=GetElementKeyType ']' ('[' keys+=GetElementKeyType ']')* ';';

5: GetElementKeyType:
6:   key=[VariableReference] | INT | STRING;

Like this unfortuantely it doesn't work, because of the 3 line!

I also tried 3 seperated rules (for: map, list and bytefield), but then It's difficult (impossible) for the parser to recognize the correct rule.

ListGetElement:
  val=[VariableReference] '='
  ref=[List]
  '[' key+=GetElementKeyType ']' ('[' key+=GetElementKeyType ']')* ';';
... same for the others

Error then is:

Decision can match input such as "RULE_ID '=' RULE_ID '[' RULE_ID ']' '[' RULE_ID ']' ';'" using multiple alternatives: 5, 6
The following alternatives can never be matched: 6

What's the best way to achive this?

War es hilfreich?

Lösung

there are two problems in your grammar,

  1. assigning 3 different types to attribute 'ref'
  2. generating 3 different types by parsing some ID

I am not sure what do you want to do. But, I can give you an example. Hope it can help you.

e.g.

List:
  'list' '(' elements += Element * ')';
Map:
  'map' '(' pairs += Pair * ')';
GeneralDataType:
  List | Map
CollectionGetElement:
    val=[VariableReference] '='
    ref = GeneralDataType
;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top