adding cs236

This commit is contained in:
dm
2016-04-06 20:46:32 -07:00
parent cf99ec6565
commit 8af25fbb22
56 changed files with 2313 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
#ifndef __FACT_H__
#define __FACT_H__
#include "predicate.h"
#include <vector>
#include <sstream>
using namespace std;
class fact {
public:
vector<predicate> pred_facts;
};
#endif
+13
View File
@@ -0,0 +1,13 @@
#ifndef __PARAMETER_H__
#define __PARAMETER_H__
#include <iostream>
using namespace std;
class parameter {
public:
string param;
string type;
};
#endif
+200
View File
@@ -0,0 +1,200 @@
#include "parser.h"
string parser::get_token() {
string type = tokens[0].type;
return type;
}
void parser::check_datalog() {
match("SCHEMES");
match("COLON");
if(get_token() == "FACTS") {
error();
}
check_schemelist(get_token());
match("FACTS");
match("COLON");
check_factlist(get_token());
match("RULES");
match("COLON");
check_rulelist(get_token());
match("QUERIES");
match("COLON");
check_querylist(get_token());
out();
}
string parser::out() {
stringstream s;
s << "Success!" << endl;
s << "Schemes(" << schemelist.size() << "):" << endl;
for(unsigned int i = 0; i < schemelist.size(); i++) {
s << " " << schemelist[i].toString();
}
s << "Facts(" << factlist.size() << "):" << endl;
for(unsigned int i = 0; i < factlist.size(); i++) {
s << " " << factlist[i].toString(false);
}
s << "Rules(" << rulelist.size() << "):" << endl;
for(unsigned int i = 0; i < rulelist.size(); i++) {
s << " " << rulelist[i].toString();
}
s << "Queries(" << querylist.size() << "):" << endl;
double a = 0;
for(unsigned int i = 0; i < querylist.size(); i++) {
s << " " << querylist[i].toString(a);
}
s << "Domain(" << domain.size() << "):" << endl;
for (auto it=domain.cbegin(); it != domain.cend(); ++it) {
s << " '" << *it << "'" << endl;
}
return s.str();
}
void parser::check_schemelist(string type) {
if(type == "FACTS") {
return;
}
else {
check_scheme(type);
check_schemelist(get_token());
}
}
void parser::check_scheme(string type) {
schemelist.push_back(check_predicate(type));
}
void parser::check_factlist(string type) {
if(type == "RULES") {
return;
}
else {
check_fact(type);
check_factlist(get_token());
}
}
void parser::check_fact(string type) {
factlist.push_back(check_predicate(type));
match("PERIOD");
}
void parser::check_rulelist(string type) {
if(type == "QUERIES") {
return;
}
else {
check_rule(type);
check_rulelist(get_token());
}
}
void parser::check_rule(string type) {
rule r;
r.head = check_predicate(type);
match("COLON_DASH");
check_predicate_list(get_token(), r);
match("PERIOD");
rulelist.push_back(r);
}
void parser::check_querylist(string type) {
check_query(type);
if(tokens.empty()) {
return;
}
else {
check_querylist(get_token());
}
}
void parser::check_query(string type) {
querylist.push_back(check_predicate(type));
match("Q_MARK");
}
void parser::check_predicate_list(string type, rule& r) {
r.pred_rule.push_back(check_predicate(type));
if(get_token() == "COMMA") {
match("COMMA");
check_predicate_list(get_token(), r);
}
else {
return;
}
}
predicate parser::check_predicate(string type) {
predicate pred;
pred.id = tokens[0].character;
match("ID");
match("LEFT_PAREN");
if(get_token() == "RIGHT_PAREN") {
error();
}
check_parameterlist(get_token(), pred);
match("RIGHT_PAREN");
return pred;
}
void parser::check_parameterlist(string type, predicate& pred) {
if(type == "RIGHT_PAREN") {
return;
}
else {
check_parameter(type, pred);
if(get_token() == "COMMA") {
match("COMMA");
if(get_token() == "RIGHT_PAREN") {
error();
}
check_parameterlist(get_token(), pred);
}
else {
return;
}
}
}
void parser::check_parameter(string type, predicate& pred) {
parameter para;
if(type == "STRING") {
domain.insert(tokens[0].character);
para.param = tokens[0].character;
para.type = tokens[0].type;
pred.pred_list.push_back(para);
match("STRING");
return;
}
else if(type == "ID") {
para.param = tokens[0].character;
para.type = tokens[0].type;
pred.pred_list.push_back(para);
match("ID");
return;
}
else {
error();
}
}
void parser::match(string type) {
if(get_token() == type) {
if(tokens.empty()) {
error();
}
else {
tokens.erase(tokens.begin());
}
}
else {
error();
}
}
void parser::error() {
stringstream oss;
oss << tokens[0] << endl;
throw oss.str();
}
+49
View File
@@ -0,0 +1,49 @@
#ifndef __PARSER_H__
#define __PARSER_H__
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include "../lexer/token.h"
#include "scheme.h"
#include "fact.h"
#include "rule.h"
#include "query.h"
#include "predicate.h"
#include "parameter.h"
using namespace std;
class parser {
public:
parser() {}
vector<token> tokens;
set<string> domain;
vector<predicate> schemelist;
vector<predicate> factlist;
vector<predicate> querylist;
vector<predicate> predlist;
vector<rule> rulelist;
string get_token();
void check_datalog();
void check_schemelist(string);
void check_scheme(string);
void check_factlist(string);
void check_fact(string);
void check_rulelist(string);
void check_rule(string);
void check_querylist(string);
void check_query(string);
void check_predicate_list(string, rule&);
predicate check_predicate(string);
void check_parameterlist(string type, predicate&);
void check_parameter(string, predicate&);
void match(string);
void error();
string out();
};
#endif
+83
View File
@@ -0,0 +1,83 @@
#ifndef __PREDICATE_H__
#define __PREDICATE_H__
#include "parameter.h"
#include <vector>
#include <iostream>
using namespace std;
class predicate {
public:
string id;
vector<parameter> pred_list;
string toString() {
//schemes
stringstream s;
s << id << "(";
for(unsigned int i = 0; i < pred_list.size(); i++) {
s << pred_list[i].param;
if(i < pred_list.size()-1) {
s << ",";
}
}
s << ")\n";
return s.str();
}
string toString(bool a) {
//facts
stringstream s;
s << id << "(";
for(unsigned int i = 0; i < pred_list.size(); i++) {
s << "'" << pred_list[i].param << "'";
if(i < pred_list.size()-1) {
s << ",";
}
}
s << ").\n";
return s.str();
}
string toString(double a) {
//query
stringstream s;
s << id << "(";
for(unsigned int i = 0; i < pred_list.size(); i++) {
if(pred_list[i].type == "STRING") {
s << "'" << pred_list[i].param << "'";
if(i < pred_list.size()-1) {
s << ",";
}
}
if(pred_list[i].type == "ID") {
s << pred_list[i].param;
if(i < pred_list.size()-1) {
s << ",";
}
}
}
s << ")?\n";
return s.str();
}
string toString(int a) {
//rules
stringstream s;
s << id << "(";
for(unsigned int i = 0; i < pred_list.size(); i++) {
if(pred_list[i].type == "STRING") {
s << "'" << pred_list[i].param << "'";
if(i < pred_list.size()-1) {
s << ",";
}
}
if(pred_list[i].type == "ID") {
s << pred_list[i].param;
if(i < pred_list.size()-1) {
s << ",";
}
}
}
s << ")";
return s.str();
}
};
#endif
+14
View File
@@ -0,0 +1,14 @@
#ifndef __QUERY_H__
#define __QUERY_H__
#include "predicate.h"
#include <vector>
#include <iostream>
using namespace std;
class query {
public:
vector<predicate> pred_queries;
};
#endif
+27
View File
@@ -0,0 +1,27 @@
#ifndef __RULE_H__
#define __RULE_H__
#include "predicate.h"
#include <vector>
#include <iostream>
using namespace std;
class rule {
public:
predicate head;
vector<predicate> pred_rule;
string toString() {
stringstream s;
s << head.toString(1) << " :- ";
for(unsigned int i = 0; i < pred_rule.size(); i++) {
s << pred_rule[i].toString(1);
if(i < pred_rule.size()-1) {
s << ",";
}
}
s << ".\n";
return s.str();
}
};
#endif
+14
View File
@@ -0,0 +1,14 @@
#ifndef __SCHEME_H__
#define __SCHEME_H__
#include "predicate.h"
#include <vector>
#include <iostream>
using namespace std;
class scheme {
public:
vector<predicate> pred_schemes;
};
#endif