Commit b2bb5ec4 authored by Tony Zhang's avatar Tony Zhang
Browse files

Fix Query Parser

parent cfbb3e6e
......@@ -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);
};
......@@ -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];
......
......@@ -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);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment