now using tokens to describe predicate type instead of string

This commit is contained in:
Pedro 2014-10-22 16:01:25 +02:00
parent d2b5e10945
commit d2baa4a3c0
6 changed files with 38 additions and 32 deletions

View File

@ -2,6 +2,7 @@
#define __SQLPARSER_H_
#include "Statement.h"
#include "bison_parser.h"
class SQLParser {
public:

View File

@ -61,6 +61,7 @@ typedef void* yyscan_t;
*********************************/
%union {
float number;
uint uintnum;
char* sval;
Statement* statement;
@ -79,9 +80,8 @@ typedef void* yyscan_t;
*********************************/
%token SELECT FROM GROUP BY WHERE NOT AND OR
%token <sval> NAME STRING COMPARISON
%token <number> INTNUM
%token <number> FLOAT
%token <uintnum> EQUALS NOTEQUALS LESS GREATER LESSEQ GREATEREQ
/*********************************
** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html)
@ -182,7 +182,12 @@ predicate:
comparison_predicate:
scalar_exp COMPARISON scalar_exp { $$ = makePredicate($1, $2, $3); }
scalar_exp EQUALS scalar_exp { $$ = makePredicate($1, EQUALS, $3); }
| scalar_exp NOTEQUALS scalar_exp { $$ = makePredicate($1, NOTEQUALS, $3); }
| scalar_exp LESS scalar_exp { $$ = makePredicate($1, LESS, $3); }
| scalar_exp GREATER scalar_exp { $$ = makePredicate($1, GREATER, $3); }
| scalar_exp LESSEQ scalar_exp { $$ = makePredicate($1, LESSEQ, $3); }
| scalar_exp GREATEREQ scalar_exp { $$ = makePredicate($1, GREATEREQ, $3); }
;
// TODO: Expression can also be scalar_exp
@ -209,7 +214,7 @@ table_name:
literal:
STRING { $$ = makeStringLiteral($1); }
| INTNUM { $$ = makeFloatLiteral($1); }
| FLOAT { $$ = makeFloatLiteral($1); }
;

View File

@ -15,7 +15,7 @@
#include "bison_parser.h"
#include <stdio.h>
#define TOK(name) { return name; }
#define TOKEN(name) { return name; }
%}
/***************************
@ -46,36 +46,34 @@
[ \t\n]+ /* skip whitespace */;
SELECT TOK(SELECT)
FROM TOK(FROM)
GROUP TOK(GROUP)
BY TOK(BY)
WHERE TOK(WHERE)
NOT TOK(NOT)
AND TOK(AND)
OR TOK(OR)
SELECT TOKEN(SELECT)
FROM TOKEN(FROM)
GROUP TOKEN(GROUP)
BY TOKEN(BY)
WHERE TOKEN(WHERE)
NOT TOKEN(NOT)
AND TOKEN(AND)
OR TOKEN(OR)
"=" TOKEN(EQUALS)
"<>" TOKEN(NOTEQUALS)
"<" TOKEN(LESS)
">" TOKEN(GREATER)
"<=" TOKEN(LESSEQ)
">=" TOKEN(GREATEREQ)
"=" |
"<>" |
"<" |
">" |
"<=" |
">=" {
yylval->sval = strdup(yytext);
return COMPARISON;
}
[-+*/(),.;] TOKEN(yytext[0])
[-+*/(),.;] TOK(yytext[0])
[0-9]+ |
[0-9]+"."[0-9]* |
"."[0-9]* {
yylval->number = atof(yytext);
return INTNUM;
return FLOAT;
}
[A-Za-z][A-Za-z0-9_]* {
yylval->sval = strdup(yytext);
return NAME;

View File

@ -15,9 +15,10 @@ Expr* makeFunctionRef(char* func_name, Expr* expr) {
return e;
}
Expr* makePredicate(Expr* expr1, char* op, Expr* expr2) {
Expr* makePredicate(Expr* expr1, uint op, Expr* expr2) {
ALLOC_EXPR(e, eExprPredicate);
e->name = op;
// printf("Pred: %u\n", op);
e->pred_type = op;
e->expr = expr1;
e->expr2 = expr2;
return e;

View File

@ -17,9 +17,10 @@ typedef struct Expr Expr;
struct Expr {
EExprType type;
char* name;
Expr* expr;
Expr* expr2;
char* name;
uint pred_type;
float float_literal;
};
@ -36,7 +37,7 @@ struct Expr {
Expr* makeColumnRef(char* name);
Expr* makeFunctionRef(char* func_name, Expr* expr);
Expr* makePredicate(Expr* expr1, char* op, Expr* expr2);
Expr* makePredicate(Expr* expr1, uint op, Expr* expr2);
Expr* makeFloatLiteral(float value);
Expr* makeStringLiteral(char* string);

View File

@ -18,7 +18,7 @@ void SelectTest1() {
printf("Test: SelectTest1... ");
fflush(stdout);
const char* sql = "SELECT age, name, address from table WHERE age > 12.5;";
const char* sql = "SELECT age, name, address from table WHERE age < 12.5;";
Statement* sqlStatement = SQLParser::parseSQL(sql);
ASSERT(sqlStatement != NULL);
ASSERT(sqlStatement->type == eSelect);
@ -38,7 +38,7 @@ void SelectTest1() {
ASSERT(stmt->where_clause != NULL);
ASSERT(stmt->where_clause->expr->type == eExprColumnRef);
ASSERT_STR(stmt->where_clause->expr->name, "age");
ASSERT_STR(stmt->where_clause->name, ">");
ASSERT(stmt->where_clause->pred_type == LESS);
ASSERT(stmt->where_clause->expr2->type == eExprLiteralFloat);
ASSERT(stmt->where_clause->expr2->float_literal == 12.5);