简体   繁体   English

即使输入有额外的令牌,ANTLR 3.5.2也会匹配规则

[英]ANTLR 3.5.2 matches rule even though input has extra tokens

I am writing a grammar to parse sql statements. 我正在编写语法来解析sql语句。 I have the following rule: 我有以下规则:

show_databases :
    SHOW DATABASES { System.out.println("Showing databases");    
;

When my input is show databases , I get the message. 当我的输入是show databases ,我得到了消息。 However, when my input is show databases now , I DO see the message. 但是,当我输入的是show databases now ,我确实看到了消息。 I am building a REPL and all of the lines end with ; 我正在建立一个REPL,所有的行都以结尾; . I want to get an error since the syntax is wrong. 我想得到一个错误,因为语法错误。 Any ideas? 有任何想法吗?

Match the end of input as well: 还要匹配输入的结尾:

SHOW DATABASES ';'

or 要么

SHOW DATABASES EOF

The way you have it, parser has no idea there cannot be "now" later as part of another statement. 拥有解析器的方式,解析器不知道以后再也不能作为另一个语句的一部分了。 In fact, it stops when it matches the rule successfully and doesn't even look at the next token if it doesn't need to. 实际上,它在成功匹配规则时停止,并且在不需要时甚至不查看下一个标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM