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