1

这是示例规则(只需将其替换为 MyDsl 语法):

Start:
    elem += (integer)*
;   

int_rule:
    'int' (name += integer ('=' values += INT)?) (',' name+=integer ('=' values += INT)?)* ';'
;

/* I have to put the rule name as "integer", so when users hover
 * on variable names, they see exactly type "integer". This is a bit
 * adhoc, but it's acceptable for the time being. However, using this method
 * If some other rules refer to "integer", it can only either retrieve the name
 * in this "integer" rule or its
 */
integer:
    name = ID
;

/*
 * Example: assignment like num1 = 2, num2 = 3.... the variable name of type
 * integer can't be referred, since I have to either refer to "int_rule" rule to
 * retrieve its value or "integer" to retrieve its name. I can't get both.
 */
assignment:
    name = [integer] // or name = [int_rule]
;

我在评论中解释了。基本上,整数规则由两个规则组成:int_rule并且integer我想在规则中使用这两个assignment规则。但是,Xtext 只能让我引用一个规则,并且该name功能只能引用规则的一个名称实例,而不能像示例中那样在同一规则中引用多个名称实例。我真的需要这两个规则中的两个信息,但我只能参考其中一个。

4

1 回答 1

3

I would suggest a different design for your problem: define the terms variable, reference and value in your grammar. A variable is only a definition - a place where you can present the available type information. Where you want to use this variable, you have to use a variable reference - when evaluating the code described in your language, you have to find what variable it refers to - Xtext helps this by connecting your reference on the EMF level. Finally, values can be constants and variable references - use them accordingly in your grammar.

As an example, look something as follows (it is not tested in Xtext, so minor error might be present):

Variable:
  (type = 'int')? //Optional type definition - you could use any type here
  name = ID
  ('=' initialValue = Value)? //Optional initial declaration;

Value:
  Integer | VariableReference;

Integer:
  value = int;

VariableReference:
  referredVariable = [Variable];

Assignment:
  'let' lhs = [VariableReference] '=' rhs = [Value];

I hope, this is helpful this way - or if I have misunderstood your problem, please clarify, and I will try to update my answer.

于 2012-09-05T20:29:54.760 回答