尝试在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')+;
您的ID
模式与INT
或STRING
匹配的所有内容都匹配,因此它们无关紧要。我认为这不是您想要的。
ID
不应与以数字开头的令牌匹配; 42
不是标识符。并且您的注释暗示STRING
应该是字符串文字('hello'
),但您的词法模式不会尝试匹配'
。