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]
;

コメントで説明しました。基本的に、整数のルールは 2 つのルールで構成されています: int_ruleandintegerと ルールで両方を使用したいですassignment。ただし、Xtext では 1 つのルールしか参照できず、name機能はルールの 1 つの名前インスタンスのみを参照できますが、例のように同じルール内の複数の名前インスタンスは参照できません。両方のルールの両方の情報が本当に必要ですが、一方しか参照できません。

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 に答える