【问题标题】:Xtext grammar : mismatched input '0' expecting RULE_INTXtext 语法:不匹配的输入 '0' 期望 RULE_INT
【发布时间】:2015-03-17 07:55:53
【问题描述】:

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

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

【问题讨论】:

  • 您确定您发布了语法的所有相关部分吗?确定没有与 INT 终端规则重叠的关键字或终端规则?
  • 我有这个终端规则,但我还没有使用它:终端 FLOAT:'-'? INT ('.' INT)?;

标签: grammar dsl xtext ecore


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    词法分析和解析是不同的步骤。因此不使用没关系。并且您的语法变得模棱两可(查看生成语言时的警告)您应该将其转换为数据类型规则(只需省略终端关键字)

    => 将你的语法改为

    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)?;
    

    【讨论】:

    • 没有很好地理解你的答案,我在生成工件时有这两个警告:warning(200): ../org.xtext.railway/src-gen/org/xtext/railway/ parser/antlr/internal/InternalRailWay.g:80:1:决策可以使用多个替代项匹配诸如“EOF”之类的输入:1、2、3 因此,该输入警告禁用了替代项 2,3( 200):../org.xtext.railway.ui/src-gen/org/xtext/railway/ui/contentassist/antlr/internal/InternalRailWay.g:176:1:决策可以匹配输入,例如“EOF”使用多个备选方案:1、2、3 结果,该输入的备选方案 2,3 被禁用
    • 这不起作用,我仍然有同样的错误。我删除了终端规则 FLOAT 但 INT 似乎仍然不起作用
    • 请分享您的完整语法。不要忘记重新生成并重新启动运行时应用
    猜你喜欢
    • 1970-01-01
    • 2022-06-21
    • 2022-01-05
    • 1970-01-01
    • 2022-11-10
    • 2013-10-04
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多