reformatted and split parser files

This commit is contained in:
Rebecca Schofield 2015-10-06 20:15:43 -05:00
parent 7efbf43c29
commit 47e8fd3c1e
5 changed files with 387 additions and 368 deletions

120
PHelpers.cpp Executable file
View file

@ -0,0 +1,120 @@
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "PHelpers.h"
using namespace std;
PAttribute::PAttribute(){
name = "~";
type = "~";
key = false;
size = 0;
}
PAttribute::PAttribute(string str, string t){
name = str;
type = t;
key = false;
size = 0;
}
PAttribute::PAttribute(string str, string t, int s){
name = str;
type = t;
key = false;
size = s;
}
PUnion::PUnion(){
Un1 = "~";
Un2 = "~";
}
PUnion::PUnion(string s1, string s2){
Un1 = s1;
Un2 = s2;
}
string PUnion::getPUnion(){
return "Union of " + Un1 + " and " + Un2;
}
PProduct::PProduct(){
Pr1 = "~";
Pr2 = "~";
}
PProduct::PProduct(string s1, string s2){
Pr1 = s1;
Pr2 = s2;
}
string PProduct::getPProduct(){
return "Product of " + Pr1 + " and " + Pr2;
}
PDifference::PDifference(){
D1 = "~";
D2 = "~";
}
PDifference::PDifference(string s1, string s2){
D1 = s1;
D2 = s2;
}
string PDifference::getPDifference(){
return "Difference of " + D1 + " and " + D2;
}
PRenaming::PRenaming(){
newName = "~";
oldName = "~";
}
PRenaming::PRenaming(string s1, string s2){
newName = s1;
oldName = s2;
}
string PRenaming::doRename(){
return "Renaming " + oldName + " to " + newName;
}
PProjection::PProjection(){
newName = "~";
oldName = "~";
}
PProjection::PProjection(string s1, string s2){
newName = s1;
oldName = s2;
}
string PProjection::doPProjection(){
return "Projecting " + oldName + " onto " + newName;
}
PComparison::PComparison(){
op.setPOp("~");
operand1.setPOperand("~");
operand2.setPOperand("~");
}
PComparison::PComparison(string str1, string str2, string str3){
operand1.setPOperand(str1);
op.setPOp(str2);
operand2.setPOperand(str3);
}
void PComparison::setPComparison(string str1, string str2, string str3){
operand1.setPOperand(str1);
op.setPOp(str2);
operand2.setPOperand(str3);
}
string PComparison::getPComparison(){
return operand1.getPOperand() + " " + op.getPOp() + " " + operand2.getPOperand();
}

165
PHelpers.h Executable file
View file

@ -0,0 +1,165 @@
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "DBEngine.h"
using namespace std;
class PRelation{
string name;
public:
PRelation(){ name = "~"; }
PRelation(string str){ name = str; }
void setPRelation(string str){ name = str; }
string getName(){ return name; }
};
class PAttribute{
string name;
string type;
bool key;
int size;
public:
PAttribute();
PAttribute(string str, string t);
PAttribute(string str, string t, int s);
void setPAttributeName(string str){ name = str; }
void setPAttributeType(string t){ type = t; }
void setPAttributeKey(){ key = true; }
void setPAttributeSize(int s){ size = s; }
string getPAttribute(){ return name; }
string getPAttributeType(){ return type; }
bool getPAttributeKey(){ return key; }
int getPAttributeSize(){ return size; }
};
class PUnion{
string Un1;
string Un2;
public:
PUnion();
PUnion(string s1, string s2);
string getPUnion();
};
class PProduct{
string Pr1;
string Pr2;
public:
PProduct();
PProduct(string s1, string s2);
string getPProduct();
};
class PDifference{
string D1;
string D2;
public:
PDifference();
PDifference(string s1, string s2);
string getPDifference();
};
class PRenaming{
string newName;
string oldName;
public:
PRenaming();
PRenaming(string s1, string s2);
string doRename();
};
class PProjection{
string newName;
string oldName;
public:
PProjection();
PProjection(string s1, string s2);
string doPProjection();
};
class POperand{
string op;
public:
POperand(){ op = "~"; }
POperand(string str){ op = str; }
void setPOperand(string str){ op = str; }
string getPOperand(){ return op; }
};
class POp{
string op;
public:
POp(){ op = "~"; }
POp(string str){ op = str; }
void setPOp(string str){ op = str; }
string getPOp(){ return op; }
};
class PComparison{
POp op;
POperand operand1;
POperand operand2;
public:
PComparison();
PComparison(string str1, string str2, string str3);
void setPComparison(string str1, string str2, string str3);
string getPComparison();
};
class PConjunction{
string conj;
public:
PConjunction(){ conj = "~"; }
PConjunction(string str){ conj = str; }
void setPConjunction(string str){ conj = str; }
string getPConjunction(){ return conj; }
};
class PCondition{
string cond;
public:
PCondition(){ cond = "~"; }
PCondition(string str){ cond = str; }
void setPCondition(string str){ cond = str; }
string getPCondition(){ return cond; }
};
class PSelection{
string select;
public:
PSelection(){ select = "~"; }
PSelection(string str){ select = str; }
string getPSelection(){ return select; }
};
class PExpression{
PRelation rel;
PSelection sel;
PProjection proj;
PRenaming ren;
PUnion un;
PDifference diff;
PProduct prod;
string temp;
public:
PExpression();
PExpression(string str){ temp = str; }
void setPExpression(string str){ temp = str; }
string getPExpression(){ return temp; }
};

