HyriseSQLParser/src/lex_parser.y

178 lines
2.6 KiB
Plaintext
Raw Normal View History

2014-10-09 01:30:22 +02:00
%{
/*
* parser.y file
* To generate the parser run: "bison parser.y"
*/
#include "Statement.h"
#include "List.h"
#include "lex_parser.h"
#include "lex_lexer.h"
#include <iostream>
int yyerror(Statement **expression, yyscan_t scanner, const char *msg) {
// Add error handling routine as needed
}
%}
%code requires {
#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void* yyscan_t;
#endif
}
%output "lex_parser.c"
%defines "lex_parser.h"
%define api.pure
%lex-param { yyscan_t scanner }
%parse-param { Statement **statement }
%parse-param { yyscan_t scanner }
%union {
int value;
char* sval;
Statement* statement;
SelectStatement* select_statement;
TableRef* table;
2014-10-09 04:09:47 +02:00
Expr* expr;
2014-10-09 01:30:22 +02:00
List<char*>* slist;
2014-10-09 04:09:47 +02:00
List<Expr*>* explist;
2014-10-09 01:30:22 +02:00
}
%token SELECT FROM GROUP BY WHERE NOT AND OR
%token INTNUM COMPARISON STRING
%token <sval> NAME
2014-10-09 01:30:22 +02:00
%type <statement> statement
%type <select_statement> select_statement
%type <sval> column_name table_name
%type <table> from_clause table_exp
2014-10-09 01:30:22 +02:00
%type <expr> expr;
%type <slist> table_ref_commalist
%type <explist> expr_list group_clause
2014-10-09 01:30:22 +02:00
%%
input:
statement opt_semicolon { *statement = $1; }
;
2014-10-09 01:30:22 +02:00
statement:
select_statement { $$ = $1; }
| /* empty */ { $$ = NULL; }
;
2014-10-09 01:30:22 +02:00
select_statement:
SELECT expr_list from_clause where_clause group_clause
2014-10-09 01:30:22 +02:00
{
SelectStatement* s = new SelectStatement();
s->_select_list = $2;
s->_from_table = $3;
s->_group_by = $5;
2014-10-09 01:30:22 +02:00
$$ = s;
}
;
2014-10-09 01:30:22 +02:00
from_clause:
FROM table_exp { $$ = $2; }
;
2014-10-09 01:30:22 +02:00
where_clause:
WHERE search_condition
| /* empty */
;
group_clause:
GROUP BY expr_list { $$ = $3; }
| /* empty */ { $$ = NULL; }
;
2014-10-09 01:30:22 +02:00
table_exp:
table_ref_commalist {
2014-10-09 01:30:22 +02:00
TableRef* t = new TableRef(eTableName);
t->_table_names = $1;
2014-10-09 01:30:22 +02:00
$$ = t;
}
| '(' select_statement ')' {
2014-10-09 01:30:22 +02:00
TableRef* t = new TableRef(eTableSelect);
t->_stmt = $2;
2014-10-09 01:30:22 +02:00
$$ = t;
}
;
2014-10-09 01:30:22 +02:00
search_condition:
predicate
;
predicate:
comparison_predicate
;
2014-10-09 01:30:22 +02:00
comparison_predicate:
scalar_exp COMPARISON scalar_exp
;
2014-10-09 01:30:22 +02:00
expr:
2014-10-09 04:09:47 +02:00
column_name { $$ = new Expr($1); }
| NAME '(' column_name ')' { $$ = new Expr($3, $1); }
;
/* Lists */
expr_list:
2014-10-09 04:09:47 +02:00
expr { $$ = new List<Expr*>($1); }
| expr_list ',' expr { $1->push_back($3); $$ = $1; }
;
// TODO: add table_ref to include names and select queries
table_ref_commalist:
table_name { $$ = new List<char*>($1); }
| table_ref_commalist ',' table_name { $1->push_back($3); $$ = $1; }
;
/* / Lists */
column_name:
NAME
;
table_name:
NAME
| NAME '.' NAME
;
literal:
STRING
| INTNUM
;
scalar_exp:
column_name
| literal
;
opt_semicolon:
';'
| /* empty */
;
2014-10-09 01:30:22 +02:00
%%