ANTLR 匹配输入末尾
我试图将我的语法与整个字符串相匹配,如果它无法消耗整个输入,则会出错。基本上,这个伪正则表达式:
\whitespace* [a-zA-Z]+ [-]? [a-zA-Z]+ \whitespace* $
根据 this,EOF 应该可以工作。因此,请考虑以下语法:
start : CHARS EOF
;
CHARS : ('a'..'z')+
;
如果我在此代码中将输入设置为 "hello"
:
var ss = new Antlr.Runtime.ANTLRStringStream(input);
var lex = new autocompleteLexer(ss);
var tokens = new CommonTokenStream(lex);
var parser = new autocompleteParser(tokens);
return (BaseTree)(parser.start().Tree);
我会得到一个带有两个子项的 AST,HELLO
和 EOF
,正如预期的那样。但是,如果我将输入设置为“hello#”,它会返回相同的树,并且根本不会抱怨哈希值。我不确定我做错了什么。
I'm trying to match my grammar to an entire string, and have it error out if it cannot consume the entire input. Basically, this pseudo regex:
\whitespace* [a-zA-Z]+ [-]? [a-zA-Z]+ \whitespace* $
According to this, EOF should work. So, consider this grammar:
start : CHARS EOF
;
CHARS : ('a'..'z')+
;
If I set input to "hello"
in this code:
var ss = new Antlr.Runtime.ANTLRStringStream(input);
var lex = new autocompleteLexer(ss);
var tokens = new CommonTokenStream(lex);
var parser = new autocompleteParser(tokens);
return (BaseTree)(parser.start().Tree);
I get an AST with two children, HELLO
and EOF
, as expected. But if I set the input to say "hello#"
, it gives back the same tree, and doesn't complain about the hash at all. I'm not sure what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了问题,那就是异常被词法分析器捕获,而不是传播到我的代码中。因此,通过添加以下内容:
我现在得到了适当的例外,而不是让它们被吞掉。
I found the problem, it was that exceptions were getting trapped by the Lexer instead of propagating into my code. So, by adding this:
I now get the proper exceptions, instead of having them be swallowed.