diff --git a/Parserv3.h b/Parserv3.h index 1fb63d7..830f98e 100644 --- a/Parserv3.h +++ b/Parserv3.h @@ -11,38 +11,63 @@ class Relation { string name; - Relation(string str) - { - name = str; - } - - void setRelation(string str) - { - name = str; - } - string getName() - { - return name; - } + public: + Relation() + { + } + + Relation(string str) + { + name = str; + } + + void setRelation(string str) + { + name = str; + } + string getName() + { + return name; + } }; - +class Attribute +{ + string name; + + public: + Attribute(string str) + { + name = str; + } + + void setAttribute(string str) + { + name = str; + } + + string getAttribute() + { + return name; + } +}; class Union { string Un1; string Un2; - Union (string s1, string s2) - { - Un1 = s1; - Un2 = s2; - } - - string getUnion() - { - return "Union of " + Un1 + " and " + Un2; - } + public: + Union (string s1, string s2) + { + Un1 = s1; + Un2 = s2; + } + + string getUnion() + { + return "Union of " + Un1 + " and " + Un2; + } }; class Product @@ -50,16 +75,17 @@ class Product string Pr1; string Pr2; - Product(string s1, string s2) - { - Pr1 = s1; - Pr2 = s2; - } - - string getProduct() - { - return "Product of " + Pr1 + " and " + Pr2; - } + public: + Product(string s1, string s2) + { + Pr1 = s1; + Pr2 = s2; + } + + string getProduct() + { + return "Product of " + Pr1 + " and " + Pr2; + } }; class Difference @@ -67,16 +93,17 @@ class Difference string D1; string D2; - Difference(string s1, string s2) - { - D1 = s1; - D2 = s2; - } - - string getDifference() - { - return "Difference of " + D1 + " and " + D2; - } + public: + Difference(string s1, string s2) + { + D1 = s1; + D2 = s2; + } + + string getDifference() + { + return "Difference of " + D1 + " and " + D2; + } }; class Renaming @@ -84,16 +111,17 @@ class Renaming string newName; string oldName; - Renaming(string s1, string s2) - { - newName = s1; - oldName = s2; - } - - string doRename() - { - return "Renaming " + oldName + " to " + newName; - } + public: + Renaming(string s1, string s2) + { + newName = s1; + oldName = s2; + } + + string doRename() + { + return "Renaming " + oldName + " to " + newName; + } }; class Projection @@ -101,16 +129,17 @@ class Projection string newName; string oldName; - Projection(string s1, string s2) - { - newName = s1; - oldName = s2; - } - - string doProjection() - { - return "Projecting " + oldName + " onto " + newName; - } + public: + Projection(string s1, string s2) + { + newName = s1; + oldName = s2; + } + + string doProjection() + { + return "Projecting " + oldName + " onto " + newName; + } }; class Operand @@ -216,6 +245,11 @@ class Condition { string cond; public: + + Condition() + { + } + Condition(string str) { cond = str; @@ -246,3 +280,29 @@ class Selection return select; } }; + +class Expression +{ + string exp; + + public: + + Expression() + { + } + + Expression(string str) + { + exp = str; + } + + void setExpression(string str) + { + exp = str; + } + + string getExpression() + { + return exp; + } +}; diff --git a/Parserv4.cpp b/Parserv4.cpp new file mode 100644 index 0000000..8d5f17b --- /dev/null +++ b/Parserv4.cpp @@ -0,0 +1,429 @@ +#include // std::string +#include // std::cout +#include // std::stringstream +#include +#include +#include "Parserv3.h" + +using namespace std; + +vector tokenize(string ss) +{ + string tempString; + stringstream lineStream(ss); + vector output; + + while (lineStream >> tempString) + { + output.push_back(tempString); + } + + //testing--------------- + cout<<"TokenList: "; + + for (int i = 0; i input) +{ + cout<<"TokenList: "< showCMD(vector input) +{ + if (input.size() > 3) + { + cout<<"Syntax error!"< saveCMD(vector input) +{ + if (input.size() > 3) + { + cout<<"Syntax error!"< closeCMD(vector input) +{ + if (input.size() > 3) + { + cout<<"Syntax error!"< openCMD(vector input) +{ + if (input.size() > 3) + { + cout<<"Syntax error!"< exitCMD(vector input) +{ + if (input[1] != ";") + { + cout<<"ERROR: missing semicolon!"< createCMD(vector input) +{ + //relation name will be the first element of the vector of data returned by this function + + if (input[0] == "CREATE" && input[1] == "TABLE") + { + input.erase(input.begin()); + input.erase(input.begin()); + + Relation r; + r.setRelation(input[0]); + + input.erase(input.begin()); + + if(input[0] == "(") + { + input.erase(input.begin()); + + vector e1; + + while(input[0] != ")") //inserting all values to relation + { + if (input[0] == ",") + { + input.erase(input.begin()); + } + + e1.push_back(input[0]); + + input.erase(input.begin()); + } + + vector e2; + + if(input[0] == "PRIMARY" && input[1] == "KEY") + { + input.erase(input.begin()); + input.erase(input.begin()); + + if(input[0] == "(") + { + + while(input[0] != ")") //inserting all values to relation + { + if (input[0] == ",") + { + input.erase(input.begin()); + } + + e2.push_back(input[0]); + + input.erase(input.begin()); + } + + cout << "Creating a table: " << r.getName() << "\n"; + cout << "Primary key: "; + while(!e2.empty()) + { + cout << e2[0].getExpression() << " "; + e2.erase(e2.begin()); + } + + return input; + } + } + } + + else cout<<"Syntax error!"< insertCMD(vector input) +{ + //relation name will be the first element of the vector of data returned by this function + + if (input[0] == "INTO") + { + input.erase(input.begin()); + + Relation r(input[0]); + + input.erase(input.begin()); + + if (input[0] == "VALUES" && input[1] == "FROM") + { + input.erase(input.begin()); + input.erase(input.begin()); + + + if(input[0] == "(") + { + vector e; + input.erase(input.begin()); + + while(input[0] != ")") //inserting all values to relation + //for (int i = 0; i < 2; ++i) + { + if (input[0] == ",") input.erase(input.begin()); + + e.push_back(input[0]); + + input.erase(input.begin()); + } + + cout << "Inserting: "; + while(!e.empty()) + { + cout << e[0].getExpression() << " "; + e.erase(e.begin()); + } + cout << "into " << r.getName() << ".\n"; + + return input; + + } + + else if (input[0] == "RELATION") + { + input.erase(input.begin()); + + Expression e; + + while(input[0] != ";") + { + e.setExpression(e.getExpression() + input[0]); + } + + cout << "Inserting: " << e.getExpression() << " into " << r.getName() << ".\n"; + + return input; + } + + else cout<<"Syntax error!"< updateCMD(vector input) +{ + //parse Relation r + + //parse out SET + if(input[0] == "SET") + { + input.erase(input.begin()); + + Relation r(input[0]); + + input.erase(input.begin()); + + //parse out ( and send everything until ) into an Expression vector + if(input[0] == "(") + { + + vector e; + input.erase(input.begin()); + + while(input[0] != ")") + { + e.push_back(input[0]); + } + } + + if(input[0] == "WHERE") + { + Condition c; + while(input[0] != ";") + { + c.setCondition(/*c.getCondition +*/ input[0]); + } + } + } + + else cout<<"Syntax error!"< deleteCMD(vector input) +{ + // parse out DELETE FROM + if (input[0] == "DELETE" && input[1] == "FROM") + { + input.erase(input.begin()); + input.erase(input.begin()); + + Relation r(input[0]); + if(input[0] == "(") + { + vector e; + input.erase(input.begin()); + + while(input[0] != ")") + { + if (input[0] == ",") input.erase(input.begin()); + + e.push_back(input[0]); + + input.erase(input.begin()); + } + + cout << "Deleting: "; + while(!e.empty()) + { + cout << e[0].getExpression() << " "; + e.erase(e.begin()); + } + cout << "from " << r.getName() << ".\n"; + + return input; + + } + else cout<<"Syntax error!"< input) //calls par_command() or par_query() depending on first item from token list +{ +/* +• Match the first item in the token list and determine weather this is a command or a query. +• Call functions par_command() or par_query(); +• After either par_command() or par_query() returns, make sure the line ends properly with “;” token +*/ + string tempChar = input.back(); + + if (tempChar != ";") + { + cout<<"ERROR! missing semicolon "< insertInput = insertCMD(input); + cout<<"arguments: "< insertInput = insertCMD(input); + cout<<"arguments: "< listOfTokens = tokenize(ss); + vector listOfTokens2 = tokenize(ss2); + vector listOfTokens3 = tokenize(ss3); + + par_line(listOfTokens); + par_line(listOfTokens2); + par_line(listOfTokens3); + + +}