Antlr4部分匹配但不报错
这是我的antlr4语法:
grammar TestExpr;
prog: stat ;
stat: expr
;
expr : expr '|' expr #orJoin
| expr '&' expr #andJoin
| '(' expr ')' #nested
| KEY '=' value #kv
;
value: KEY | VALUE;
KEY : [a-zA-Z] [a-zA-Z0-9_-]* ;
VALUE: [a-zA-Z0-9] [a-zA-Z0-9._-]* ;
WS : [ \t]+ -> skip ; // toss out whitespace
如果输入“a233=A(”,则只能匹配“a233=A”。我以为它会报错,但没有。
here is my antlr4 grammar:
grammar TestExpr;
prog: stat ;
stat: expr
;
expr : expr '|' expr #orJoin
| expr '&' expr #andJoin
| '(' expr ')' #nested
| KEY '=' value #kv
;
value: KEY | VALUE;
KEY : [a-zA-Z] [a-zA-Z0-9_-]* ;
VALUE: [a-zA-Z0-9] [a-zA-Z0-9._-]* ;
WS : [ \t]+ -> skip ; // toss out whitespace
If "a233=A(" is entered, only "a233=A" can be matched. I expected it to report an error, but it didn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的
prog
规则更改为:ANTLR 默认情况下将匹配与您的语法匹配的最长有效字符序列。这也意味着当某些东西停止“工作”时,如果到此为止的所有内容都是有效的(即使存在更多输入但使解析处于不完整状态),它将退出解析。
通过添加
EOF
规则,您的“规则”是它必须匹配所有输入(包括EOF
),这样您就不会遇到此问题。因此,最好的做法是使用任何规则来解析所有以 EOF 标记结尾的输入。change your
prog
rule to be:ANTLR, by default, will match the longest valid sequence of characters that match your grammar. This also means it will quit parsing when something ceases to "work", if everything up to that point is valid (even if more input is present but leaves the parse in an incomplete state).
By adding the
EOF
rule, you're saying that your "rule" is that it must match ALL the input (including theEOF
) so you won't encounter this problem. Because of this it's considered a best practice to have any rules that should parse all input end with anEOF
token.