Skip to content
Snippets Groups Projects
Commit baccbb01 authored by JasonJPu's avatar JasonJPu
Browse files

Add comments

parent b3e9314e
No related branches found
No related tags found
No related merge requests found
No preview for this file type
To run the parser, run `make` then run `./SQLPParser`. An example of a test input is `select s.name from student s where s.name = "John";`. Currently a syntax error will be reported when the semi-colon is read, but the parse tree that is outputted is correct. To run the SQLP parser, run `make` then run `./SQLPParser`. An example of a test input is `select s.name from student s where s.name = "John" and s.num = 10;`. Currently a syntax error will be reported when the semi-colon is read, but the parse tree that is outputted is correct.
The grammar is located in `SQLPGrammar.y`, and non-keyword types (such as `INTEGER`, `IDENTIFIER`, `STRING`) are defined in `SQLPScanner.l`. `util.c` contains the functions needed to build the con cell structure. The grammar is located in `SQLPGrammar.y`, and non-keyword types (such as `INTEGER`, `IDENTIFIER`, `STRING`) are defined in `SQLPScanner.l`. `util.c` contains the functions needed to build the con cell structure.
For parsing any general string into a tree of `cons_cells`, run `make` then run `./Parser`. An example of a test input is `PROJ (AS (SPCOL (VAR (s) PF (ATTR (name)))) ATOM (TABLE (student) VAR (s)))`. Every clause encosed in brackets is nested within a child `cons_cell`, and any two adjacent terms are connected via the `next` pointer.
A common way of defining the behavior of a process or thread in cases where A common way of defining the behavior of a process or thread in cases where
performance is critical is in terms of a collection of n+1 files containing performance is critical is in terms of a collection of n+1 files containing
code in the C programming language: n "module" files with a ".c" suffix that code in the C programming language: n "module" files with a ".c" suffix that
......
No preview for this file type
...@@ -10,8 +10,10 @@ cons_cell* parse(char* input, int size) { ...@@ -10,8 +10,10 @@ cons_cell* parse(char* input, int size) {
return NULL; return NULL;
} }
if (input[0] == '(') { if (input[0] == '(') {
// Nested case
int count_bracket = 1; int count_bracket = 1;
int it = 0; int it = 0;
// Find where the brackets enclose
while (count_bracket > 0) { while (count_bracket > 0) {
it++; it++;
if (input[it] == '(') { if (input[it] == '(') {
...@@ -23,16 +25,26 @@ cons_cell* parse(char* input, int size) { ...@@ -23,16 +25,26 @@ cons_cell* parse(char* input, int size) {
char buffer[it]; char buffer[it];
strncpy(buffer, &input[1], it - 1); strncpy(buffer, &input[1], it - 1);
buffer[it - 1] = '\0'; buffer[it - 1] = '\0';
// Recurse on what is enclosed by the brackets
cons_cell* car_cons = parse(buffer, it - 1); cons_cell* car_cons = parse(buffer, it - 1);
// Copy everything after the brackets
int next_char = it + 1; int next_char = it + 1;
while (input[next_char] == ' ' && input[next_char] != NULL) { while (input[next_char] == ' ' && input[next_char] != NULL) {
next_char++; next_char++;
} }
char buffer_next[500]; char buffer_next[500];
strncpy(buffer_next, &input[next_char], size - next_char + 1); 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); cons_cell* cdr_cons = parse(buffer_next, size - next_char);
return create_cons_cell(car_cons, cdr_cons); return create_cons_cell(car_cons, cdr_cons);
} else { } else {
// Not nested case
// Copy the first term
int first_space_idx = 0; int first_space_idx = 0;
while (input[first_space_idx] != ' ' && input[first_space_idx] != NULL) { while (input[first_space_idx] != ' ' && input[first_space_idx] != NULL) {
first_space_idx++; first_space_idx++;
...@@ -40,6 +52,8 @@ cons_cell* parse(char* input, int size) { ...@@ -40,6 +52,8 @@ cons_cell* parse(char* input, int size) {
char buff_first[500]; char buff_first[500];
strncpy(buff_first, input, first_space_idx); strncpy(buff_first, input, first_space_idx);
buff_first[first_space_idx] = '\0'; buff_first[first_space_idx] = '\0';
// Create cons_cell for first term
cons_cell* car_cons = create_cons_cell_w_atom(buff_first, NULL); cons_cell* car_cons = create_cons_cell_w_atom(buff_first, NULL);
char buff_other[500]; char buff_other[500];
int next_char = first_space_idx; int next_char = first_space_idx;
...@@ -47,7 +61,9 @@ cons_cell* parse(char* input, int size) { ...@@ -47,7 +61,9 @@ cons_cell* parse(char* input, int size) {
next_char++; next_char++;
} }
// Copy everything after the first term
strncpy(buff_other, &input[next_char], size - next_char + 1); 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); cons_cell* cdr_cons = parse(buff_other, size - next_char);
car_cons->cdr = cdr_cons; car_cons->cdr = cdr_cons;
return car_cons; return car_cons;
......
...@@ -32,6 +32,35 @@ atom* create_atom(char* val); ...@@ -32,6 +32,35 @@ atom* create_atom(char* val);
// Creates a cons_cell that has an atom as its car // Creates a cons_cell that has an atom as its car
cons_cell* create_cons_cell_w_atom(char* val, cons_cell* cdr); 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 // The following are all helper functions to create the
// various relational algebra expressions // various relational algebra expressions
cons_cell* create_spcol(cons_cell* var, cons_cell* pf); cons_cell* create_spcol(cons_cell* var, cons_cell* pf);
......
No preview for this file type
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