Skip to content
Snippets Groups Projects
Commit 370d694d authored by Tony Zhang's avatar Tony Zhang
Browse files

Remove unused files

parent e0e814c8
No related branches found
No related tags found
1 merge request!6Rewrite Parsers for SQLP Query, Schema, and Referring Type Expression
*.o
\ No newline at end of file
*.o
.DS_Store
SQLPParser.dSYM/
SQLPRefExp.dSYM/
SQLPSchema.dSYM/
......@@ -2,12 +2,6 @@
#CFLAGS = `pkg-config --cflags glib-2.0`
#LDLIBS = `pkg-config --libs glib-2.0`
all: SQLPSchema SQLPParser_new SQLPRefExp
SQLPParser: SQLPParser.c SQLPGrammar.y SQLPScanner.l util.o
bison --verbose -d SQLPGrammar.y
flex SQLPScanner.l
gcc -g -w SQLPParser.c util.o -o SQLPParser
rm -f lex.yy.c SQLPGrammar.tab.c SQLPGrammar.tab.h
SQLPParser_new: SQLPParser_new.c SQLPGrammar_new.y SQLPScanner_new.l util_new.o
bison --verbose -d SQLPGrammar_new.y
......@@ -27,17 +21,5 @@ SQLPRefExp: SQLPRefExp_main.c SQLPRefExp.y SQLPScanner_new.l util_new.o
gcc -g -w SQLPRefExp_main.c util_new.o -o SQLPRefExp
rm -f lex.yy.c SQLPRefExp.tab.c SQLPRefExp.tab.h
util.o: util.c util.h
gcc -g -c util.c
util_new.o: util_new.c util_new.h
gcc -g -c util_new.c
Parser: parser.c util.o
gcc -g -w parser.c util.o -o Parser
clean:
rm -f lex.yy.c *.tab.c *.tab.h *.fasl Parser util.o util_new.o
%{
#include "util.h"
%}
%define api.value.type {cons_cell *}
%token IMPLIES OR AND NOT LE GE LT GT NE HAS MAX MIN AS ASC DESC MOD ASSIGN EQ STAR COMMA DOT
%token SIZE SELECTIVITY OVERLAP
%token FREQUENCY UNIT TIME SPACE
%token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF
%token STORE STORING DYNAMIC STATIC OF TYPE ORDERED BY
%token INDEX LIST ARRAY BINARY TREE DISTRIBUTED POINTER
%token SCHEMA CLASS ISA PROPERTIES CONSTRAINTS PROPERTY
%token ON DETERMINED COVER QUERY GIVEN FROM SELECT WHERE ORDER
%token PRECOMPUTED ONE EXIST FOR ALL TRANSACTION INTCLASS STRCLASS OID
%token INTEGER REAL DOUBLEREAL STRING MAXLEN RANGE TO SELF
%token INSERT END CHANGE DELETE DECLARE RETURN UNION
%start SQLPProgram
%%
SQLPProgram
: Query
{ printf("Input Query\n");
cons_cell *n = $1;
printf("Printing Tree\n");
print_cons_tree(n);
}
;
Query
: Union_Query
{ printf("Union Query\n");
$$ = $1;
}
;
Select_Query
: SELECT Select_List Body
{ printf("SQLP Query\n");
$$ = create_proj_operator($2, $3);
}
;
Body
: FROM TablePath
{ printf("Body 1\n");
$$ = $2;
}
| FROM TablePath WHERE Bool
{ printf("Body 2\n");
$$ = create_eval_operator($4, $2);
}
| FROM TablePath WHERE Pred
{ printf("Body 3\n");
$$ = create_eval_operator($4, $2);
}
;
TablePath
: TableIdentifier VarIdentifier
{ printf("last table path\n");
$$ = create_rename_operator($1, $2);
}
| TableIdentifier VarIdentifier ',' TablePath
{ printf("table path2\n");
$$ = create_cross_operator(create_rename_operator($1, $2), $4);
}
;
Union_Query
: Select_Query
{ printf("union query 1\n");
$$ = $1;
}
| Union_Query UNION Select_Query
{ printf("union query 2\n");
$$ = create_union_all_operator($1, $3);
}
;
Select_List
: STAR
{ printf("star\n");
//$$ = new_node(1, Select_List);
//$$->children[0] = new_node(0, STAR);
}
| Col
{ printf("select list attr path\n");
$$ = create_rename_operator($1, NULL);
}
| Col ',' Select_List
{ printf("Select list\n");
$$ = create_rename_operator($1, $2);
}
;
Col
: VarIdentifier '.' AttrPath
{
printf("col\n");
$$ = create_spcol($1, $3);
}
AttrPath
: AttrIdentifier
{ printf("path id\n");
$$ = create_pf($1, NULL);
}
| AttrIdentifier '.' AttrPath
{ printf("Path Function\n");
$$ = create_pf($1, $3);
}
;
VarIdentifier
: IDENTIFIER
{
printf("|%s| ", yytext);
$$ = create_var(yytext);
}
TableIdentifier
: IDENTIFIER
{
printf("|%s| ", yytext);
$$ = create_table(yytext);
}
AttrIdentifier
: IDENTIFIER
{
printf("|%s| ", yytext);
$$ = create_attr(yytext);
}
Operator
: EQ
{
$$ = create_op(yytext);
}
| NE
{
$$ = create_op(yytext);
}
| LE
{
$$ = create_op(yytext);
}
| GE
{
$$ = create_op(yytext);
}
| LT
{
$$ = create_op(yytext);
}
| GT
{
$$ = create_op(yytext);
}
;
Bool
: Col Operator Col
{ printf("Col op Col\n");
cons_cell* first_term = create_term($1);
cons_cell* second_term = create_term($3);
$$ = create_comp_operator($2, first_term, second_term, NULL);
}
| Col Operator Constant
{ printf("Col op Constant\n");
cons_cell* first_term = create_term($1);
$$ = create_comp_operator($2, first_term, $3, NULL);
}
;
Pred
: Bool AND Pred
{ printf("pred and pred\n");
$$ = $1;
$$->cdr->cdr->cdr->cdr->car = $3;
}
| NOT Pred
{ printf("Not Pred\n");
$$ = create_not_operator($1, NULL);
}
| EXIST '(' Query ')'
{ printf("Exist query\n");
$$ = create_atom("INPROG");
//$$->children[0] = new_node(0, EXIST);
//$$->children[1] = $3;
}
;
Constant
: INTEGER
{ printf("|%s|", yytext);
$$ = create_constant(yytext);
}
| REAL
{ printf("|%s|", yytext);
$$ = create_constant(yytext);
}
| STRING
{ printf("|%s|", yytext);
$$ = create_constant(yytext);
}
File deleted
/*
* Copyright (C) 1989, G. E. Weddell.
*
* This file is part of RDM.
*
* RDM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RDM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RDM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "util.h"
#include "lex.yy.c"
#include "SQLPGrammar.tab.c"
int main()
{
int Result;
strcpy(LineBuffer, "");
printf("(\n");
Result = yyparse();
if (Result)
printf("**error**");
else
fprintf(stderr, "checking semantics.\n");
printf(")\n");
return (Result);
}
%{
/*
* Copyright (C) 1989, G. E. Weddell.
*
* This file is part of RDM.
*
* RDM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RDM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RDM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SQLPGrammar_new.tab.h"
#include <stdio.h>
#include <ctype.h>
int LineNumber = 1;
char LineBuffer[200];
%}
%p 3000
D [0-9]
L [a-zA-Z_]
E [Ee][+-]?{D}+
%%
\%.*\n { LineNumber++; }
\n { LineNumber++; strcpy(LineBuffer, ""); }
"all" { strcat(LineBuffer, yytext); return(ALL); }
"and" { strcat(LineBuffer, yytext); return(AND); }
"array" { strcat(LineBuffer, yytext); return(ARRAY); }
"as" { strcat(LineBuffer, yytext); return(AS); }
"asc" { strcat(LineBuffer, yytext); return(ASC); }
"binary" { strcat(LineBuffer, yytext); return(BINARY); }
"by" { strcat(LineBuffer, yytext); return(BY); }
"change" { strcat(LineBuffer, yytext); return(CHANGE); }
"class" { strcat(LineBuffer, yytext); return(CLASS); }
"constraints" { strcat(LineBuffer, yytext); return(CONSTRAINTS); }
"cover" { strcat(LineBuffer, yytext); return(COVER); }
"declare" { strcat(LineBuffer, yytext); return(DECLARE); }
"delete" { strcat(LineBuffer, yytext); return(DELETE); }
"desc" { strcat(LineBuffer, yytext); return(DESC); }
"union" { strcat(LineBuffer, yytext); return(UNION); }
"determined" { strcat(LineBuffer, yytext); return(DETERMINED); }
"distributed" { strcat(LineBuffer, yytext); return(DISTRIBUTED); }
"dynamic" { strcat(LineBuffer, yytext); return(DYNAMIC); }
"exist" { strcat(LineBuffer, yytext); return(EXIST); }
"for" { strcat(LineBuffer, yytext); return(FOR); }
"frequency" { strcat(LineBuffer, yytext); return(FREQUENCY); }
"from" { strcat(LineBuffer, yytext); return(FROM); }
"given" { strcat(LineBuffer, yytext); return(GIVEN); }
"has" { strcat(LineBuffer, yytext); return(HAS); }
"implies" { strcat(LineBuffer, yytext); return(IMPLIES); }
"index" { strcat(LineBuffer, yytext); return(INDEX); }
"insert" { strcat(LineBuffer, yytext); return(INSERT); }
"Integer" { strcat(LineBuffer, yytext); return(INTCLASS); }
"isa" { strcat(LineBuffer, yytext); return(ISA); }
"list" { strcat(LineBuffer, yytext); return(LIST); }
"max" { strcat(LineBuffer, yytext); return(MAX); }
"maxlen" { strcat(LineBuffer, yytext); return(MAXLEN); }
"min" { strcat(LineBuffer, yytext); return(MIN); }
"mod" { strcat(LineBuffer, yytext); return(MOD); }
"not" { strcat(LineBuffer, yytext); return(NOT); }
"of" { strcat(LineBuffer, yytext); return(OF); }
"on" { strcat(LineBuffer, yytext); return(ON); }
"one" { strcat(LineBuffer, yytext); return(ONE); }
"or" { strcat(LineBuffer, yytext); return(OR); }
"order" { strcat(LineBuffer, yytext); return(ORDER); }
"ordered" { strcat(LineBuffer, yytext); return(ORDERED); }
"overlap" { strcat(LineBuffer, yytext); return(OVERLAP); }
"pointer" { strcat(LineBuffer, yytext); return(POINTER); }
"precomputed" { strcat(LineBuffer, yytext); return(PRECOMPUTED); }
"property" { strcat(LineBuffer, yytext); return(PROPERTY); }
"properties" { strcat(LineBuffer, yytext); return(PROPERTIES); }
"query" { strcat(LineBuffer, yytext); return(QUERY); }
"range" { strcat(LineBuffer, yytext); return(RANGE); }
"return" { strcat(LineBuffer, yytext); return(RETURN); }
"schema" { strcat(LineBuffer, yytext); return(SCHEMA); }
"select" { strcat(LineBuffer, yytext); return(SELECT); }
"selectivity" { strcat(LineBuffer, yytext); return(SELECTIVITY); }
"size" { strcat(LineBuffer, yytext); return(SIZE); }
"space" { strcat(LineBuffer, yytext); return(SPACE); }
"static" { strcat(LineBuffer, yytext); return(STATIC); }
"store" { strcat(LineBuffer, yytext); return(STORE); }
"storing" { strcat(LineBuffer, yytext); return(STORING); }
"String" { strcat(LineBuffer, yytext); return(STRCLASS); }
"time" { strcat(LineBuffer, yytext); return(TIME); }
"to" { strcat(LineBuffer, yytext); return(TO); }
"transaction" { strcat(LineBuffer, yytext); return(TRANSACTION); }
"tree" { strcat(LineBuffer, yytext); return(TREE); }
"type" { strcat(LineBuffer, yytext); return(TYPE); }
"unit" { strcat(LineBuffer, yytext); return(UNIT); }
"where" { strcat(LineBuffer, yytext); return(WHERE); }
"->" { strcat(LineBuffer, yytext); return(REF); }
{L}({L}|{D})* { strcat(LineBuffer, yytext); return(IDENTIFIER); }
{D}+ { strcat(LineBuffer, yytext); return(INTEGER); }
{D}+{E} { strcat(LineBuffer, yytext); return(REAL); }
{D}*"."{D}+({E})? { strcat(LineBuffer, yytext); return(REAL); }
{D}+"."{D}*({E})? { strcat(LineBuffer, yytext); return(REAL); }
\"(\\.|[^\\"])*\" { strcat(LineBuffer, yytext); return(STRING); }
":=" { strcat(LineBuffer, yytext); return(ASSIGN); }
"<=" { strcat(LineBuffer, yytext); return(LE); }
">=" { strcat(LineBuffer, yytext); return(GE); }
"<>" { strcat(LineBuffer, yytext); return(NE); }
";" { strcat(LineBuffer, yytext); return(';'); }
"{" { strcat(LineBuffer, yytext); return('{'); }
"}" { strcat(LineBuffer, yytext); return('}'); }
"," { strcat(LineBuffer, yytext); return(','); }
"=" { strcat(LineBuffer, yytext); return(EQ); }
"(" { strcat(LineBuffer, yytext); return('('); }
")" { strcat(LineBuffer, yytext); return(')'); }
"[" { strcat(LineBuffer, yytext); return('['); }
"]" { strcat(LineBuffer, yytext); return(']'); }
"." { strcat(LineBuffer, yytext); return('.'); }
"-" { strcat(LineBuffer, yytext); return('-'); }
"+" { strcat(LineBuffer, yytext); return('+'); }
"*" { strcat(LineBuffer, yytext); return(STAR); }
"/" { strcat(LineBuffer, yytext); return('/'); }
"<" { strcat(LineBuffer, yytext); return('<'); }
">" { strcat(LineBuffer, yytext); return('>'); }
" " { strcat(LineBuffer, yytext); }
"\r" { strcat(LineBuffer, yytext); }
. { strcat(LineBuffer, yytext);
yyerror("illegal character"); }
%%
yyerror(s)
char *s;
{
int i;
char c;
fprintf( stderr, "\n%s", LineBuffer );
c = input();
while ( c != '\n' && c != 0 ) {
putc(c, stderr);
c = input();
}
putc('\n', stderr);
for (i=1; i <= strlen( LineBuffer ) - strlen( yytext ); i++)
putc(' ', stderr);
for (i=1; i <= strlen( yytext ); i++)
putc('^', stderr);
putc('\n', stderr);
fprintf ( stderr, "** error: %s found in line %d.\n\n", s, LineNumber );
}
yywrap()
{
return(1);
}
\ No newline at end of file
No preview for this file type
%{
#include "util.h"
%}
// %union {
// struct node *n;
//}
%define api.value.type {struct node *}
%token IMPLIES OR AND NOT LE GE LT GT NE HAS MAX MIN AS ASC DESC MOD ASSIGN EQ STAR COMMA DOT
%token SIZE SELECTIVITY OVERLAP
%token FREQUENCY UNIT TIME SPACE
%token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF
%token STORE STORING DYNAMIC STATIC OF TYPE ORDERED BY
%token INDEX LIST ARRAY BINARY TREE DISTRIBUTED POINTER
%token SCHEMA CLASS ISA PROPERTIES CONSTRAINTS PROPERTY
%token ON DETERMINED COVER QUERY GIVEN FROM SELECT WHERE ORDER
%token PRECOMPUTED ONE EXIST FOR ALL TRANSACTION INTCLASS STRCLASS
%token INTEGER REAL DOUBLEREAL STRING MAXLEN RANGE TO
%token INSERT END CHANGE DELETE DECLARE RETURN UNION
%start SQLPProgram
%%
SQLPProgram
: Query
{ printf("Input Query\n");
struct node *n = new_node(1, SQLPProgram);
n->children[0] = $1;
printf("Printing Tree\n");
print_tree(n, 0);
struct linked_node *linked_tree = malloc(sizeof(struct linked_node));
translate(linked_tree, n);
print_linked_tree(linked_tree, 0);
}
;
Identifier
: IDENTIFIER
{ printf("|%s| ", yytext);
$$ = new_node(1, Identifier);
$$->children[0] = new_node(0, IDENTIFIER);
}
;
Query
: Union_Query
{ printf("Union Query\n");
$$ = new_node(1, Query);
$$->children[0] = $1;
}
;
Select_Query
: SELECT Select_List Body
{ printf("SQLP Query\n");
$$ = new_node(3, Select_Query);
$$->children[0] = new_node(0, SELECT);
$$->children[1] = $2;
$$->children[2] = $3;
}
;
Body
: FROM TablePath
{ printf("Body 1\n");
$$ = new_node(2, Body);
$$->children[0] = new_node(0, FROM);
$$->children[1] = $2;}
| FROM TablePath WHERE Bool
{ printf("Body 2\n");
$$ = new_node(4, Body);
$$->children[0] = new_node(0, FROM);
$$->children[1] = $2;
$$->children[2] = new_node(0, WHERE);
$$->children[3] = $4;
}
| FROM TablePath WHERE Pred
{ printf("Body 3\n");
$$ = new_node(4, Body);
$$->children[0] = new_node(0, FROM);
$$->children[1] = $2;
$$->children[2] = new_node(0, WHERE);
$$->children[3] = $4;
}
;
TablePath
: Identifier Identifier
{ printf("table path1\n");
$$ = new_node(2, TablePath);
$$->children[0] = $1;
$$->children[1] = $2;
}
| Identifier Identifier ',' TablePath
{ printf("table path2\n");
$$ = new_node(4, TablePath);
$$->children[0] = $1;
$$->children[1] = $2;
$$->children[2] = new_node(0, COMMA);
$$->children[3] = $4;
}
;
Union_Query
: Select_Query
{ printf("union query 1\n");
$$ = new_node(1, Union_Query);
$$->children[0] = $1;
}
| Union_Query UNION Select_Query
{ printf("union query 2\n");
$$ = new_node(3, Union_Query);
$$->children[0] = $1;
$$->children[1] = new_node(0, UNION);
$$->children[2] = $3;
}
;
Select_List
: STAR
{ printf("star\n");
$$ = new_node(1, Select_List);
$$->children[0] = new_node(0, STAR);
}
| AttrPath
{ printf("select list attr path\n");
$$ = new_node(1, Select_List);
$$->children[0] = $1;
}
| AttrPath ',' Select_List
{ printf("Select list\n");
$$ = new_node(3, Select_List);
$$->children[0] = $1;
$$->children[1] = new_node(0, COMMA);
$$->children[2] = $3;
}
;
AttrPath
: Identifier
{ printf("path id\n");
$$ = new_node(1, AttrPath);
$$->children[0] = $1;
}
| Identifier '.' AttrPath
{ printf("Path Function\n");
$$ = new_node(3, AttrPath);
$$->children[0] = $1;
$$->children[1] = new_node(0, DOT);
$$->children[2] = $3;
}
;
Operator
: EQ
| NE
| LE
| GE
| LT
| GT
;
Bool
: AttrPath Operator AttrPath
{ printf("AttrPath op AttrPath\n");
$$ = new_node(3, Bool);
$$->children[0] = $1;
$$->children[1] = $2;
$$->children[2] = $3;
}
| AttrPath Operator CONSTANT
{ printf("AttrPath op Constant\n");
$$ = new_node(3, Bool);
$$->children[0] = $1;
$$->children[1] = $2;
$$->children[2] = new_node(0, CONSTANT);
}
;
Pred
: Bool AND Pred
{ printf("pred and pred\n");
$$ = new_node(3, Pred);
$$->children[0] = $1;
$$->children[1] = new_node(0, AND);
$$->children[2] = $3;
}
| NOT Pred
{ printf("Not Pred\n");
$$ = new_node(2, Pred);
$$->children[0] = new_node(0, NOT);
$$->children[1] = $2;
}
| EXIST '(' Query ')'
{ printf("Exist query\n");
$$ = new_node(2, Pred);
$$->children[0] = new_node(0, EXIST);
$$->children[1] = $3;
}
;
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "parser.h"
cons_cell* parse(char* input, int size) {
if (input[0] == NULL) {
assert(size == 0);
return NULL;
}
if (input[0] == '(') {
// Nested case
int count_bracket = 1;
int it = 0;
// Find where the brackets enclose
while (count_bracket > 0) {
it++;
if (input[it] == '(') {
count_bracket++;
} else if (input[it] == ')') {
count_bracket--;
}
}
char buffer[it];
strncpy(buffer, &input[1], it - 1);
buffer[it - 1] = '\0';
// Recurse on what is enclosed by the brackets
cons_cell* car_cons = parse(buffer, it - 1);
// Copy everything after the brackets
int next_char = it + 1;
while (input[next_char] == ' ' && input[next_char] != NULL) {
next_char++;
}
char buffer_next[500];
strncpy(buffer_next, &input[next_char], size - next_char + 1);
// Recurse on everything after the brackets
cons_cell* cdr_cons = parse(buffer_next, size - next_char);
return create_cons_cell(car_cons, cdr_cons);
} else {
// Not nested case
// Copy the first term
int first_space_idx = 0;
while (input[first_space_idx] != ' ' && input[first_space_idx] != NULL) {
first_space_idx++;
}
char buff_first[500];
strncpy(buff_first, input, first_space_idx);
buff_first[first_space_idx] = '\0';
// Create cons_cell for first term
cons_cell* car_cons = create_cons_cell_w_atom(buff_first, NULL);
char buff_other[500];
int next_char = first_space_idx;
while (input[next_char] == ' ' && input[next_char] != NULL) {
next_char++;
}
// Copy everything after the first term
strncpy(buff_other, &input[next_char], size - next_char + 1);
// Recurse on everything after the first term
cons_cell* cdr_cons = parse(buff_other, size - next_char);
car_cons->cdr = cdr_cons;
return car_cons;
}
}
int main() {
char str[500];
printf("Enter the json-string to parse: \n");
scanf("%[^\n]s", str);
printf("You entered: %s\n", str);
int size = 0;
while (str[size] != NULL) {
size++;
}
cons_cell* cons = parse(str, size);
print_cons_tree(cons);
return 0;
}
#ifndef PARSER_H
#define PARSER_H
#include "util.h"
cons_cell* parse(char* input, int size);
#endif
\ No newline at end of file
#include "util.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;
}
// todo: why pass NULL
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;
}
// todo: ra2?
cons_cell* create_not_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] = "NOT\0";
cons_cell* operator_cons = create_cons_cell_w_atom(operator, ra1_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_H
#define UTIL_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* ra1, cons_cell* ra2);
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