Miguel Velez
Miguel Velez

Reputation: 616

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?

Upvotes: 0

Views: 95

Answers (1)

Jiri Tousek
Jiri Tousek

Reputation: 12450

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.

Upvotes: 2

Related Questions