无法生成语法的Xtext工件,该语法一次使用括号和交叉引用

时间:2019-01-04 17:57:53

标签: dsl xtext

我正在尝试根据这种语法生成DSL:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Program:
     "print" expression=Expression "where" constant=Constant  | 
     "print" expression=Expression;

Expression:
    Add;

Add returns Expression:
    Primary({Add.expression1=current} "+"  expression2=Primary)*;

Primary returns Expression:
    ExpressionParentheses | Number | ConstUsage;

Number returns Expression:
    value=INT;

Constant:
    name=ID "=" number=Number;

ConstUsage returns Expression:
    name=[Constant];

ExpressionParentheses returns Expression: 
    "(" Add ")";

但是在Eclipse中生成Xtext工件总是会产生错误。它总是在我同时在主规则中使用ExpressionParentheses和ConstUsage时发生。当我在其中放置ConstUsage或ExpressionParentheses时,一切正常。我的语法可能是什么问题?

1 个答案:

答案 0 :(得分:0)

以下语法效果很好

Program:
     "print" expression=Expression ("where" constant=Constant)?
;

Expression:
    Add;

Add returns Expression:
    Primary({Add.expression1=current} "+"  expression2=Primary)*;

Primary returns Expression:
    Number | ConstUsage | "("Add")";

Number returns Expression:
    value=INT;

Constant:
    name=ID "=" number=Number;

ConstUsage returns Expression:
    name=[Constant];