Merge branch 'master' of https://github.tamu.edu/scho4077/DMS
This commit is contained in:
commit
c846e72eb8
2 changed files with 553 additions and 64 deletions
188
Parserv3.h
188
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;
|
||||
}
|
||||
};
|
||||
|
|
429
Parserv4.cpp
Normal file
429
Parserv4.cpp
Normal file
|
@ -0,0 +1,429 @@
|
|||
#include <string> // std::string
|
||||
#include <iostream> // std::cout
|
||||
#include <sstream> // std::stringstream
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "Parserv3.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<string> tokenize(string ss)
|
||||
{
|
||||
string tempString;
|
||||
stringstream lineStream(ss);
|
||||
vector<string> output;
|
||||
|
||||
while (lineStream >> tempString)
|
||||
{
|
||||
output.push_back(tempString);
|
||||
}
|
||||
|
||||
//testing---------------
|
||||
cout<<"TokenList: ";
|
||||
|
||||
for (int i = 0; i <output.size(); ++i)
|
||||
{
|
||||
cout<<output[i]<<" ";
|
||||
}
|
||||
//----------------------
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
void displayTokenList(vector<string> input)
|
||||
{
|
||||
cout<<"TokenList: "<<endl;
|
||||
for (int i = 0; i < input.size(); ++i)
|
||||
{
|
||||
cout<<input[i]<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
vector<string> showCMD(vector<string> input)
|
||||
{
|
||||
if (input.size() > 3)
|
||||
{
|
||||
cout<<"Syntax error!"<<endl;
|
||||
}
|
||||
|
||||
cout<<"\nPassing the following arguments to dbEngine: "<<endl;
|
||||
cout<<"command :"<<input[0]<<endl;
|
||||
cout<<"argument: "<<input[1]<<endl;
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
vector<string> saveCMD(vector<string> input)
|
||||
{
|
||||
if (input.size() > 3)
|
||||
{
|
||||
cout<<"Syntax error!"<<endl;
|
||||
}
|
||||
|
||||
cout<<"\nPassing the following arguments to dbEngine: "<<endl;
|
||||
cout<<"command :"<<input[0]<<endl;
|
||||
cout<<"argument: "<<input[1]<<endl;
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
vector<string> closeCMD(vector<string> input)
|
||||
{
|
||||
if (input.size() > 3)
|
||||
{
|
||||
cout<<"Syntax error!"<<endl;
|
||||
}
|
||||
|
||||
cout<<"\nPassing the following arguments to dbEngine: "<<endl;
|
||||
cout<<"command :"<<input[0]<<endl;
|
||||
cout<<"argument: "<<input[1]<<endl;
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
vector<string> openCMD(vector<string> input)
|
||||
{
|
||||
if (input.size() > 3)
|
||||
{
|
||||
cout<<"Syntax error!"<<endl;
|
||||
}
|
||||
|
||||
cout<<"\nPassing the following arguments to dbEngine: "<<endl;
|
||||
cout<<"command :"<<input[0]<<endl;
|
||||
cout<<"argument: "<<input[1]<<endl;
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
vector<string> exitCMD(vector<string> input)
|
||||
{
|
||||
if (input[1] != ";")
|
||||
{
|
||||
cout<<"ERROR: missing semicolon!"<<endl;
|
||||
}
|
||||
|
||||
if (input[0] != "EXIT") { cout<<"Wrong function/syntax error!"<<endl;}
|
||||
cout<<"Passing command: "<<input[0]<<endl;
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
|
||||
vector<string> createCMD(vector<string> 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 <Expression> 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 <Expression> 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!"<<endl;
|
||||
}
|
||||
else cout<<"Syntax error!"<<endl;
|
||||
|
||||
}
|
||||
|
||||
vector<string> insertCMD(vector<string> 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 <Expression> 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!"<<endl;
|
||||
}
|
||||
|
||||
cout<<"Syntax error!"<<endl;
|
||||
|
||||
}
|
||||
|
||||
else cout<<"Syntax error!"<<endl;
|
||||
|
||||
}
|
||||
|
||||
vector<string> updateCMD(vector<string> 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 <Expression> 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!"<<endl;
|
||||
}
|
||||
|
||||
vector<string> deleteCMD(vector<string> 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 <Expression> 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!"<<endl;
|
||||
}
|
||||
//parser relation name into Relation r
|
||||
|
||||
//parse out WHERE
|
||||
|
||||
//parse condition into Condition c
|
||||
|
||||
//cout "Deleting [c] from [r]"
|
||||
|
||||
//return input
|
||||
}
|
||||
|
||||
void par_line(vector<string> 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 "<<endl;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
//command
|
||||
// if ( input[0] == "open-cmd" || input[0] == "close-cmd" ||
|
||||
// input[0] == "save-cmd" || input[0] == "exit-cmd" ||
|
||||
// input[0] == "show-cmd" || input[0] == "create-cmd" ||
|
||||
// input[0] == "update-cmd" || input[0] == "insert-cmd" ||
|
||||
// input[0] == "delete-cmd" )
|
||||
// {
|
||||
// par_command(input[0]);
|
||||
// }
|
||||
|
||||
//query
|
||||
// if ( input[0] == "select" || input[0] == "project" ||
|
||||
// input[0] == "product" || input[0] == "difference" ||
|
||||
// input[0] == "union" || input[0] == "renaming" ||
|
||||
// input[0] == "update-cmd" || input[0] == "insert-cmd" ||
|
||||
// input[0] == "delete-cmd" )
|
||||
// {
|
||||
// par_query(input[0]);
|
||||
// }
|
||||
|
||||
if ( input[0] == "INSERT")
|
||||
{
|
||||
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
||||
input.erase(input.begin());
|
||||
vector<string> insertInput = insertCMD(input);
|
||||
cout<<"arguments: "<<endl;
|
||||
displayTokenList(insertInput);
|
||||
|
||||
}
|
||||
|
||||
if ( input[0] == "CREATE")
|
||||
{
|
||||
cout<<"\nPassing the following arguments to dbEngine: \nCommand: "<<input[0]<<endl;
|
||||
input.erase(input.begin());
|
||||
input.erase(input.begin());
|
||||
|
||||
vector<string> insertInput = insertCMD(input);
|
||||
cout<<"arguments: "<<endl;
|
||||
displayTokenList(insertInput);
|
||||
|
||||
}
|
||||
|
||||
if ( input[0] == "SHOW")
|
||||
{
|
||||
showCMD(input);
|
||||
}
|
||||
|
||||
if ( input[0] == "EXIT")
|
||||
{
|
||||
exitCMD(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main () {
|
||||
|
||||
|
||||
string ss = "INSERT INTO animals VALUES FROM ( Joe , cat , 4 ) ;";
|
||||
string ss2 = "SHOW Dogs ;";
|
||||
string ss3 = "EXIT ; ";
|
||||
|
||||
vector<string> listOfTokens = tokenize(ss);
|
||||
vector<string> listOfTokens2 = tokenize(ss2);
|
||||
vector<string> listOfTokens3 = tokenize(ss3);
|
||||
|
||||
par_line(listOfTokens);
|
||||
par_line(listOfTokens2);
|
||||
par_line(listOfTokens3);
|
||||
|
||||
|
||||
}
|
Reference in a new issue