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