David542
David542

Reputation: 110382

Comparing two ways to capture SQL statements in antlr

What would be the difference between the following two ways to define a SQL script, which I've come across from a few existing grammar files:

root
   : stmtblock EOF
   ;

stmtblock
   : stmtmulti
   ;

stmtmulti
   : (stmt SEMI?)*
   ;

stmt 
   : selectstmt   // | or others ...
   ;

And:

root
   : stmtmulti EOF
   ;

stmtmulti
   : stmt*
   ;

stmt 
   : selectstmt ';' // | or others ...
   ;

It seems that:

Which of these two versions would be preferable and why?

Upvotes: 0

Views: 53

Answers (1)

Mike Cargal
Mike Cargal

Reputation: 6785

The main difference is that the first example treats the ‘;’ as optional (and not part of the stmt context. The second example makes the ‘;’ a necessary part of the stmt and won’t match the rule without it being present.

Upvotes: 1

Related Questions