以下标记定义永远无法匹配,因为先前的标记匹配相同的输入:INT,STRING

问题描述 投票:0回答:1

尝试在antlr上执行简单的语法。它应该解析输入,例如L = [1,2,hello]。但是,antlr会产生此错误:以下令牌定义永远无法匹配,因为先前的令牌与相同的输入匹配:INT,STRING。有帮助吗?

  grammar List;
    decl: ID '=[' Inside1 ']'; // Declaration of a List. Example : L=[1,'hello']
    Inside1: (INT|STRING) Inside2| ; // First element in the List. Could be nothing
    Inside2:',' (INT|STRING) Inside2 | ; //

    ID:('0'..'Z')+;
    INT:('0'..'9')+;
    STRING:('a'..'Z')+;

编辑:更新的语法。该错误仅包含ID。

grammar List;
decl: STRING '=[' Inside1 ']'; // Declaration of a List. Example : L=[1,'hello']
Inside1: (INT|'"'STRING'"') Inside2| ; // First element in the List. Could be nothing
Inside2:',' (INT|'"'STRING'"') Inside2 | ; //

STRING:('A'..'Z')+;
INT:('0'..'9')+;
parsing token antlr
1个回答
1
投票

您的ID模式与INTSTRING匹配的所有内容都匹配,因此它们无关紧要。我认为这不是您想要的。

ID不应与以数字开头的令牌匹配; 42不是标识符。并且您的注释暗示STRING应该是字符串文字('hello'),但您的词法模式不会尝试匹配'

© www.soinside.com 2019 - 2024. All rights reserved.