Antlr:“在输入中没有可行的替代方案' start 001 vs 003;”

发布于 2025-01-29 07:46:00 字数 480 浏览 3 评论 0原文

我正在与Antlr和Python进行一些工作,并遇到了此错误消息“ 1:0”第1行,在输入中没有可行的替代方案'开始001 vs 003'”,但不知道该怎么办或如何修复它。


start: expr | <EOF>;

expr
        : 'start' duration=DURATION 'fight' player_1=FILE 'vs' player_2=FILE #durationfightExpr
        ;

FILE : ('A'..'Z'|'a'..'z'|'0'..'9'|':'|'\\'|'/'|' '|'-'|'_'|'.')+ ;
DURATION : ('0' .. '9') + ('.' ('0' .. '9') +)?;
WS : [ \r\n\t]+ -> skip;

“错误消息”

I am doing some work with Antlr and Python and have come across this error message "line 1:0 no viable alternative at input 'start 001 vs 003'" but have no idea what to do with it or how to fix it.


start: expr | <EOF>;

expr
        : 'start' duration=DURATION 'fight' player_1=FILE 'vs' player_2=FILE #durationfightExpr
        ;

FILE : ('A'..'Z'|'a'..'z'|'0'..'9'|':'|'\\'|'/'|' '|'-'|'_'|'.')+ ;
DURATION : ('0' .. '9') + ('.' ('0' .. '9') +)?;
WS : [ \r\n\t]+ -> skip;

Error message

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书信已泛黄 2025-02-05 07:46:00

对于输入开始001 vs 003,Lexer仅创建1 <代码>文件令牌。您可能需要从file中排除白色空间字符:

FILE : ('A'..'Z'|'a'..'z'|'0'..'9'|':'|'\\'|'/'|'-'|'_'|'.')+ ;

但是随后遇到了创建以下令牌的问题:

'start'              `start`
FILE                 `001`
'vs'                 `vs`
FILE                 `003`

ie 001003文件令牌。因为文件也可以匹配此输入,并且由于它是在 持续时间之前放置,因此int/double的输入将始终成为>文件。但是,如果您这些规则:

DURATION : ('0' .. '9') + ('.' ('0' .. '9') +)?;
FILE : ('A'..'Z'|'a'..'z'|'0'..'9'|':'|'\\'|'/'|'-'|'_'|'.')+ ;

然后输入001永远不会成为file> file令牌(也许对您来说还可以,我不知道)。请注意,这些规则可以像这样更简洁:

DURATION : [0-9]+ ('.' [0-9]+)?;
FILE : [A-Za-z0-9:\\/\-_.]+;

For the input start 001 vs 003, the lexer creates just 1 FILE token. You'll probably want to exclude the white space char from FILE:

FILE : ('A'..'Z'|'a'..'z'|'0'..'9'|':'|'\\'|'/'|'-'|'_'|'.')+ ;

But then you run into the problem that the following tokens are created:

'start'              `start`
FILE                 `001`
'vs'                 `vs`
FILE                 `003`

I.e. both 001 and 003 are FILE tokens. Because FILE can match this input too, and because it is placed before DURATION, int/double kind of input will always become a FILE. But if you these rules:

DURATION : ('0' .. '9') + ('.' ('0' .. '9') +)?;
FILE : ('A'..'Z'|'a'..'z'|'0'..'9'|':'|'\\'|'/'|'-'|'_'|'.')+ ;

then input like 001 will never become a FILE token (perhaps that's OK for you, I don't know). Note that these rules can be written more succinctly like this:

DURATION : [0-9]+ ('.' [0-9]+)?;
FILE : [A-Za-z0-9:\\/\-_.]+;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文