From d2baa4a3c0d634ad154abae3287d0c9680f8d079 Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 22 Oct 2014 16:01:25 +0200 Subject: [PATCH] now using tokens to describe predicate type instead of string --- src/bison/SQLParser.h | 1 + src/bison/bison_parser.y | 15 ++++++++++----- src/bison/flex_lexer.l | 40 +++++++++++++++++++--------------------- src/lib/Expr.cpp | 5 +++-- src/lib/Expr.h | 5 +++-- src/sql_tests.cpp | 4 ++-- 6 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/bison/SQLParser.h b/src/bison/SQLParser.h index 779a644..c82c54d 100644 --- a/src/bison/SQLParser.h +++ b/src/bison/SQLParser.h @@ -2,6 +2,7 @@ #define __SQLPARSER_H_ #include "Statement.h" +#include "bison_parser.h" class SQLParser { public: diff --git a/src/bison/bison_parser.y b/src/bison/bison_parser.y index 5ce8054..2422b34 100644 --- a/src/bison/bison_parser.y +++ b/src/bison/bison_parser.y @@ -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 NAME STRING COMPARISON -%token INTNUM - - +%token FLOAT +%token 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); } ; diff --git a/src/bison/flex_lexer.l b/src/bison/flex_lexer.l index 96c7081..80bf361 100644 --- a/src/bison/flex_lexer.l +++ b/src/bison/flex_lexer.l @@ -15,7 +15,7 @@ #include "bison_parser.h" #include -#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; diff --git a/src/lib/Expr.cpp b/src/lib/Expr.cpp index 05d9ab5..dcca445 100644 --- a/src/lib/Expr.cpp +++ b/src/lib/Expr.cpp @@ -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; diff --git a/src/lib/Expr.h b/src/lib/Expr.h index dc3f812..99d6c5e 100644 --- a/src/lib/Expr.h +++ b/src/lib/Expr.h @@ -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); diff --git a/src/sql_tests.cpp b/src/sql_tests.cpp index 247d285..f2ef14d 100644 --- a/src/sql_tests.cpp +++ b/src/sql_tests.cpp @@ -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);