xref: /sqlite-3.40.0/src/parse.y (revision 9c42626e)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains SQLite's grammar for SQL.  Process this file
13 ** using the lemon parser generator to generate C code that runs
14 ** the parser.  Lemon will also generate a header file containing
15 ** numeric codes for all of the tokens.
16 */
17 
18 // All token codes are small integers with #defines that begin with "TK_"
19 %token_prefix TK_
20 
21 // The type of the data attached to each token is Token.  This is also the
22 // default type for non-terminals.
23 //
24 %token_type {Token}
25 %default_type {Token}
26 
27 // The generated parser function takes a 4th argument as follows:
28 %extra_argument {Parse *pParse}
29 
30 // This code runs whenever there is a syntax error
31 //
32 %syntax_error {
33   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
34   if( TOKEN.z[0] ){
35     sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
36   }else{
37     sqlite3ErrorMsg(pParse, "incomplete input");
38   }
39 }
40 %stack_overflow {
41   sqlite3ErrorMsg(pParse, "parser stack overflow");
42 }
43 
44 // The name of the generated procedure that implements the parser
45 // is as follows:
46 %name sqlite3Parser
47 
48 // The following text is included near the beginning of the C source
49 // code file that implements the parser.
50 //
51 %include {
52 #include "sqliteInt.h"
53 
54 /*
55 ** Disable all error recovery processing in the parser push-down
56 ** automaton.
57 */
58 #define YYNOERRORRECOVERY 1
59 
60 /*
61 ** Make yytestcase() the same as testcase()
62 */
63 #define yytestcase(X) testcase(X)
64 
65 /*
66 ** Indicate that sqlite3ParserFree() will never be called with a null
67 ** pointer.
68 */
69 #define YYPARSEFREENEVERNULL 1
70 
71 /*
72 ** In the amalgamation, the parse.c file generated by lemon and the
73 ** tokenize.c file are concatenated.  In that case, sqlite3RunParser()
74 ** has access to the the size of the yyParser object and so the parser
75 ** engine can be allocated from stack.  In that case, only the
76 ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
77 ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
78 ** omitted.
79 */
80 #ifdef SQLITE_AMALGAMATION
81 # define sqlite3Parser_ENGINEALWAYSONSTACK 1
82 #endif
83 
84 /*
85 ** Alternative datatype for the argument to the malloc() routine passed
86 ** into sqlite3ParserAlloc().  The default is size_t.
87 */
88 #define YYMALLOCARGTYPE  u64
89 
90 /*
91 ** An instance of the following structure describes the event of a
92 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
93 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
94 **
95 **      UPDATE ON (a,b,c)
96 **
97 ** Then the "b" IdList records the list "a,b,c".
98 */
99 struct TrigEvent { int a; IdList * b; };
100 
101 /*
102 ** Disable lookaside memory allocation for objects that might be
103 ** shared across database connections.
104 */
105 static void disableLookaside(Parse *pParse){
106   pParse->disableLookaside++;
107   pParse->db->lookaside.bDisable++;
108 }
109 
110 } // end %include
111 
112 // Input is a single SQL command
113 input ::= cmdlist.
114 cmdlist ::= cmdlist ecmd.
115 cmdlist ::= ecmd.
116 ecmd ::= SEMI.
117 ecmd ::= explain cmdx SEMI.
118 explain ::= .
119 %ifndef SQLITE_OMIT_EXPLAIN
120 explain ::= EXPLAIN.              { pParse->explain = 1; }
121 explain ::= EXPLAIN QUERY PLAN.   { pParse->explain = 2; }
122 %endif  SQLITE_OMIT_EXPLAIN
123 cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }
124 
125 ///////////////////// Begin and end transactions. ////////////////////////////
126 //
127 
128 cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}
129 trans_opt ::= .
130 trans_opt ::= TRANSACTION.
131 trans_opt ::= TRANSACTION nm.
132 %type transtype {int}
133 transtype(A) ::= .             {A = TK_DEFERRED;}
134 transtype(A) ::= DEFERRED(X).  {A = @X; /*A-overwrites-X*/}
135 transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
136 transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
137 cmd ::= COMMIT|END(X) trans_opt.   {sqlite3EndTransaction(pParse,@X);}
138 cmd ::= ROLLBACK(X) trans_opt.     {sqlite3EndTransaction(pParse,@X);}
139 
140 savepoint_opt ::= SAVEPOINT.
141 savepoint_opt ::= .
142 cmd ::= SAVEPOINT nm(X). {
143   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
144 }
145 cmd ::= RELEASE savepoint_opt nm(X). {
146   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
147 }
148 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
149   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
150 }
151 
152 ///////////////////// The CREATE TABLE statement ////////////////////////////
153 //
154 cmd ::= create_table create_table_args.
155 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
156    sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
157 }
158 createkw(A) ::= CREATE(A).  {disableLookaside(pParse);}
159 
160 %type ifnotexists {int}
161 ifnotexists(A) ::= .              {A = 0;}
162 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
163 %type temp {int}
164 %ifndef SQLITE_OMIT_TEMPDB
165 temp(A) ::= TEMP.  {A = 1;}
166 %endif  SQLITE_OMIT_TEMPDB
167 temp(A) ::= .      {A = 0;}
168 create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
169   sqlite3EndTable(pParse,&X,&E,F,0);
170 }
171 create_table_args ::= AS select(S). {
172   sqlite3EndTable(pParse,0,0,0,S);
173   sqlite3SelectDelete(pParse->db, S);
174 }
175 %type table_options {int}
176 table_options(A) ::= .    {A = 0;}
177 table_options(A) ::= WITHOUT nm(X). {
178   if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
179     A = TF_WithoutRowid | TF_NoVisibleRowid;
180   }else{
181     A = 0;
182     sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
183   }
184 }
185 columnlist ::= columnlist COMMA columnname carglist.
186 columnlist ::= columnname carglist.
187 columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);}
188 
189 // Declare some tokens early in order to influence their values, to
190 // improve performance and reduce the executable size.  The goal here is
191 // to get the "jump" operations in ISNULL through ESCAPE to have numeric
192 // values that are early enough so that all jump operations are clustered
193 // at the beginning, but also so that the comparison tokens NE through GE
194 // are as large as possible so that they are near to FUNCTION, which is a
195 // token synthesized by addopcodes.tcl.
196 //
197 %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
198 %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
199 %token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
200 %token GT LE LT GE ESCAPE.
201 
202 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
203 // fallback to ID if they will not parse as their original value.
204 // This obviates the need for the "id" nonterminal.
205 //
206 %fallback ID
207   ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
208   CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
209   IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
210   QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW
211   ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
212 %ifdef SQLITE_OMIT_COMPOUND_SELECT
213   EXCEPT INTERSECT UNION
214 %endif SQLITE_OMIT_COMPOUND_SELECT
215   REINDEX RENAME CTIME_KW IF
216   .
217 %wildcard ANY.
218 
219 // Define operator precedence early so that this is the first occurrence
220 // of the operator tokens in the grammer.  Keeping the operators together
221 // causes them to be assigned integer values that are close together,
222 // which keeps parser tables smaller.
223 //
224 // The token values assigned to these symbols is determined by the order
225 // in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,
226 // NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See
227 // the sqlite3ExprIfFalse() routine for additional information on this
228 // constraint.
229 //
230 %left OR.
231 %left AND.
232 %right NOT.
233 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
234 %left GT LE LT GE.
235 %right ESCAPE.
236 %left BITAND BITOR LSHIFT RSHIFT.
237 %left PLUS MINUS.
238 %left STAR SLASH REM.
239 %left CONCAT.
240 %left COLLATE.
241 %right BITNOT.
242 
243 // An IDENTIFIER can be a generic identifier, or one of several
244 // keywords.  Any non-standard keyword can also be an identifier.
245 //
246 %token_class id  ID|INDEXED.
247 
248 
249 // And "ids" is an identifer-or-string.
250 //
251 %token_class ids  ID|STRING.
252 
253 // The name of a column or table can be any of the following:
254 //
255 %type nm {Token}
256 nm(A) ::= id(A).
257 nm(A) ::= STRING(A).
258 nm(A) ::= JOIN_KW(A).
259 
260 // A typetoken is really zero or more tokens that form a type name such
261 // as can be found after the column name in a CREATE TABLE statement.
262 // Multiple tokens are concatenated to form the value of the typetoken.
263 //
264 %type typetoken {Token}
265 typetoken(A) ::= .   {A.n = 0; A.z = 0;}
266 typetoken(A) ::= typename(A).
267 typetoken(A) ::= typename(A) LP signed RP(Y). {
268   A.n = (int)(&Y.z[Y.n] - A.z);
269 }
270 typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
271   A.n = (int)(&Y.z[Y.n] - A.z);
272 }
273 %type typename {Token}
274 typename(A) ::= ids(A).
275 typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
276 signed ::= plus_num.
277 signed ::= minus_num.
278 
279 // The scanpt non-terminal takes a value which is a pointer to the
280 // input text just past the last token that has been shifted into
281 // the parser.  By surrounding some phrase in the grammar with two
282 // scanpt non-terminals, we can capture the input text for that phrase.
283 // For example:
284 //
285 //      something ::= .... scanpt(A) phrase scanpt(Z).
286 //
287 // The text that is parsed as "phrase" is a string starting at A
288 // and containing (int)(Z-A) characters.  There might be some extra
289 // whitespace on either end of the text, but that can be removed in
290 // post-processing, if needed.
291 //
292 %type scanpt {const char*}
293 scanpt(A) ::= . {
294   assert( yyLookahead!=YYNOCODE );
295   A = yyLookaheadToken.z;
296 }
297 
298 // "carglist" is a list of additional constraints that come after the
299 // column name and column type in a CREATE TABLE statement.
300 //
301 carglist ::= carglist ccons.
302 carglist ::= .
303 ccons ::= CONSTRAINT nm(X).           {pParse->constraintName = X;}
304 ccons ::= DEFAULT scanpt(A) term(X) scanpt(Z).
305                             {sqlite3AddDefaultValue(pParse,X,A,Z);}
306 ccons ::= DEFAULT LP(A) expr(X) RP(Z).
307                             {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
308 ccons ::= DEFAULT PLUS(A) term(X) scanpt(Z).
309                             {sqlite3AddDefaultValue(pParse,X,A.z,Z);}
310 ccons ::= DEFAULT MINUS(A) term(X) scanpt(Z).      {
311   Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
312   sqlite3AddDefaultValue(pParse,p,A.z,Z);
313 }
314 ccons ::= DEFAULT scanpt id(X).       {
315   Expr *p = tokenExpr(pParse, TK_STRING, X);
316   if( p ){
317     sqlite3ExprIdToTrueFalse(p);
318     testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
319   }
320   sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
321 }
322 
323 // In addition to the type name, we also care about the primary key and
324 // UNIQUE constraints.
325 //
326 ccons ::= NULL onconf.
327 ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}
328 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
329                                  {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
330 ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
331                                    SQLITE_IDXTYPE_UNIQUE);}
332 ccons ::= CHECK LP expr(X) RP.   {sqlite3AddCheckConstraint(pParse,X);}
333 ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
334                                  {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
335 ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}
336 ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}
337 
338 // The optional AUTOINCREMENT keyword
339 %type autoinc {int}
340 autoinc(X) ::= .          {X = 0;}
341 autoinc(X) ::= AUTOINCR.  {X = 1;}
342 
343 // The next group of rules parses the arguments to a REFERENCES clause
344 // that determine if the referential integrity checking is deferred or
345 // or immediate and which determine what action to take if a ref-integ
346 // check fails.
347 //
348 %type refargs {int}
349 refargs(A) ::= .                  { A = OE_None*0x0101; /* EV: R-19803-45884 */}
350 refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
351 %type refarg {struct {int value; int mask;}}
352 refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
353 refarg(A) ::= ON INSERT refact.      { A.value = 0;     A.mask = 0x000000; }
354 refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
355 refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
356 %type refact {int}
357 refact(A) ::= SET NULL.              { A = OE_SetNull;  /* EV: R-33326-45252 */}
358 refact(A) ::= SET DEFAULT.           { A = OE_SetDflt;  /* EV: R-33326-45252 */}
359 refact(A) ::= CASCADE.               { A = OE_Cascade;  /* EV: R-33326-45252 */}
360 refact(A) ::= RESTRICT.              { A = OE_Restrict; /* EV: R-33326-45252 */}
361 refact(A) ::= NO ACTION.             { A = OE_None;     /* EV: R-33326-45252 */}
362 %type defer_subclause {int}
363 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt.     {A = 0;}
364 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
365 %type init_deferred_pred_opt {int}
366 init_deferred_pred_opt(A) ::= .                       {A = 0;}
367 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
368 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
369 
370 conslist_opt(A) ::= .                         {A.n = 0; A.z = 0;}
371 conslist_opt(A) ::= COMMA(A) conslist.
372 conslist ::= conslist tconscomma tcons.
373 conslist ::= tcons.
374 tconscomma ::= COMMA.            {pParse->constraintName.n = 0;}
375 tconscomma ::= .
376 tcons ::= CONSTRAINT nm(X).      {pParse->constraintName = X;}
377 tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
378                                  {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
379 tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
380                                  {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
381                                        SQLITE_IDXTYPE_UNIQUE);}
382 tcons ::= CHECK LP expr(E) RP onconf.
383                                  {sqlite3AddCheckConstraint(pParse,E);}
384 tcons ::= FOREIGN KEY LP eidlist(FA) RP
385           REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
386     sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
387     sqlite3DeferForeignKey(pParse, D);
388 }
389 %type defer_subclause_opt {int}
390 defer_subclause_opt(A) ::= .                    {A = 0;}
391 defer_subclause_opt(A) ::= defer_subclause(A).
392 
393 // The following is a non-standard extension that allows us to declare the
394 // default behavior when there is a constraint conflict.
395 //
396 %type onconf {int}
397 %type orconf {int}
398 %type resolvetype {int}
399 onconf(A) ::= .                              {A = OE_Default;}
400 onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}
401 orconf(A) ::= .                              {A = OE_Default;}
402 orconf(A) ::= OR resolvetype(X).             {A = X;}
403 resolvetype(A) ::= raisetype(A).
404 resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}
405 resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}
406 
407 ////////////////////////// The DROP TABLE /////////////////////////////////////
408 //
409 cmd ::= DROP TABLE ifexists(E) fullname(X). {
410   sqlite3DropTable(pParse, X, 0, E);
411 }
412 %type ifexists {int}
413 ifexists(A) ::= IF EXISTS.   {A = 1;}
414 ifexists(A) ::= .            {A = 0;}
415 
416 ///////////////////// The CREATE VIEW statement /////////////////////////////
417 //
418 %ifndef SQLITE_OMIT_VIEW
419 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
420           AS select(S). {
421   sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
422 }
423 cmd ::= DROP VIEW ifexists(E) fullname(X). {
424   sqlite3DropTable(pParse, X, 1, E);
425 }
426 %endif  SQLITE_OMIT_VIEW
427 
428 //////////////////////// The SELECT statement /////////////////////////////////
429 //
430 cmd ::= select(X).  {
431   SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
432   sqlite3Select(pParse, X, &dest);
433   sqlite3SelectDelete(pParse->db, X);
434 }
435 
436 %type select {Select*}
437 %destructor select {sqlite3SelectDelete(pParse->db, $$);}
438 %type selectnowith {Select*}
439 %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
440 %type oneselect {Select*}
441 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
442 
443 %include {
444   /*
445   ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
446   ** all elements in the list.  And make sure list length does not exceed
447   ** SQLITE_LIMIT_COMPOUND_SELECT.
448   */
449   static void parserDoubleLinkSelect(Parse *pParse, Select *p){
450     if( p->pPrior ){
451       Select *pNext = 0, *pLoop;
452       int mxSelect, cnt = 0;
453       for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
454         pLoop->pNext = pNext;
455         pLoop->selFlags |= SF_Compound;
456       }
457       if( (p->selFlags & SF_MultiValue)==0 &&
458         (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
459         cnt>mxSelect
460       ){
461         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
462       }
463     }
464   }
465 }
466 
467 select(A) ::= with(W) selectnowith(X). {
468   Select *p = X;
469   if( p ){
470     p->pWith = W;
471     parserDoubleLinkSelect(pParse, p);
472   }else{
473     sqlite3WithDelete(pParse->db, W);
474   }
475   A = p; /*A-overwrites-W*/
476 }
477 
478 selectnowith(A) ::= oneselect(A).
479 %ifndef SQLITE_OMIT_COMPOUND_SELECT
480 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z).  {
481   Select *pRhs = Z;
482   Select *pLhs = A;
483   if( pRhs && pRhs->pPrior ){
484     SrcList *pFrom;
485     Token x;
486     x.n = 0;
487     parserDoubleLinkSelect(pParse, pRhs);
488     pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
489     pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
490   }
491   if( pRhs ){
492     pRhs->op = (u8)Y;
493     pRhs->pPrior = pLhs;
494     if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
495     pRhs->selFlags &= ~SF_MultiValue;
496     if( Y!=TK_ALL ) pParse->hasCompound = 1;
497   }else{
498     sqlite3SelectDelete(pParse->db, pLhs);
499   }
500   A = pRhs;
501 }
502 %type multiselect_op {int}
503 multiselect_op(A) ::= UNION(OP).             {A = @OP; /*A-overwrites-OP*/}
504 multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
505 multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP; /*A-overwrites-OP*/}
506 %endif SQLITE_OMIT_COMPOUND_SELECT
507 oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y)
508                  groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
509 #if SELECTTRACE_ENABLED
510   Token s = S; /*A-overwrites-S*/
511 #endif
512   A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
513 #if SELECTTRACE_ENABLED
514   /* Populate the Select.zSelName[] string that is used to help with
515   ** query planner debugging, to differentiate between multiple Select
516   ** objects in a complex query.
517   **
518   ** If the SELECT keyword is immediately followed by a C-style comment
519   ** then extract the first few alphanumeric characters from within that
520   ** comment to be the zSelName value.  Otherwise, the label is #N where
521   ** is an integer that is incremented with each SELECT statement seen.
522   */
523   if( A!=0 ){
524     const char *z = s.z+6;
525     int i;
526     sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "#%d",
527                      ++pParse->nSelect);
528     while( z[0]==' ' ) z++;
529     if( z[0]=='/' && z[1]=='*' ){
530       z += 2;
531       while( z[0]==' ' ) z++;
532       for(i=0; sqlite3Isalnum(z[i]); i++){}
533       sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z);
534     }
535   }
536 #endif /* SELECTRACE_ENABLED */
537 }
538 oneselect(A) ::= values(A).
539 
540 %type values {Select*}
541 %destructor values {sqlite3SelectDelete(pParse->db, $$);}
542 values(A) ::= VALUES LP nexprlist(X) RP. {
543   A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
544 }
545 values(A) ::= values(A) COMMA LP exprlist(Y) RP. {
546   Select *pRight, *pLeft = A;
547   pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
548   if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
549   if( pRight ){
550     pRight->op = TK_ALL;
551     pRight->pPrior = pLeft;
552     A = pRight;
553   }else{
554     A = pLeft;
555   }
556 }
557 
558 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
559 // present and false (0) if it is not.
560 //
561 %type distinct {int}
562 distinct(A) ::= DISTINCT.   {A = SF_Distinct;}
563 distinct(A) ::= ALL.        {A = SF_All;}
564 distinct(A) ::= .           {A = 0;}
565 
566 // selcollist is a list of expressions that are to become the return
567 // values of the SELECT statement.  The "*" in statements like
568 // "SELECT * FROM ..." is encoded as a special expression with an
569 // opcode of TK_ASTERISK.
570 //
571 %type selcollist {ExprList*}
572 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
573 %type sclp {ExprList*}
574 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
575 sclp(A) ::= selcollist(A) COMMA.
576 sclp(A) ::= .                                {A = 0;}
577 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y).     {
578    A = sqlite3ExprListAppend(pParse, A, X);
579    if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
580    sqlite3ExprListSetSpan(pParse,A,B,Z);
581 }
582 selcollist(A) ::= sclp(A) scanpt STAR. {
583   Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
584   A = sqlite3ExprListAppend(pParse, A, p);
585 }
586 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
587   Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
588   Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
589   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
590   A = sqlite3ExprListAppend(pParse,A, pDot);
591 }
592 
593 // An option "AS <id>" phrase that can follow one of the expressions that
594 // define the result set, or one of the tables in the FROM clause.
595 //
596 %type as {Token}
597 as(X) ::= AS nm(Y).    {X = Y;}
598 as(X) ::= ids(X).
599 as(X) ::= .            {X.n = 0; X.z = 0;}
600 
601 
602 %type seltablist {SrcList*}
603 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
604 %type stl_prefix {SrcList*}
605 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
606 %type from {SrcList*}
607 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
608 
609 // A complete FROM clause.
610 //
611 from(A) ::= .                {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
612 from(A) ::= FROM seltablist(X). {
613   A = X;
614   sqlite3SrcListShiftJoinType(A);
615 }
616 
617 // "seltablist" is a "Select Table List" - the content of the FROM clause
618 // in a SELECT statement.  "stl_prefix" is a prefix of this list.
619 //
620 stl_prefix(A) ::= seltablist(A) joinop(Y).    {
621    if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
622 }
623 stl_prefix(A) ::= .                           {A = 0;}
624 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
625                   on_opt(N) using_opt(U). {
626   A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
627   sqlite3SrcListIndexedBy(pParse, A, &I);
628 }
629 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
630                   on_opt(N) using_opt(U). {
631   A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
632   sqlite3SrcListFuncArgs(pParse, A, E);
633 }
634 %ifndef SQLITE_OMIT_SUBQUERY
635   seltablist(A) ::= stl_prefix(A) LP select(S) RP
636                     as(Z) on_opt(N) using_opt(U). {
637     A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
638   }
639   seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
640                     as(Z) on_opt(N) using_opt(U). {
641     if( A==0 && Z.n==0 && N==0 && U==0 ){
642       A = F;
643     }else if( F->nSrc==1 ){
644       A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
645       if( A ){
646         struct SrcList_item *pNew = &A->a[A->nSrc-1];
647         struct SrcList_item *pOld = F->a;
648         pNew->zName = pOld->zName;
649         pNew->zDatabase = pOld->zDatabase;
650         pNew->pSelect = pOld->pSelect;
651         pOld->zName = pOld->zDatabase = 0;
652         pOld->pSelect = 0;
653       }
654       sqlite3SrcListDelete(pParse->db, F);
655     }else{
656       Select *pSubquery;
657       sqlite3SrcListShiftJoinType(F);
658       pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
659       A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
660     }
661   }
662 %endif  SQLITE_OMIT_SUBQUERY
663 
664 %type dbnm {Token}
665 dbnm(A) ::= .          {A.z=0; A.n=0;}
666 dbnm(A) ::= DOT nm(X). {A = X;}
667 
668 %type fullname {SrcList*}
669 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
670 fullname(A) ::= nm(X) dbnm(Y).
671    {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
672 
673 %type joinop {int}
674 joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
675 joinop(X) ::= JOIN_KW(A) JOIN.
676                   {X = sqlite3JoinType(pParse,&A,0,0);  /*X-overwrites-A*/}
677 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
678                   {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
679 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
680                   {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
681 
682 %type on_opt {Expr*}
683 %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
684 on_opt(N) ::= ON expr(E).   {N = E;}
685 on_opt(N) ::= .             {N = 0;}
686 
687 // Note that this block abuses the Token type just a little. If there is
688 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
689 // there is an INDEXED BY clause, then the token is populated as per normal,
690 // with z pointing to the token data and n containing the number of bytes
691 // in the token.
692 //
693 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
694 // normally illegal. The sqlite3SrcListIndexedBy() function
695 // recognizes and interprets this as a special case.
696 //
697 %type indexed_opt {Token}
698 indexed_opt(A) ::= .                 {A.z=0; A.n=0;}
699 indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
700 indexed_opt(A) ::= NOT INDEXED.      {A.z=0; A.n=1;}
701 
702 %type using_opt {IdList*}
703 %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
704 using_opt(U) ::= USING LP idlist(L) RP.  {U = L;}
705 using_opt(U) ::= .                        {U = 0;}
706 
707 
708 %type orderby_opt {ExprList*}
709 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
710 
711 // the sortlist non-terminal stores a list of expression where each
712 // expression is optionally followed by ASC or DESC to indicate the
713 // sort order.
714 //
715 %type sortlist {ExprList*}
716 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
717 
718 orderby_opt(A) ::= .                          {A = 0;}
719 orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
720 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z). {
721   A = sqlite3ExprListAppend(pParse,A,Y);
722   sqlite3ExprListSetSortOrder(A,Z);
723 }
724 sortlist(A) ::= expr(Y) sortorder(Z). {
725   A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
726   sqlite3ExprListSetSortOrder(A,Z);
727 }
728 
729 %type sortorder {int}
730 
731 sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
732 sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
733 sortorder(A) ::= .              {A = SQLITE_SO_UNDEFINED;}
734 
735 %type groupby_opt {ExprList*}
736 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
737 groupby_opt(A) ::= .                      {A = 0;}
738 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
739 
740 %type having_opt {Expr*}
741 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
742 having_opt(A) ::= .                {A = 0;}
743 having_opt(A) ::= HAVING expr(X).  {A = X;}
744 
745 %type limit_opt {Expr*}
746 
747 // The destructor for limit_opt will never fire in the current grammar.
748 // The limit_opt non-terminal only occurs at the end of a single production
749 // rule for SELECT statements.  As soon as the rule that create the
750 // limit_opt non-terminal reduces, the SELECT statement rule will also
751 // reduce.  So there is never a limit_opt non-terminal on the stack
752 // except as a transient.  So there is never anything to destroy.
753 //
754 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
755 limit_opt(A) ::= .       {A = 0;}
756 limit_opt(A) ::= LIMIT expr(X).
757                          {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
758 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
759                          {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
760 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
761                          {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
762 
763 /////////////////////////// The DELETE statement /////////////////////////////
764 //
765 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
766 cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W)
767         orderby_opt(O) limit_opt(L). {
768   sqlite3WithPush(pParse, C, 1);
769   sqlite3SrcListIndexedBy(pParse, X, &I);
770   sqlite3DeleteFrom(pParse,X,W,O,L);
771 }
772 %endif
773 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
774 cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W). {
775   sqlite3WithPush(pParse, C, 1);
776   sqlite3SrcListIndexedBy(pParse, X, &I);
777   sqlite3DeleteFrom(pParse,X,W,0,0);
778 }
779 %endif
780 
781 %type where_opt {Expr*}
782 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
783 
784 where_opt(A) ::= .                    {A = 0;}
785 where_opt(A) ::= WHERE expr(X).       {A = X;}
786 
787 ////////////////////////// The UPDATE command ////////////////////////////////
788 //
789 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
790 cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
791         where_opt(W) orderby_opt(O) limit_opt(L).  {
792   sqlite3WithPush(pParse, C, 1);
793   sqlite3SrcListIndexedBy(pParse, X, &I);
794   sqlite3ExprListCheckLength(pParse,Y,"set list");
795   sqlite3Update(pParse,X,Y,W,R,O,L);
796 }
797 %endif
798 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
799 cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
800         where_opt(W).  {
801   sqlite3WithPush(pParse, C, 1);
802   sqlite3SrcListIndexedBy(pParse, X, &I);
803   sqlite3ExprListCheckLength(pParse,Y,"set list");
804   sqlite3Update(pParse,X,Y,W,R,0,0);
805 }
806 %endif
807 
808 %type setlist {ExprList*}
809 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
810 
811 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
812   A = sqlite3ExprListAppend(pParse, A, Y);
813   sqlite3ExprListSetName(pParse, A, &X, 1);
814 }
815 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
816   A = sqlite3ExprListAppendVector(pParse, A, X, Y);
817 }
818 setlist(A) ::= nm(X) EQ expr(Y). {
819   A = sqlite3ExprListAppend(pParse, 0, Y);
820   sqlite3ExprListSetName(pParse, A, &X, 1);
821 }
822 setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
823   A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
824 }
825 
826 ////////////////////////// The INSERT command /////////////////////////////////
827 //
828 cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) select(S). {
829   sqlite3WithPush(pParse, W, 1);
830   sqlite3Insert(pParse, X, S, F, R);
831 }
832 cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) DEFAULT VALUES.
833 {
834   sqlite3WithPush(pParse, W, 1);
835   sqlite3Insert(pParse, X, 0, F, R);
836 }
837 
838 %type insert_cmd {int}
839 insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
840 insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
841 
842 %type idlist_opt {IdList*}
843 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
844 %type idlist {IdList*}
845 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
846 
847 idlist_opt(A) ::= .                       {A = 0;}
848 idlist_opt(A) ::= LP idlist(X) RP.    {A = X;}
849 idlist(A) ::= idlist(A) COMMA nm(Y).
850     {A = sqlite3IdListAppend(pParse->db,A,&Y);}
851 idlist(A) ::= nm(Y).
852     {A = sqlite3IdListAppend(pParse->db,0,&Y); /*A-overwrites-Y*/}
853 
854 /////////////////////////// Expression Processing /////////////////////////////
855 //
856 
857 %type expr {Expr*}
858 %destructor expr {sqlite3ExprDelete(pParse->db, $$);}
859 %type term {Expr*}
860 %destructor term {sqlite3ExprDelete(pParse->db, $$);}
861 
862 %include {
863 
864   /* Construct a new Expr object from a single identifier.  Use the
865   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
866   ** that created the expression.
867   */
868   static Expr *tokenExpr(Parse *pParse, int op, Token t){
869     Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
870     if( p ){
871       memset(p, 0, sizeof(Expr));
872       p->op = (u8)op;
873       p->flags = EP_Leaf;
874       p->iAgg = -1;
875       p->u.zToken = (char*)&p[1];
876       memcpy(p->u.zToken, t.z, t.n);
877       p->u.zToken[t.n] = 0;
878       if( sqlite3Isquote(p->u.zToken[0]) ){
879         if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
880         sqlite3Dequote(p->u.zToken);
881       }
882 #if SQLITE_MAX_EXPR_DEPTH>0
883       p->nHeight = 1;
884 #endif
885     }
886     return p;
887   }
888 }
889 
890 expr(A) ::= term(A).
891 expr(A) ::= LP expr(X) RP. {A = X;}
892 expr(A) ::= id(X).          {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
893 expr(A) ::= JOIN_KW(X).     {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
894 expr(A) ::= nm(X) DOT nm(Y). {
895   Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
896   Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
897   A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
898 }
899 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
900   Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
901   Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
902   Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
903   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
904   A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
905 }
906 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
907 term(A) ::= STRING(X).          {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
908 term(A) ::= INTEGER(X). {
909   A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
910 }
911 expr(A) ::= VARIABLE(X).     {
912   if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
913     u32 n = X.n;
914     A = tokenExpr(pParse, TK_VARIABLE, X);
915     sqlite3ExprAssignVarNumber(pParse, A, n);
916   }else{
917     /* When doing a nested parse, one can include terms in an expression
918     ** that look like this:   #1 #2 ...  These terms refer to registers
919     ** in the virtual machine.  #N is the N-th register. */
920     Token t = X; /*A-overwrites-X*/
921     assert( t.n>=2 );
922     if( pParse->nested==0 ){
923       sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
924       A = 0;
925     }else{
926       A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
927       if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
928     }
929   }
930 }
931 expr(A) ::= expr(A) COLLATE ids(C). {
932   A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
933 }
934 %ifndef SQLITE_OMIT_CAST
935 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
936   A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
937   sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
938 }
939 %endif  SQLITE_OMIT_CAST
940 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. {
941   if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
942     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
943   }
944   A = sqlite3ExprFunction(pParse, Y, &X);
945   if( D==SF_Distinct && A ){
946     A->flags |= EP_Distinct;
947   }
948 }
949 expr(A) ::= id(X) LP STAR RP. {
950   A = sqlite3ExprFunction(pParse, 0, &X);
951 }
952 term(A) ::= CTIME_KW(OP). {
953   A = sqlite3ExprFunction(pParse, 0, &OP);
954 }
955 
956 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
957   ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
958   A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
959   if( A ){
960     A->x.pList = pList;
961   }else{
962     sqlite3ExprListDelete(pParse->db, pList);
963   }
964 }
965 
966 expr(A) ::= expr(A) AND(OP) expr(Y).    {A=sqlite3PExpr(pParse,@OP,A,Y);}
967 expr(A) ::= expr(A) OR(OP) expr(Y).     {A=sqlite3PExpr(pParse,@OP,A,Y);}
968 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
969                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
970 expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {A=sqlite3PExpr(pParse,@OP,A,Y);}
971 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
972                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
973 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
974                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
975 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
976                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
977 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
978 %type likeop {Token}
979 likeop(A) ::= LIKE_KW|MATCH(A).
980 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
981 expr(A) ::= expr(A) likeop(OP) expr(Y).  [LIKE_KW]  {
982   ExprList *pList;
983   int bNot = OP.n & 0x80000000;
984   OP.n &= 0x7fffffff;
985   pList = sqlite3ExprListAppend(pParse,0, Y);
986   pList = sqlite3ExprListAppend(pParse,pList, A);
987   A = sqlite3ExprFunction(pParse, pList, &OP);
988   if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
989   if( A ) A->flags |= EP_InfixFunc;
990 }
991 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E).  [LIKE_KW]  {
992   ExprList *pList;
993   int bNot = OP.n & 0x80000000;
994   OP.n &= 0x7fffffff;
995   pList = sqlite3ExprListAppend(pParse,0, Y);
996   pList = sqlite3ExprListAppend(pParse,pList, A);
997   pList = sqlite3ExprListAppend(pParse,pList, E);
998   A = sqlite3ExprFunction(pParse, pList, &OP);
999   if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1000   if( A ) A->flags |= EP_InfixFunc;
1001 }
1002 
1003 expr(A) ::= expr(A) ISNULL|NOTNULL(E).   {A = sqlite3PExpr(pParse,@E,A,0);}
1004 expr(A) ::= expr(A) NOT NULL.    {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
1005 
1006 %include {
1007   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1008   ** unary TK_ISNULL or TK_NOTNULL expression. */
1009   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1010     sqlite3 *db = pParse->db;
1011     if( pA && pY && pY->op==TK_NULL ){
1012       pA->op = (u8)op;
1013       sqlite3ExprDelete(db, pA->pRight);
1014       pA->pRight = 0;
1015     }
1016   }
1017 }
1018 
1019 //    expr1 IS expr2
1020 //    expr1 IS NOT expr2
1021 //
1022 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL.  If expr2
1023 // is any other expression, code as TK_IS or TK_ISNOT.
1024 //
1025 expr(A) ::= expr(A) IS expr(Y).     {
1026   A = sqlite3PExpr(pParse,TK_IS,A,Y);
1027   binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1028 }
1029 expr(A) ::= expr(A) IS NOT expr(Y). {
1030   A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1031   binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1032 }
1033 
1034 expr(A) ::= NOT(B) expr(X).
1035               {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1036 expr(A) ::= BITNOT(B) expr(X).
1037               {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1038 expr(A) ::= MINUS expr(X). [BITNOT]
1039               {A = sqlite3PExpr(pParse, TK_UMINUS, X, 0);}
1040 expr(A) ::= PLUS expr(X). [BITNOT]
1041               {A = sqlite3PExpr(pParse, TK_UPLUS, X, 0);}
1042 
1043 %type between_op {int}
1044 between_op(A) ::= BETWEEN.     {A = 0;}
1045 between_op(A) ::= NOT BETWEEN. {A = 1;}
1046 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1047   ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1048   pList = sqlite3ExprListAppend(pParse,pList, Y);
1049   A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1050   if( A ){
1051     A->x.pList = pList;
1052   }else{
1053     sqlite3ExprListDelete(pParse->db, pList);
1054   }
1055   if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1056 }
1057 %ifndef SQLITE_OMIT_SUBQUERY
1058   %type in_op {int}
1059   in_op(A) ::= IN.      {A = 0;}
1060   in_op(A) ::= NOT IN.  {A = 1;}
1061   expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
1062     if( Y==0 ){
1063       /* Expressions of the form
1064       **
1065       **      expr1 IN ()
1066       **      expr1 NOT IN ()
1067       **
1068       ** simplify to constants 0 (false) and 1 (true), respectively,
1069       ** regardless of the value of expr1.
1070       */
1071       sqlite3ExprDelete(pParse->db, A);
1072       A = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1);
1073     }else if( Y->nExpr==1 ){
1074       /* Expressions of the form:
1075       **
1076       **      expr1 IN (?1)
1077       **      expr1 NOT IN (?2)
1078       **
1079       ** with exactly one value on the RHS can be simplified to something
1080       ** like this:
1081       **
1082       **      expr1 == ?1
1083       **      expr1 <> ?2
1084       **
1085       ** But, the RHS of the == or <> is marked with the EP_Generic flag
1086       ** so that it may not contribute to the computation of comparison
1087       ** affinity or the collating sequence to use for comparison.  Otherwise,
1088       ** the semantics would be subtly different from IN or NOT IN.
1089       */
1090       Expr *pRHS = Y->a[0].pExpr;
1091       Y->a[0].pExpr = 0;
1092       sqlite3ExprListDelete(pParse->db, Y);
1093       /* pRHS cannot be NULL because a malloc error would have been detected
1094       ** before now and control would have never reached this point */
1095       if( ALWAYS(pRHS) ){
1096         pRHS->flags &= ~EP_Collate;
1097         pRHS->flags |= EP_Generic;
1098       }
1099       A = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A, pRHS);
1100     }else{
1101       A = sqlite3PExpr(pParse, TK_IN, A, 0);
1102       if( A ){
1103         A->x.pList = Y;
1104         sqlite3ExprSetHeightAndFlags(pParse, A);
1105       }else{
1106         sqlite3ExprListDelete(pParse->db, Y);
1107       }
1108       if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1109     }
1110   }
1111   expr(A) ::= LP select(X) RP. {
1112     A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1113     sqlite3PExprAddSelect(pParse, A, X);
1114   }
1115   expr(A) ::= expr(A) in_op(N) LP select(Y) RP.  [IN] {
1116     A = sqlite3PExpr(pParse, TK_IN, A, 0);
1117     sqlite3PExprAddSelect(pParse, A, Y);
1118     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1119   }
1120   expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
1121     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
1122     Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
1123     if( E )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
1124     A = sqlite3PExpr(pParse, TK_IN, A, 0);
1125     sqlite3PExprAddSelect(pParse, A, pSelect);
1126     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1127   }
1128   expr(A) ::= EXISTS LP select(Y) RP. {
1129     Expr *p;
1130     p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
1131     sqlite3PExprAddSelect(pParse, p, Y);
1132   }
1133 %endif SQLITE_OMIT_SUBQUERY
1134 
1135 /* CASE expressions */
1136 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1137   A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1138   if( A ){
1139     A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1140     sqlite3ExprSetHeightAndFlags(pParse, A);
1141   }else{
1142     sqlite3ExprListDelete(pParse->db, Y);
1143     sqlite3ExprDelete(pParse->db, Z);
1144   }
1145 }
1146 %type case_exprlist {ExprList*}
1147 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1148 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
1149   A = sqlite3ExprListAppend(pParse,A, Y);
1150   A = sqlite3ExprListAppend(pParse,A, Z);
1151 }
1152 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1153   A = sqlite3ExprListAppend(pParse,0, Y);
1154   A = sqlite3ExprListAppend(pParse,A, Z);
1155 }
1156 %type case_else {Expr*}
1157 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1158 case_else(A) ::=  ELSE expr(X).         {A = X;}
1159 case_else(A) ::=  .                     {A = 0;}
1160 %type case_operand {Expr*}
1161 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1162 case_operand(A) ::= expr(X).            {A = X; /*A-overwrites-X*/}
1163 case_operand(A) ::= .                   {A = 0;}
1164 
1165 %type exprlist {ExprList*}
1166 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1167 %type nexprlist {ExprList*}
1168 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1169 
1170 exprlist(A) ::= nexprlist(A).
1171 exprlist(A) ::= .                            {A = 0;}
1172 nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
1173     {A = sqlite3ExprListAppend(pParse,A,Y);}
1174 nexprlist(A) ::= expr(Y).
1175     {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
1176 
1177 %ifndef SQLITE_OMIT_SUBQUERY
1178 /* A paren_exprlist is an optional expression list contained inside
1179 ** of parenthesis */
1180 %type paren_exprlist {ExprList*}
1181 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1182 paren_exprlist(A) ::= .   {A = 0;}
1183 paren_exprlist(A) ::= LP exprlist(X) RP.  {A = X;}
1184 %endif SQLITE_OMIT_SUBQUERY
1185 
1186 
1187 ///////////////////////////// The CREATE INDEX command ///////////////////////
1188 //
1189 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1190         ON nm(Y) LP sortlist(Z) RP where_opt(W). {
1191   sqlite3CreateIndex(pParse, &X, &D,
1192                      sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
1193                       &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
1194 }
1195 
1196 %type uniqueflag {int}
1197 uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
1198 uniqueflag(A) ::= .        {A = OE_None;}
1199 
1200 
1201 // The eidlist non-terminal (Expression Id List) generates an ExprList
1202 // from a list of identifiers.  The identifier names are in ExprList.a[].zName.
1203 // This list is stored in an ExprList rather than an IdList so that it
1204 // can be easily sent to sqlite3ColumnsExprList().
1205 //
1206 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1207 // used for the arguments to an index.  That is just an historical accident.
1208 //
1209 // IMPORTANT COMPATIBILITY NOTE:  Some prior versions of SQLite accepted
1210 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1211 // places - places that might have been stored in the sqlite_master schema.
1212 // Those extra features were ignored.  But because they might be in some
1213 // (busted) old databases, we need to continue parsing them when loading
1214 // historical schemas.
1215 //
1216 %type eidlist {ExprList*}
1217 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1218 %type eidlist_opt {ExprList*}
1219 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1220 
1221 %include {
1222   /* Add a single new term to an ExprList that is used to store a
1223   ** list of identifiers.  Report an error if the ID list contains
1224   ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1225   ** error while parsing a legacy schema.
1226   */
1227   static ExprList *parserAddExprIdListTerm(
1228     Parse *pParse,
1229     ExprList *pPrior,
1230     Token *pIdToken,
1231     int hasCollate,
1232     int sortOrder
1233   ){
1234     ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1235     if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1236         && pParse->db->init.busy==0
1237     ){
1238       sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1239                          pIdToken->n, pIdToken->z);
1240     }
1241     sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1242     return p;
1243   }
1244 } // end %include
1245 
1246 eidlist_opt(A) ::= .                         {A = 0;}
1247 eidlist_opt(A) ::= LP eidlist(X) RP.         {A = X;}
1248 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z).  {
1249   A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
1250 }
1251 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1252   A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
1253 }
1254 
1255 %type collate {int}
1256 collate(C) ::= .              {C = 0;}
1257 collate(C) ::= COLLATE ids.   {C = 1;}
1258 
1259 
1260 ///////////////////////////// The DROP INDEX command /////////////////////////
1261 //
1262 cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
1263 
1264 ///////////////////////////// The VACUUM command /////////////////////////////
1265 //
1266 %ifndef SQLITE_OMIT_VACUUM
1267 %ifndef SQLITE_OMIT_ATTACH
1268 cmd ::= VACUUM.                {sqlite3Vacuum(pParse,0);}
1269 cmd ::= VACUUM nm(X).          {sqlite3Vacuum(pParse,&X);}
1270 %endif  SQLITE_OMIT_ATTACH
1271 %endif  SQLITE_OMIT_VACUUM
1272 
1273 ///////////////////////////// The PRAGMA command /////////////////////////////
1274 //
1275 %ifndef SQLITE_OMIT_PRAGMA
1276 cmd ::= PRAGMA nm(X) dbnm(Z).                {sqlite3Pragma(pParse,&X,&Z,0,0);}
1277 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).    {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1278 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1279 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1280                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1281 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1282                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1283 
1284 nmnum(A) ::= plus_num(A).
1285 nmnum(A) ::= nm(A).
1286 nmnum(A) ::= ON(A).
1287 nmnum(A) ::= DELETE(A).
1288 nmnum(A) ::= DEFAULT(A).
1289 %endif SQLITE_OMIT_PRAGMA
1290 %token_class number INTEGER|FLOAT.
1291 plus_num(A) ::= PLUS number(X).       {A = X;}
1292 plus_num(A) ::= number(A).
1293 minus_num(A) ::= MINUS number(X).     {A = X;}
1294 //////////////////////////// The CREATE TRIGGER command /////////////////////
1295 
1296 %ifndef SQLITE_OMIT_TRIGGER
1297 
1298 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1299   Token all;
1300   all.z = A.z;
1301   all.n = (int)(Z.z - A.z) + Z.n;
1302   sqlite3FinishTrigger(pParse, S, &all);
1303 }
1304 
1305 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1306                     trigger_time(C) trigger_event(D)
1307                     ON fullname(E) foreach_clause when_clause(G). {
1308   sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1309   A = (Z.n==0?B:Z); /*A-overwrites-T*/
1310 }
1311 
1312 %type trigger_time {int}
1313 trigger_time(A) ::= BEFORE|AFTER(X).  { A = @X; /*A-overwrites-X*/ }
1314 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
1315 trigger_time(A) ::= .            { A = TK_BEFORE; }
1316 
1317 %type trigger_event {struct TrigEvent}
1318 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1319 trigger_event(A) ::= DELETE|INSERT(X).   {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1320 trigger_event(A) ::= UPDATE(X).          {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1321 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
1322 
1323 foreach_clause ::= .
1324 foreach_clause ::= FOR EACH ROW.
1325 
1326 %type when_clause {Expr*}
1327 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1328 when_clause(A) ::= .             { A = 0; }
1329 when_clause(A) ::= WHEN expr(X). { A = X; }
1330 
1331 %type trigger_cmd_list {TriggerStep*}
1332 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1333 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1334   assert( A!=0 );
1335   A->pLast->pNext = X;
1336   A->pLast = X;
1337 }
1338 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1339   assert( A!=0 );
1340   A->pLast = A;
1341 }
1342 
1343 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1344 // within a trigger.  The table to INSERT, UPDATE, or DELETE is always in
1345 // the same database as the table that the trigger fires on.
1346 //
1347 %type trnm {Token}
1348 trnm(A) ::= nm(A).
1349 trnm(A) ::= nm DOT nm(X). {
1350   A = X;
1351   sqlite3ErrorMsg(pParse,
1352         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1353         "statements within triggers");
1354 }
1355 
1356 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1357 // statements within triggers.  We make a specific error message for this
1358 // since it is an exception to the default grammar rules.
1359 //
1360 tridxby ::= .
1361 tridxby ::= INDEXED BY nm. {
1362   sqlite3ErrorMsg(pParse,
1363         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1364         "within triggers");
1365 }
1366 tridxby ::= NOT INDEXED. {
1367   sqlite3ErrorMsg(pParse,
1368         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1369         "within triggers");
1370 }
1371 
1372 
1373 
1374 %type trigger_cmd {TriggerStep*}
1375 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1376 // UPDATE
1377 trigger_cmd(A) ::=
1378    UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z) scanpt(E).
1379    {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R, B.z, E);}
1380 
1381 // INSERT
1382 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
1383                       trnm(X) idlist_opt(F) select(S) scanpt(Z).
1384    {A = sqlite3TriggerInsertStep(pParse->db,&X,F,S,R,B,Z);/*A-overwrites-R*/}
1385 
1386 // DELETE
1387 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1388    {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y, B.z, E);}
1389 
1390 // SELECT
1391 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1392    {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
1393 
1394 // The special RAISE expression that may occur in trigger programs
1395 expr(A) ::= RAISE LP IGNORE RP.  {
1396   A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1397   if( A ){
1398     A->affinity = OE_Ignore;
1399   }
1400 }
1401 expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP.  {
1402   A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1403   if( A ) {
1404     A->affinity = (char)T;
1405   }
1406 }
1407 %endif  !SQLITE_OMIT_TRIGGER
1408 
1409 %type raisetype {int}
1410 raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
1411 raisetype(A) ::= ABORT.     {A = OE_Abort;}
1412 raisetype(A) ::= FAIL.      {A = OE_Fail;}
1413 
1414 
1415 ////////////////////////  DROP TRIGGER statement //////////////////////////////
1416 %ifndef SQLITE_OMIT_TRIGGER
1417 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1418   sqlite3DropTrigger(pParse,X,NOERR);
1419 }
1420 %endif  !SQLITE_OMIT_TRIGGER
1421 
1422 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1423 %ifndef SQLITE_OMIT_ATTACH
1424 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1425   sqlite3Attach(pParse, F, D, K);
1426 }
1427 cmd ::= DETACH database_kw_opt expr(D). {
1428   sqlite3Detach(pParse, D);
1429 }
1430 
1431 %type key_opt {Expr*}
1432 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1433 key_opt(A) ::= .                     { A = 0; }
1434 key_opt(A) ::= KEY expr(X).          { A = X; }
1435 
1436 database_kw_opt ::= DATABASE.
1437 database_kw_opt ::= .
1438 %endif SQLITE_OMIT_ATTACH
1439 
1440 ////////////////////////// REINDEX collation //////////////////////////////////
1441 %ifndef SQLITE_OMIT_REINDEX
1442 cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
1443 cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
1444 %endif  SQLITE_OMIT_REINDEX
1445 
1446 /////////////////////////////////// ANALYZE ///////////////////////////////////
1447 %ifndef SQLITE_OMIT_ANALYZE
1448 cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
1449 cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
1450 %endif
1451 
1452 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1453 %ifndef SQLITE_OMIT_ALTERTABLE
1454 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1455   sqlite3AlterRenameTable(pParse,X,&Z);
1456 }
1457 cmd ::= ALTER TABLE add_column_fullname
1458         ADD kwcolumn_opt columnname(Y) carglist. {
1459   Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
1460   sqlite3AlterFinishAddColumn(pParse, &Y);
1461 }
1462 add_column_fullname ::= fullname(X). {
1463   disableLookaside(pParse);
1464   sqlite3AlterBeginAddColumn(pParse, X);
1465 }
1466 kwcolumn_opt ::= .
1467 kwcolumn_opt ::= COLUMNKW.
1468 %endif  SQLITE_OMIT_ALTERTABLE
1469 
1470 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1471 %ifndef SQLITE_OMIT_VIRTUALTABLE
1472 cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
1473 cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
1474 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1475                 nm(X) dbnm(Y) USING nm(Z). {
1476     sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1477 }
1478 vtabarglist ::= vtabarg.
1479 vtabarglist ::= vtabarglist COMMA vtabarg.
1480 vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
1481 vtabarg ::= vtabarg vtabargtoken.
1482 vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
1483 vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
1484 lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
1485 anylist ::= .
1486 anylist ::= anylist LP anylist RP.
1487 anylist ::= anylist ANY.
1488 %endif  SQLITE_OMIT_VIRTUALTABLE
1489 
1490 
1491 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1492 %type with {With*}
1493 %type wqlist {With*}
1494 %destructor with {sqlite3WithDelete(pParse->db, $$);}
1495 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
1496 
1497 with(A) ::= . {A = 0;}
1498 %ifndef SQLITE_OMIT_CTE
1499 with(A) ::= WITH wqlist(W).              { A = W; }
1500 with(A) ::= WITH RECURSIVE wqlist(W).    { A = W; }
1501 
1502 wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1503   A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/
1504 }
1505 wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1506   A = sqlite3WithAdd(pParse, A, &X, Y, Z);
1507 }
1508 %endif  SQLITE_OMIT_CTE
1509