简体   繁体   中英

ANTLR 3.5.2 matches rule even though input has extra tokens

I am writing a grammar to parse sql statements. I have the following rule:

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

When my input is show databases , I get the message. However, when my input is show databases now , I DO see the message. I am building a REPL and all of the lines end with ; . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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