From c3128db80c1c184e80bc17c698fdf5fdc927b2f4 Mon Sep 17 00:00:00 2001
From: Tony Zhang <tony.zhang.domore@outlook.com>
Date: Thu, 27 Feb 2020 12:45:15 -0500
Subject: [PATCH] Fix parsing for Select List

---
 .gitignore                        |  1 +
 SQLPGrammar.y                     | 45 +++++++++++++------------------
 SQLPParserTests/TranslationQuery1 |  2 +-
 util.c                            |  9 +++++--
 util.h                            |  1 +
 5 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/.gitignore b/.gitignore
index 5fe5f83..c49cd8d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 *.o
+*.output
 .DS_Store
 SQLPParser.dSYM/
 SQLPRefExp.dSYM/
diff --git a/SQLPGrammar.y b/SQLPGrammar.y
index 641d6bd..2495421 100644
--- a/SQLPGrammar.y
+++ b/SQLPGrammar.y
@@ -135,45 +135,38 @@ UnionQuery
    	;
 
 SelectList
-	: Col
+	: SelectListElem ',' SelectList
 	{
-		printf("SelectList 1\n");
-		$$ = create_rename_operator($1, NULL);
+		printf("SelectListElem 1\n");
+		$$ = create_select_list($1, $3);
 	}
-	| Col AS AttrIdentifier
+	| SelectListElem
 	{
-		printf("SelectList 2\n");
-		$$ = create_as_operator($1, $3);
+		printf("SelectListElem 2\n");
+		$$ = create_select_list($1, NULL);
 	}
-	| Col ',' SelectList
+	;
+
+SelectListElem
+	: Col
 	{
-		printf("SelectList 3\n");
-		$$ = create_rename_operator($1, $2);
+		printf("SelectListElem 1\n");
+		$$ = $1;
 	}
-	| Col AS AttrIdentifier ',' SelectList
+	| Col AS AttrIdentifier
 	{
-		printf("SelectList 4\n");
-		$$ = create_rename_operator($1, create_as_operator($3, $5));
+		printf("SelectListElem 2\n");
+		$$ = create_rename_operator($1, $3);
 	}
 	| AttrIdentifier
 	{
-		printf("SelectList 1\n");
-		$$ = create_rename_operator($1, NULL);
+		printf("SelectListElem 3\n");
+		$$ = $1;
 	}
 	| AttrIdentifier AS AttrIdentifier
 	{
-		printf("SelectList 2\n");
-		$$ = create_as_operator($1, $3);
-	}
-	| AttrIdentifier ',' SelectList
-	{
-		printf("SelectList 3\n");
-		$$ = create_rename_operator($1, $2);
-	}
-	| AttrIdentifier AS AttrIdentifier ',' SelectList
-	{
-		printf("SelectList 4\n");
-		$$ = create_rename_operator($1, create_as_operator($3, $5));
+		printf("SelectListElem 4\n");
+		$$ = create_rename_operator($1, $3);
 	}
 	;
 
diff --git a/SQLPParserTests/TranslationQuery1 b/SQLPParserTests/TranslationQuery1
index 5f63cbc..450426f 100644
--- a/SQLPParserTests/TranslationQuery1
+++ b/SQLPParserTests/TranslationQuery1
@@ -1 +1 @@
-select x.ssn_num from Person x where x.std_info.name = "A" 
\ No newline at end of file
+select x.ssn_num, x.age from Person x where x.std_info.name = "A" 
diff --git a/util.c b/util.c
index f3ea651..51a29c9 100644
--- a/util.c
+++ b/util.c
@@ -87,6 +87,13 @@ cons_cell* create_spcol(cons_cell* var, cons_cell* pf) {
 	return operator_cons;
 }
 
+cons_cell* create_select_list(cons_cell* elem, cons_cell* lst) {
+	cons_cell* lst_cons = create_cons_cell(elem, lst);
+	char operator[] = "SELECT_LIST\0";
+	cons_cell* select_list_cons = create_cons_cell_w_atom(operator, lst_cons);
+	return select_list_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";
@@ -419,7 +426,6 @@ void print_cons_tree_helper(cons_cell *root, int indent) {
 		printf("\n");
 		print_cons_tree_helper(root->cdr, indent+1);
 	} else {
-		cons_cell* tmp = (cons_cell*)root->car;
 		print_cons_tree_helper((cons_cell*)root->car, indent);
 		print_cons_tree_helper(root->cdr, indent);
 	}
@@ -558,7 +564,6 @@ cons_list *list_tables_helper(cons_cell *root, cons_list *tablelst) {
 		}
 		list_tables_helper(root->cdr, tablelst);
 	} else {
-		cons_cell* tmp = (cons_cell*)root->car;
 		list_tables_helper((cons_cell*)root->car, tablelst);
 		list_tables_helper(root->cdr, tablelst);
 	}
diff --git a/util.h b/util.h
index 428629e..efa7571 100644
--- a/util.h
+++ b/util.h
@@ -77,6 +77,7 @@ algebra expressions for SQL queries:
 // 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_select_list(cons_cell* elem, cons_cell* lst);
 cons_cell* create_pf(cons_cell* attr, cons_cell* next_attr);
 cons_cell* create_table(char *table);
 cons_cell* create_term(cons_cell *term);
-- 
GitLab