View file

@ -1,16 +1,15 @@
#include <string> // std::string
#include <iostream> // std::cout
#include <fstream> // std::ofstream
#include <sstream> // std::stringstream
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "Parser.h"
#include <tuple>
#include <vector>
#include "Parser.h"
#include "PHelpers.h"
using namespace std;
vector<string> tokenize(string ss)
{
vector<string> tokenize(string ss){
string tempString;
stringstream lineStream(ss);
vector<string> output;
@ -22,8 +21,7 @@ vector<string> tokenize(string ss)
return output;
}
void displayTokenList(vector<string> input)
{
void displayTokenList(vector<string> input){
cout<<"TokenList: "<<endl;
for (int i = 0; i < input.size(); ++i)
{
@ -31,8 +29,7 @@ void displayTokenList(vector<string> input)
}
}
Relation condition(vector<string> input, Relation &r, DBEngine &engine)
{
Relation condition(vector<string> input, Relation &r, DBEngine &engine){
Relation rfinal = r;
Attribute a1 = r.getAttributeByName(input[0]);
@ -41,19 +38,16 @@ Relation condition(vector<string> input, Relation &r, DBEngine &engine)
input.erase(input.begin());
Attribute a2;
string c;
if(r.isAttribute(input[0]))
{
if(r.isAttribute(input[0])){
a2 = r.getAttributeByName(input[0]);
input.erase(input.begin());
for(int i = 0; i < r.getAttributes().size(); ++i)
{
if(r.getAttributes()[i].getName() == a1.getName())
{
for(int i = 0; i < r.getAttributes().size(); ++i){
if(r.getAttributes()[i].getName() == a1.getName()){
int offset = 0;
if(op == "==")
{
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x)
{
if(op == "=="){
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
if(r.getAttributeByName(a1.getName()).getValues()[x] != r.getAttributeByName(a2.getName()).getValues()[x])
{
rfinal.removeTuple(x - offset);
@ -61,90 +55,76 @@ Relation condition(vector<string> input, Relation &r, DBEngine &engine)
}
}
}
else if(op == "!=")
{
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x)
{
if(r.getAttributeByName(a1.getName()).getValues()[x] == r.getAttributeByName(a2.getName()).getValues()[x])
{
else if(op == "!="){
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
if(r.getAttributeByName(a1.getName()).getValues()[x] == r.getAttributeByName(a2.getName()).getValues()[x]){
rfinal.removeTuple(x - offset);
offset += 1;
}
}
}
else if(op == ">=")
{
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER")
{
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x)
{
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) < stoi(r.getAttributeByName(a2.getName()).getValues()[x]))
{
else if(op == ">="){
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER"){
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) < stoi(r.getAttributeByName(a2.getName()).getValues()[x])){
rfinal.removeTuple(x - offset);
offset += 1;
}
}
}
else
{
cout << "Attribute type is not an INTEGER." << endl;
exit(1);
}
}
else if(op == "<=")
{
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER")
{
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x)
{
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) > stoi(r.getAttributeByName(a2.getName()).getValues()[x]))
{
rfinal.removeTuple(x - offset);
offset += 1;
}
}
}
else
{
cout << "Attribute type is not an INTEGER." << endl;
exit(1);
}
}
else if(op == ">")
{
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER")
{
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x)
{
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) <= stoi(r.getAttributeByName(a2.getName()).getValues()[x]))
{
rfinal.removeTuple(x - offset);
offset += 1;
}
}
}
else
{
else{
cout << "Attribute type is not an INTEGER." << endl;
exit(1);
}
}
else if(op == "<")
{
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER")
{
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x)
{
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) >= stoi(r.getAttributeByName(a2.getName()).getValues()[x]))
{
else if(op == "<="){
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER"){
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) > stoi(r.getAttributeByName(a2.getName()).getValues()[x])){
rfinal.removeTuple(x - offset);
offset += 1;
}
}
}
else
{
else{
cout << "Attribute type is not an INTEGER." << endl;
exit(1);
}
}
else if(op == ">"){
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER"){
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) <= stoi(r.getAttributeByName(a2.getName()).getValues()[x])){
rfinal.removeTuple(x - offset);
offset += 1;
}
}
}
else{
cout << "Attribute type is not an INTEGER." << endl;
exit(1);
}
}
else if(op == "<"){
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER" && r.getAttributeByName(a2.getName()).getType() == "INTEGER"){
for(int x = 0; x < r.getAttributeByName(a1.getName()).getSize(); ++x){
if(stoi(r.getAttributeByName(a1.getName()).getValues()[x]) >= stoi(r.getAttributeByName(a2.getName()).getValues()[x])){
rfinal.removeTuple(x - offset);
offset += 1;
}
}
}
else{
cout << "Attribute type is not an INTEGER." << endl;
exit(1);
}
@ -152,22 +132,17 @@ Relation condition(vector<string> input, Relation &r, DBEngine &engine)
}
}
}
else if(input[0].at(0) == '\"')
{
else if(input[0].at(0) == '\"'){
c = input[0].substr(1, input[0].find_last_of("\"") - 1);
input.erase(input.begin());
int offset = 0;
//input.erase(input.begin());
if(op == "==")
{
for(int i = 0; i < r.getAttributes().size(); ++i)
{
if(r.getAttributes()[i].getName() == a1.getName())
{
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j)
{
if(r.getAttributeByName(a1.getName()).getValues()[j] != c)
{
if(op == "=="){
for(int i = 0; i < r.getAttributes().size(); ++i){
if(r.getAttributes()[i].getName() == a1.getName()){
for(int j = 0; j < r.getAttributeByName(a1.getName()).getValues().size(); ++j){
if(r.getAttributeByName(a1.getName()).getValues()[j] != c){
rfinal.removeTuple(j - offset);
offset += 1;
}
@ -175,6 +150,9 @@ Relation condition(vector<string> input, Relation &r, DBEngine &engine)
}
}
}
//continue stylistic homogenization here
else if(op == "!=")
{
for(int i = 0; i < r.getAttributes().size(); ++i)
@ -192,6 +170,7 @@ Relation condition(vector<string> input, Relation &r, DBEngine &engine)
}
}
}
else if(op == ">=")
{
if(r.getAttributeByName(a1.getName()).getType() == "INTEGER")
@ -733,10 +712,9 @@ vector<string> createCMD(vector<string> input, DBEngine &engine)
return input;
}
else cout<<"Syntax error! 2"<<endl;
else cout<<"Syntax error! 2"<<endl; //refine
}
else cout<<"Syntax error! 1"<<endl;
else cout<<"Syntax error! 1"<<endl; //refine
}
vector<string> insertCMD(vector<string> input, DBEngine &engine)

290
Parser.h
View file

@ -1,267 +1,23 @@
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream
#include <vector>
#include <string>
#include "DBEngine.h"
using namespace std;
class PRelation
{
string name;
public:
PRelation() { name = "~"; }
PRelation(string str) { name = str; }
void setPRelation(string str) { name = str; }
string getName() { return name; }
};
class PAttribute
{
string name;
string type;
bool key;
int size;
public:
PAttribute() {
name = "~";
type = "~";
key = false;
size = 0;
}
PAttribute(string str, string t) {
name = str;
type = t;
key = false;
size = 0;
}
PAttribute(string str, string t, int s) {
name = str;
type = t;
key = false;
size = s;
}
void setPAttributeName(string str) { name = str; }
void setPAttributeType(string t) { type = t; }
void setPAttributeKey() { key = true; }
void setPAttributeSize(int s) { size = s; }
string getPAttribute() { return name; }
string getPAttributeType() { return type; }
bool getPAttributeKey() { return key; }
int getPAttributeSize() { return size; }
};
class PUnion
{
string Un1;
string Un2;
public:
PUnion() {
Un1 = "~";
Un2 = "~";
}
PUnion(string s1, string s2) {
Un1 = s1;
Un2 = s2;
}
string getPUnion() {
return "Union of " + Un1 + " and " + Un2;
}
};
class PProduct
{
string Pr1;
string Pr2;
public:
PProduct() {
Pr1 = "~";
Pr2 = "~";
}
PProduct(string s1, string s2) {
Pr1 = s1;
Pr2 = s2;
}
string getPProduct() {
return "Product of " + Pr1 + " and " + Pr2;
}
};
class PDifference
{
string D1;
string D2;
public:
PDifference() {
D1 = "~";
D2 = "~";
}
PDifference(string s1, string s2) {
D1 = s1;
D2 = s2;
}
string getPDifference() {
return "Difference of " + D1 + " and " + D2;
}
};
class PRenaming
{
string newName;
string oldName;
public:
PRenaming() {
newName = "~";
oldName = "~";
}
PRenaming(string s1, string s2) {
newName = s1;
oldName = s2;
}
string doRename() {
return "Renaming " + oldName + " to " + newName;
}
};
class PProjection
{
string newName;
string oldName;
public:
PProjection() {
newName = "~";
oldName = "~";
}
PProjection(string s1, string s2) {
newName = s1;
oldName = s2;
}
string doPProjection() {
return "Projecting " + oldName + " onto " + newName;
}
};
class POperand
{
string op;
public:
POperand() { op = "~"; }
POperand(string str) { op = str; }
void setPOperand(string str) { op = str; }
string getPOperand() { return op; }
};
class POp
{
string op;
public:
POp() { op = "~"; }
POp(string str) { op = str; }
void setPOp(string str) { op = str; }
string getPOp() { return op; }
};
class PComparison
{
POp op;
POperand operand1;
POperand operand2;
public:
PComparison() {
op.setPOp("~");
operand1.setPOperand("~");
operand2.setPOperand("~");
}
PComparison(string str1, string str2, string str3) {
operand1.setPOperand(str1);
op.setPOp(str2);
operand2.setPOperand(str3);
}
void setPComparison(string str1, string str2, string str3) {
operand1.setPOperand(str1);
op.setPOp(str2);
operand2.setPOperand(str3);
}
string getPComparison() {
return operand1.getPOperand() + " " + op.getPOp() + " " + operand2.getPOperand();
}
};
class PConjunction
{
string conj;
public:
PConjunction() { conj = "~"; }
PConjunction(string str) { conj = str; }
void setPConjunction(string str) { conj = str; }
string getPConjunction() { return conj; }
};
class PCondition
{
string cond;
public:
PCondition() { cond = "~"; }
PCondition(string str) { cond = str; }
void setPCondition(string str) { cond = str; }
string getPCondition() { return cond; }
};
class PSelection
{
string select;
public:
PSelection() { select = "~"; }
PSelection(string str) { select = str; }
string getPSelection() { return select; }
};
class PExpression
{
PRelation rel;
PSelection sel;
PProjection proj;
PRenaming ren;
PUnion un;
PDifference diff;
PProduct prod;
string temp;
public:
PExpression();
PExpression(string str) { temp = str; }
void setPExpression(string str) { temp = str; }
string getPExpression() { return temp; }
};
void parse(string s, DBEngine &e);
void parseList(vector<string> input, DBEngine &engine);
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "DBEngine.h"
std::vector<std::string> tokenize(std::string ss);
void displayTokenList(std::vector<std::string> input);
Relation condition(std::vector<std::string> input, Relation &r, DBEngine &engine);
tuple<std::vector<std::string>, Relation> expression(std::vector<std::string> input, DBEngine &engine);
std::vector<std::string> showCMD(std::vector<std::string> input, DBEngine &engine);
std::vector<std::string> saveCMD(std::vector<std::string> input, DBEngine &engine);
std::vector<std::string> closeCMD(std::vector<std::string> input, DBEngine &engine);
std::vector<std::string> openCMD(std::vector<std::string> input, DBEngine &engine);
std::vector<std::string> exitCMD(std::vector<std::string> input, DBEngine &engine);
std::vector<std::string> createCMD(std::vector<std::string> input, DBEngine &engine);
std::vector<std::string> insertCMD(std::vector<std::string> input, DBEngine &engine);
std::vector<std::string> updateCMD(std::vector<std::string> input, DBEngine &engine);
std::vector<std::string> deleteCMD(std::vector<std::string> input, DBEngine &engine);
std::vector<std::string> query(std::vector<std::string> input, DBEngine &engine);
void par_line(std::vector<std::string> input, DBEngine &engine);
void parse(std::string s, DBEngine &e);
void parseList(std::vector<std::string> input, DBEngine &engine);

BIN
a.out

Binary file not shown.