Xtext语法:不匹配的输入' 0'期待RULE_INT

时间:2015-03-15 10:53:34

标签: grammar dsl xtext ecore

我是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

我有三次这个错误:

输入'0'不匹配,期待RULE_INT 期望RULE_INT的输入'1'不匹配 不匹配的输入'5'期待RULE_INT

我无法看到这些错误来自哪里,我该怎么做才能解决它们。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

lexing和解析是不同的步骤。因此没有使用无关紧要。并且你的语法变得模糊(看看生成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)?;

答案 1 :(得分:0)

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