我有这样的语法:
StateMachine:
declarations+=Declaration*;
Declaration:
Transition |
State;
Transition returns Declaration:
"trans" label=ID ":" source=[State] "->" target=[State] ";" ;
State returns Declaration:
"state" id=ID ";" ;
@Override
terminal WS:
(' ' | '\t' | '\n' | '\r')+;
@Override
terminal ID:
( 'a' .. 'z' | 'A' .. 'Z' ) ( 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' )* ;
在转换规则中,当我尝试使用ref to State类型错误时,总是抛出“找不到State的类型”。当我不使用[]时,所以不是作为交叉引用,一切都很好。我该如何解决这种情况呢?这种语法会有什么问题呢?
发布于 2019-01-26 12:13:39
错误在这一行中:
"trans" label=ID ":" source=[State] "->" target=[State] ";" ;
在Xtext中,[Foo]
意味着“对Foo
类型实例的交叉引用”。Is并不意味着“对语法规则的引用”。由于下面这行代码,Xtext不会生成State
类型:
State returns Declaration:
其中returns Declaration
表示“规则State
将返回类型Declaration
”,因此不需要类型State
。
下面的语法可以解决这个问题:
StateMachine:
declarations+=Declaration*;
Declaration:
Transition |
State;
Transition:
"trans" label=ID ":" source=[State] "->" target=[State] ";" ;
State:
"state" id=ID ";" ;
在这里,Xtext将为Declaration
、Transition
和State
生成类型,其中Transition
和State
派生自Declaration
。
https://stackoverflow.com/questions/54377601
复制