避免 Xtext 多个替代警告

Avoid Xtext multiple alternatives warnings

在我的 xtext dsl 中,我定义了以下规则:

Port returns tdg::Port:
    'port'
    'kind' kind=Kind
    'type' type=Type;

enum Kind returns tdg::PortKind:
    In='in' | Out='out';

enum Type returns tdg::PortType:
    Numeric | String | Boolean | Tuple;

tdg 类 来自一个 ecore 模型。 我在编译 dsl 语法时收到以下警告。我怎样才能避免它们?

warning(200): ../com.isax.testdatagen.dsl/src-gen/com/isax/testdatagen/parser/antlr/internal/InternalTdgDSL.g:250:1: Decision can match input such as "'port' 'kind' 'in' 'type' 'String'" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input warning(200): ../com.isax.testdatagen.dsl/src-gen/com/isax/testdatagen/parser/antlr/internal/InternalTdgDSL.g:250:1: Decision can match input such as "'port' 'kind' 'in' 'type' 'Boolean'" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input warning(200): ../com.isax.testdatagen.dsl/src-gen/com/isax/testdatagen/parser/antlr/internal/InternalTdgDSL.g:250:1: Decision can match input such as "'port' 'kind' 'in' 'type' 'Tuple'" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input warning(200): ../com.isax.testdatagen.dsl/src-gen/com/isax/testdatagen/parser/antlr/internal/InternalTdgDSL.g:250:1: Decision can match input such as "'port' 'kind' 'in' 'type' 'Numeric'" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input warning(200): ../com.isax.testdatagen.dsl.ui/src-gen/com/isax/testdatagen/ui/contentassist/antlr/internal/InternalTdgDSL.g:854:30: Decision can match input such as "'port' 'kind' 'in' 'type' 'Numeric'" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input warning(200): ../com.isax.testdatagen.dsl.ui/src-gen/com/isax/testdatagen/ui/contentassist/antlr/internal/InternalTdgDSL.g:854:30: Decision can match input such as "'port' 'kind' 'in' 'type' 'String'" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input warning(200): ../com.isax.testdatagen.dsl.ui/src-gen/com/isax/testdatagen/ui/contentassist/antlr/internal/InternalTdgDSL.g:854:30: Decision can match input such as "'port' 'kind' 'in' 'type' 'Boolean'" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input warning(200): ../com.isax.testdatagen.dsl.ui/src-gen/com/isax/testdatagen/ui/contentassist/antlr/internal/InternalTdgDSL.g:854:30: Decision can match input such as "'port' 'kind' 'in' 'type' 'Tuple'" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input

这种 "multiple alternatives" 警告可能是由使用两个 "paths" 备选方案可达到的规则引起的,例如

Model:
    Rule1a | Rule1b;

Rule1a:
    Rule2 | Rule1b;

Rule1b:
    {Rule1b} 'rule1b';

Rule2: 
    {Rule2} 'rule2';

这里,rule1b 可以使用两条路径从规则 "Model" 到达:直接从 "Model" 和间接通过 "Rule1a"。

正如克里斯蒂安所说,您的错误消息不可能是由您列出的规则引起的。如果你的语法只包含 Model: ports+=Port*; 除了这些,也许你生成的语法与你认为的不同?

无论如何,要找到问题的原因,您必须确定具有不明确实例的规则:打开 com.isax.testdatagen.dsl/src-gen/com/isax/testdatagen/parser/antlr/internal/InternalTdgDSL.g 第 250 行(如日志中打印的那样)并找出它属于哪个规则到。在我的示例中,错误在于 Antlr 规则 ruleModel,它对应于 Xtext 规则 Model