Xtext 语法:不匹配的输入“0”应为 RULE_INT

Xtext grammar : mismatched input '0' expecting RULE_INT

我是 Xtext 的新手,我正在尝试为铁路系统创建一个简单的 DSL,这是我的语法:

grammar org.xtext.railway.RailWay with org.eclipse.xtext.common.Terminals

generate railWay "http://www.xtext.org/railway/RailWay"

Model:
    (trains+=Train)*
    | (paths+=Path)*
    | (sections+=Section)*
;

Train:
    'Train' name=ID ':'
    'Path'  path=[Path]
    'Speed' speed=INT
    'end'
;

Path:
    'Path'      name=ID ':'
    'Sections'  ('{' sections+=[Section] (',' sections+=[Section] )+ '}' ) | sections+=[Section]
    'end'
;

Section:
    'Section'   name=ID ':'
    'Start'     start=INT
    'End'       end=INT
    ('SpeedMax' speedMax=INT)?
    'end'
;

但是当我将此代码放在 Eclipse 实例中时:

Section brestStBrieux :
    Start 0
    End 5
end

Section StBrieuxLeMan :
    Start 5
    End 10
end

Section leManParis :
    Start 1
    End 12
end

Path brestParis :
    Sections  { brestStBrieux, StBrieuxLeMan, leManParis}
end

Train tgv :
    Path  brestParis
    Speed  23
end

这个错误我出现了 3 次:

不匹配的输入“0”预期 RULE_INT 不匹配的输入 '1' 期望 RULE_INT 不匹配的输入 '5' 期望 RULE_INT

我看不出这些错误是从哪里来的,我能做些什么来修复它们。有什么想法吗?

词法分析和解析是不同的步骤。因此没有使用无关紧要。并且你的语法变得模棱两可(在生成 lang 时查看警告)你应该把它变成一个数据类型规则(简单地省略终端关键字)

=> 将语法更改为

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

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

Model:
    (trains+=Train)*
    | (paths+=Path)*
    | (sections+=Section)*
;

Train:
    'Train' name=ID ':'
    'Path'  path=[Path]
    'Speed' speed=INT
    'end'
;

Path:
    'Path'      name=ID ':'
    'Sections'  ('{' sections+=[Section] (',' sections+=[Section] )+ '}' ) | sections+=[Section]
    'end'
;

Section:
    'Section'   name=ID ':'
    'Start'     start=INT
    'End'       end=INT
    ('SpeedMax' speedMax=INT)?
    'end'
;

FLOAT : '-'? INT ('.' INT)?;

Christian 是对的,由于不再定义 FLOAT 终端,原来的问题就解决了。无论如何,剩下的问题是规则

Path:
    'Path'      name=ID ':'
    'Sections'  ('{' sections+=[Section] (',' sections+=[Section] )+ '}' ) | sections+=[Section]
    'end'
;

当前具有此优先级:

Path:
    (
       'Path' name=ID ':' 'Sections'
       ('{' sections+=[Section] (',' sections+=[Section] )+ '}' )
    ) 
    |
    (sections+=[Section] 'end')
;

您可能想将其重写为

Path:
    'Path'      name=ID ':'
    'Sections'  
    ( 
       ('{' sections+=[Section] (',' sections+=[Section] )+ '}' )
    |  sections+=[Section]
    ) 'end'
;