diff --git a/SQLPGrammar.y b/SQLPGrammar.y index 2c4791e7b5f442d9021ad1ed172560a9f66009d4..6a5d9a5a50771f4e1251188c1fba1cd4ebee1731 100644 --- a/SQLPGrammar.y +++ b/SQLPGrammar.y @@ -21,11 +21,14 @@ SQLPQuery : Query { - $$ = NULL; + create_constant("TEST", "10"); + $$ = create_cons_cell_w_atom("SQLP_QUERY", create_cons_cell_w_atom("QUERY", $1)); + print_cons_tree($$); } | Body { - $$ = NULL; + $$ = create_cons_cell_w_atom("SQLP_QUERY", create_cons_cell_w_atom("BODY", $1)); + print_cons_tree($$); }; Query @@ -41,85 +44,85 @@ Query Body : Pred { - $$ = NULL; + $$ = create_list("PRED", $1); } | Atom { - $$ = NULL; + $$ = $1; } | NaturalJoinList { - $$ = NULL; + $$ = create_list("NATURAL_JOIN", $1); } | SubQuery { - $$ = NULL; + $$ = $1; }; Atom : TableIdentifier VarIdentifier { - $$ = NULL; + $$ = create_cons_cell_w_atom("ATOM", create_cons_cell($1, create_cons_cell($2, NULL))); }; NaturalJoinList - : NaturalJoinList JOIN '(' Body ')' + : '(' Body ')' JOIN NaturalJoinList { - $$ = NULL; + $$ = create_cons_cell($2, $5); } | '(' Body ')' { - $$ = NULL; + $$ = create_cons_cell($2, NULL); }; SubQuery : Query VarIdentifier { - $$ = NULL; + $$ = create_cons_cell_w_atom("SUB_QUERY", create_cons_cell($2, NULL)); }; Select : SELECT SetOrBag Limit AsList Body { - $$ = NULL; + $$ = create_cons_cell_w_atom("SELECT", create_cons_cell($2, create_cons_cell($3, create_cons_cell(create_list("AS", $4), create_cons_cell($5, NULL))))); }; SetOrBag : DISTINCT { - $$ = NULL; + $$ = create_cons_cell_w_atom("DISTINCT", NULL); } | ALL { - $$ = NULL; + $$ = create_cons_cell_w_atom("ALL", NULL); } | /* empty */ { - $$ = NULL; + $$ = create_cons_cell_w_atom("ALL", NULL); }; Limit - : LIMIT INTEGER + : LIMIT Integer { - $$ = NULL; + $$ = create_cons_cell_w_atom("LIMIT", $2); } | NOLIMIT { - $$ = NULL; + $$ = create_cons_cell_w_atom("NO_LIMIT", NULL); } | /* empty */ { - $$ = NULL; + $$ = create_cons_cell_w_atom("NO_LIMIT", NULL); }; AsList : As { - $$ = $1; + $$ = create_cons_cell($1, NULL); } - | AsList ',' As + | As ',' AsList { - $$ = NULL; + $$ = create_cons_cell($1, $3); }; As @@ -129,116 +132,121 @@ As } | Col AS AttrIdentifier { - $$ = NULL; + $$ = create_cons_cell_w_atom("AS", create_cons_cell($1, create_cons_cell($3, NULL))); }; Union : UNION SetOrBag Limit QueryList { - $$ = NULL; + $$ = create_cons_cell_w_atom("UNION", create_cons_cell($2, create_cons_cell($3, create_cons_cell(create_list("QUERY", $4), NULL)))); }; QueryList - : QueryList ',' '(' Query ')' + : '(' Query ')' ',' QueryList { - $$ = NULL; + $$ = create_cons_cell($2, $5); } | '(' Query ')' { - $$ = NULL; + $$ = create_cons_cell($2, NULL); } Col : VarIdentifier '.' AttributePath { - $$ = NULL; + $$ = create_cons_cell_w_atom("COL", create_cons_cell($1, create_cons_cell(create_list("ATTRIBUTE", $3), NULL))); }; -AttributePath - : AttrIdentifier - { - $$ = NULL; - }; - | AttributePath '.' AttrIdentifier - { - $$ = NULL; - }; - -VarIdentifier - : IDENTIFIER - { - $$ = NULL; - }; - - -TableIdentifier - : IDENTIFIER - { - $$ = NULL; - }; - -AttrIdentifier - : IDENTIFIER - { - $$ = NULL; - }; - Pred : Conj { - $$ = NULL; + $$ = create_cons_cell(create_list("CONJ", $1), NULL); } | '(' Conj OR Pred ')' { - $$ = NULL; + $$ = create_cons_cell(create_list("CONJ", $2), $4); }; Conj : BasicPred { - $$ = NULL; + $$ = create_cons_cell($1, NULL); } | '(' BasicPred AND Conj ')' { - $$ = NULL; + $$ = create_cons_cell($2, $4); } BasicPred : Term '=' Term { - $$ = NULL; + $$ = create_cons_cell_w_atom("EQUAL", create_cons_cell($1, create_cons_cell($3, NULL))); } | Term '<' Term { - $$ = NULL; + $$ = create_cons_cell_w_atom("LT", create_cons_cell($1, create_cons_cell($3, NULL))); } | Term LE Term { - $$ = NULL; + $$ = create_cons_cell_w_atom("LE", create_cons_cell($1, create_cons_cell($3, NULL))); } | EXISTS Body { - $$ = NULL; + $$ = create_cons_cell_w_atom("EXISTS", create_cons_cell($2, NULL)); } | NOT Pred { - $$ = NULL; + $$ = create_cons_cell_w_atom("NOT", create_cons_cell($2, NULL)); }; Term : INTEGER { - $$ = NULL; + $$ = create_constant("INTEGER", constant); } | REAL { - $$ = NULL; + $$ = create_constant("REAL", constant); } | STRING { - $$ = NULL; + $$ = create_constant("STRING", constant); } | Col { $$ = $1; }; + +AttributePath + : AttrIdentifier + { + $$ = create_cons_cell($1, NULL); + }; + | AttrIdentifier '.' AttributePath + { + $$ = create_cons_cell($1, $3); + }; + +VarIdentifier + : IDENTIFIER + { + $$ = create_identifier("VAR", identifier); + }; + +TableIdentifier + : IDENTIFIER + { + $$ = create_identifier("TABLE", identifier); + }; + +AttrIdentifier + : IDENTIFIER + { + $$ = create_identifier("ATTR", identifier); + }; + +Integer + : INTEGER + { + $$ = create_constant("INTEGER", constant); + }; diff --git a/util.c b/util.c index cd25ca23fc919c3a913847a3d49cade763eab6d8..5f82b6afedebe1c7a4cf557dff54a648cce32a4f 100644 --- a/util.c +++ b/util.c @@ -47,6 +47,12 @@ cons_cell* create_identifier(char *type, char *identifier) { return create_cons_cell_w_atom(str, cc); } +cons_cell * create_constant(char *type, char *val) { + printf("create_constant: %s, %s\n", type, val); + cons_cell* cc = create_cons_cell_w_atom(val, NULL); + return create_cons_cell_w_atom(type, cc); +} + cons_cell * create_list(char *type, cons_cell *list) { printf("create_list: %s\n", type); char str[100]; diff --git a/util.h b/util.h index 4b33bc97aae5f180596099a8024bff3032665797..0bf84525d79ca5f79244408ad9485e4652e209dc 100644 --- a/util.h +++ b/util.h @@ -35,6 +35,8 @@ cons_cell * cdr(cons_cell * cc); cons_cell * create_identifier(char *type, char *identifier); +cons_cell * create_constant(char *type, char *val); + cons_cell * create_list(char *type, cons_cell *list); void print_cons_tree(cons_cell *cc);