Appendix A. SQL Parser Grammar and Cross-Reference

Since the grammar for the SQL parser in Chapter 4 is so large, here’s a list of the rules in the order they appear in the source file, as well as a cross-reference of each token and nonterminal symbol with the rules where they appear. This cross-reference is for the slightly extended version in Chapter 8 that includes error recovery rules.

The listing in Example A-1 and the cross-references in Example A-2 are extracted from the listing file lpmysql.output created when bison compiled the grammar. The listing also includes a list of unused tokens, of which this grammar has quite a few since it defines SQL keywords not used in the subset we parse, and it includes the complete set of parser states and shift and reduce actions. The entire listing is more than 10,000 lines long, much too long to include in this book, but is invaluable for reference when debugging a grammar.

Example A-1. SQL grammar listing
 0 $accept: stmt_list $end 1 stmt_list: stmt ';' 2 | stmt_list stmt ';' 3 | error ';' 4 | stmt_list error ';' 5 stmt: select_stmt 6 select_stmt: SELECT select_opts select_expr_list 7 | SELECT select_opts select_expr_list FROM table_references opt_where opt_groupby opt_having opt_orderby opt_limit opt_into_list 8 opt_where: /* empty */ 9 | WHERE expr 10 opt_groupby: /* empty */ 11 | GROUP BY groupby_list opt_with_rollup 12 groupby_list: expr opt_asc_desc 13 | groupby_list ',' expr opt_asc_desc 14 opt_asc_desc: /* empty */ 15 | ASC 16 | ...

Get flex & bison now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.