Skip to content
Snippets Groups Projects
Commit 42f7b264 authored by Wanxin Li's avatar Wanxin Li
Browse files

todo: fix conflicts, parse tree cannot be build for AND queries

parent 96024581
No related branches found
No related tags found
No related merge requests found
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
%% %%
SQLPProgram SQLPProgram
: Query : Query
{ printf("Input Query\n"); { printf("Input Query\n");
cons_cell *n = $1; cons_cell *n = $1;
printf("Printing Tree\n"); printf("Printing Tree\n");
print_cons_tree(n); print_cons_tree(n);
...@@ -50,11 +50,11 @@ Body ...@@ -50,11 +50,11 @@ Body
$$ = $2; $$ = $2;
} }
| FROM TablePath WHERE Bool | FROM TablePath WHERE Bool
{ printf("Body 2\n"); { printf("Body 2\n");
$$ = create_eval_operator($4, $2); $$ = create_eval_operator($4, $2);
} }
| FROM TablePath WHERE Pred | FROM TablePath WHERE Pred
{ printf("Body 3\n"); { printf("Body 3\n");
$$ = create_eval_operator($4, $2); $$ = create_eval_operator($4, $2);
} }
; ;
...@@ -82,7 +82,7 @@ Union_Query ...@@ -82,7 +82,7 @@ Union_Query
; ;
Select_List Select_List
: STAR : STAR
{ printf("star\n"); { printf("star\n");
//$$ = new_node(1, Select_List); //$$ = new_node(1, Select_List);
//$$->children[0] = new_node(0, STAR); //$$->children[0] = new_node(0, STAR);
...@@ -92,19 +92,19 @@ Select_List ...@@ -92,19 +92,19 @@ Select_List
$$ = create_rename_operator($1, NULL); $$ = create_rename_operator($1, NULL);
} }
| Col ',' Select_List | Col ',' Select_List
{ printf("Select list\n"); { printf("Select list\n");
$$ = create_rename_operator($1, $2); $$ = create_rename_operator($1, $2);
} }
; ;
Col Col
: VarIdentifier '.' AttrPath : VarIdentifier '.' AttrPath
{ {
printf("col\n"); printf("col\n");
$$ = create_spcol($1, $3); $$ = create_spcol($1, $3);
} }
AttrPath AttrPath
: AttrIdentifier : AttrIdentifier
{ printf("path id\n"); { printf("path id\n");
$$ = create_pf($1, NULL); $$ = create_pf($1, NULL);
...@@ -192,7 +192,7 @@ Pred ...@@ -192,7 +192,7 @@ Pred
$$ = create_atom("INPROG"); $$ = create_atom("INPROG");
//$$->children[0] = new_node(0, EXIST); //$$->children[0] = new_node(0, EXIST);
//$$->children[1] = $3; //$$->children[1] = $3;
} }
; ;
Constant Constant
...@@ -204,7 +204,7 @@ Constant ...@@ -204,7 +204,7 @@ Constant
{ printf("|%s|", yytext); { printf("|%s|", yytext);
$$ = create_constant(yytext); $$ = create_constant(yytext);
} }
| STRING | STRING
{ printf("|%s|", yytext); { printf("|%s|", yytext);
$$ = create_constant(yytext); $$ = create_constant(yytext);
} }
......
%{ %{
#include "util.h" #include "util_new.h"
%} %}
%define api.value.type {cons_cell *} %define api.value.type {cons_cell *}
...@@ -264,7 +264,7 @@ BasicPred ...@@ -264,7 +264,7 @@ BasicPred
| NOT BasicPred | NOT BasicPred
{ {
printf("BasicPred 3 \n"); printf("BasicPred 3 \n");
$$ = create_not_operator($2, NULL); $$ = create_not_operator($2);
} }
| '(' Pred ')' | '(' Pred ')'
{ {
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include "util.h" #include "util_new.h"
#include "lex.yy.c" #include "lex.yy.c"
#include "SQLPGrammar_new.tab.c" #include "SQLPGrammar_new.tab.c"
......
#include "util_new.h"
cons_cell* create_cons_cell(void* car, cons_cell* cdr) {
cons_cell* new_cons = malloc(sizeof(cons_cell));
new_cons->car = car;
new_cons->cdr = cdr;
new_cons->is_atom = false;
return new_cons;
}
atom* create_atom(char* val) {
printf("create_atom: %s\n", val);
atom* new_atom = malloc(sizeof(atom));
new_atom->val = malloc(sizeof(char) * (strlen(val) + 1));
strcpy(new_atom->val, val);
return new_atom;
}
cons_cell* create_cons_cell_w_atom(char* val, cons_cell* cdr) {
atom* new_atom = create_atom(val);
cons_cell* new_cons = malloc(sizeof(cons_cell));
new_cons->car = new_atom;
new_cons->cdr = cdr;
new_cons->is_atom = true;
return new_cons;
}
cons_cell* create_spcol(cons_cell* var, cons_cell* pf) {
cons_cell* var_cons = create_cons_cell(var, pf);
char operator[6] = "SPCOL\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, var_cons);
return operator_cons;
}
cons_cell* create_pf(cons_cell* attr, cons_cell* next_attr) {
cons_cell* attr_cons = create_cons_cell(attr, next_attr);
char operator[3] = "PF\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, attr_cons);
return operator_cons;
}
cons_cell* create_table(char *table) {
cons_cell* table_cons = create_cons_cell_w_atom(table, NULL);
char operator[6] = "TABLE\0";
return create_cons_cell_w_atom(operator, table_cons);
}
cons_cell* create_term(cons_cell* term) {
cons_cell* term_cons = create_cons_cell(term, NULL);
char operator[5] = "TERM\0";
return create_cons_cell_w_atom(operator, term_cons);
}
cons_cell* create_constant(char* constant) {
cons_cell* constant_cons = create_cons_cell_w_atom(constant, NULL);
char operator[6] = "CONST\0";
return create_cons_cell_w_atom(operator, constant_cons);
}
cons_cell* create_col(char *col) {
cons_cell* col_cons = create_cons_cell_w_atom(col, NULL);
char operator[4] = "COL\0";
return create_cons_cell_w_atom(operator, col_cons);
}
cons_cell* create_attr(char *attr) {
cons_cell* attr_cons = create_cons_cell_w_atom(attr, NULL);
char operator[5] = "ATTR\0";
return create_cons_cell_w_atom(operator, attr_cons);
}
cons_cell* create_var(char *var) {
cons_cell* var_cons = create_cons_cell_w_atom(var, NULL);
char operator[4] = "VAR\0";
return create_cons_cell_w_atom(operator, var_cons);
}
cons_cell* create_op(char *op) {
cons_cell* op_cons = create_cons_cell_w_atom(op, NULL);
char operator[3] = "OP\0";
return create_cons_cell_w_atom(operator, op_cons);
}
// todo: why need ra
cons_cell* create_comp_operator(cons_cell* op, cons_cell* term1, cons_cell* term2, cons_cell* ra) {
cons_cell* ra_cons = create_cons_cell(ra, NULL);
cons_cell* term2_cons = create_cons_cell(term2, ra_cons);
cons_cell* term1_cons = create_cons_cell(term1, term2_cons);
cons_cell* op_cons = create_cons_cell(op, term1_cons);
char operator[5] = "COMP\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, op_cons);
return operator_cons;
}
cons_cell* create_atom_operator(cons_cell* table, cons_cell* var) {
cons_cell* var_cons = create_cons_cell(var, NULL);
cons_cell* tab_name_cons = create_cons_cell(table, var_cons);
char operator[5] = "ATOM\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, tab_name_cons);
return operator_cons;
}
cons_cell* create_union_all_operator(cons_cell* ra1, cons_cell* ra2) {
cons_cell* ra2_cons = create_cons_cell(ra2, NULL);
cons_cell* ra1_cons = create_cons_cell(ra1, ra2_cons);
char operator[10] = "UNION_ALL\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, ra1_cons);
return operator_cons;
}
cons_cell* create_union_operator(cons_cell* ra1, cons_cell* ra2) {
cons_cell* ra2_cons = create_cons_cell(ra2, NULL);
cons_cell* ra1_cons = create_cons_cell(ra1, ra2_cons);
char operator[6] = "UNION\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, ra1_cons);
return operator_cons;
}
cons_cell* create_and_operator(cons_cell* ra1, cons_cell* ra2) {
cons_cell* ra2_cons = create_cons_cell(ra2, NULL);
cons_cell* ra1_cons = create_cons_cell(ra1, ra2_cons);
char operator[4] = "AND\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, ra1_cons);
return operator_cons;
}
cons_cell* create_or_operator(cons_cell* ra1, cons_cell* ra2) {
cons_cell* ra2_cons = create_cons_cell(ra2, NULL);
cons_cell* ra1_cons = create_cons_cell(ra1, ra2_cons);
char operator[3] = "OR\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, ra1_cons);
return operator_cons;
}
cons_cell* create_njoin_operator(cons_cell* ra1, cons_cell* ra2) {
cons_cell* ra2_cons = create_cons_cell(ra2, NULL);
cons_cell* ra1_cons = create_cons_cell(ra1, ra2_cons);
char operator[6] = "NJOIN\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, ra1_cons);
return operator_cons;
}
cons_cell* create_cross_operator(cons_cell* ra1, cons_cell* ra2) {
cons_cell* ra2_cons = create_cons_cell(ra2, NULL);
cons_cell* ra1_cons = create_cons_cell(ra1, ra2_cons);
char operator[6] = "CROSS\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, ra1_cons);
return operator_cons;
}
// For when you want to assign column names to another name
cons_cell* create_rename_operator(cons_cell* table, cons_cell* var) {
cons_cell* original_cons = create_cons_cell(table, var);
char operator[7] = "RENAME\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, original_cons);
return operator_cons;
}
cons_cell* create_proj_operator(cons_cell* assign, cons_cell* ra) {
cons_cell* ra_cons = create_cons_cell(ra, NULL);
cons_cell* assign_cons = create_cons_cell(assign, ra_cons);
char operator[5] = "PROJ\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, assign_cons);
return operator_cons;
}
cons_cell* create_not_operator(cons_cell* ra) {
cons_cell* ra_cons = create_cons_cell(ra, NULL);
char operator[4] = "NOT\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, ra_cons);
return operator_cons;
}
cons_cell* create_exist_operator(cons_cell* ra1) {
cons_cell* ra1_cons = create_cons_cell(ra1, NULL);
char operator[6] = "EXIST\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, ra1_cons);
return create_cons_cell_w_atom(operator, ra1_cons);
}
cons_cell* create_limit_operator(cons_cell* ra1, cons_cell* ra2) {
cons_cell* ra2_cons = create_cons_cell(ra2, NULL);
cons_cell* ra1_cons = create_cons_cell(ra1, ra2_cons);
char operator[6] = "LIMIT\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, ra1_cons);
return operator_cons;
}
cons_cell* create_elim_operator(cons_cell* ra1) {
cons_cell* ra1_cons = create_cons_cell(ra1, NULL);
char operator[5] = "ELIM\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, ra1_cons);
return operator_cons;
}
cons_cell* create_eval_operator(cons_cell* logic, cons_cell* cross) {
cons_cell* var_cons = create_cons_cell(logic, cross);
char operator[5] = "EVAL\0";
return create_cons_cell_w_atom(operator, var_cons);
}
void print_cons_tree(cons_cell *root) {
print_cons_tree_helper(root, 0);
}
void print_cons_tree_helper(cons_cell *root, int indent) {
if (root == NULL) {
return;
}
if (root->is_atom) {
for (int i = 0; i < indent; i++) {
printf(" ");
}
printf("%s", ((atom*)root->car)->val);
printf("\n");
print_cons_tree_helper(root->cdr, indent+1);
} else {
print_cons_tree_helper((cons_cell*)root->car, indent);
print_cons_tree_helper(root->cdr, indent);
}
}
#ifndef UTIL_NEW_H
#define UTIL_NEW_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
// Basic building blocks that contain char values
typedef struct atom {
char* val;
} atom;
// A linked list like structure
// car points to element the node is holding
// cdr points to next cons cell in list or null
// is_atom determines if car is an atom
typedef struct cons_cell {
void* car;
struct cons_cell* cdr;
bool is_atom;
} cons_cell;
// Creates a cons_cell
cons_cell* create_cons_cell(void* car, cons_cell* cdr);
// Creates an atom
atom* create_atom(char* val);
// Creates a cons_cell that has an atom as its car
cons_cell* create_cons_cell_w_atom(char* val, cons_cell* cdr);
/*
Below comment block are functions that implement the following relational
algebra expressions for SQL queries:
<ra> := (comp <op> <term> <term> <ra>)
| (atom <table> <var>)
| (union-all <ra> <ra>)
| (cross <ra> <ra>)
| (proj ((rename <col> <col>)) <ra>)
| (not <ra> <ra>)
| (elim <ra>)
| (limit n <ra> <ra>)
| (eval <ra> <ra>)
<term> := <col> | <const>
<col> := <spcol> | <scol>
<spcol> := <var> "." <pf> // for SQLP
<scol> := <var> "." <attr> // for SQL
<pf> := <attr> <attr>
<var> := identifier
<attr> := identifier
<table> := identifier
<term> := identifier
<const> := identifier
<op> := [GT, GE, EQ, LE, LT]
*/
// The following are all helper functions to create the
// various relational algebra expressions
cons_cell* create_spcol(cons_cell* var, cons_cell* pf);
cons_cell* create_pf(cons_cell* attr, cons_cell* next_attr);
cons_cell* create_table(char *table);
cons_cell* create_term(cons_cell *term);
cons_cell* create_constant(char* constant);
cons_cell* create_col(char *col);
cons_cell* create_attr(char *attr);
cons_cell* create_var(char *var);
cons_cell* create_op(char *op);
cons_cell* create_comp_operator(cons_cell* op, cons_cell* term1, cons_cell* term2, cons_cell* ra);
cons_cell* create_atom_operator(cons_cell* table, cons_cell* var);
cons_cell* create_union_all_operator(cons_cell* ra1, cons_cell* ra2);
cons_cell* create_union_operator(cons_cell* ra1, cons_cell* ra2);
cons_cell* create_cross_operator(cons_cell* ra1, cons_cell* ra2);
cons_cell* create_rename_operator(cons_cell* table, cons_cell* var);
cons_cell* create_proj_operator(cons_cell* assign, cons_cell* ra);
cons_cell* create_not_operator(cons_cell* ra);
cons_cell* create_exist_operator(cons_cell* ra1);
cons_cell* create_limit_operator(cons_cell* ra1, cons_cell* ra2);
cons_cell* create_elim_operator(cons_cell* ra1);
cons_cell* create_eval_operator(cons_cell* logic, cons_cell* cross);
// Prints an in order traversal of the tree
void print_cons_tree(cons_cell *root);
void print_cons_tree_helper(cons_cell *root, int indent);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment