Browse Source

adding cs236

Derek McQuay 2 years ago
parent
commit
8af25fbb22
56 changed files with 2313 additions and 0 deletions
  1. 63
    0
      cs236/Makefile
  2. 0
    0
      cs236/bin/.hgdir
  3. 19
    0
      cs236/labs/lab01.cpp
  4. 30
    0
      cs236/labs/lab02.cpp
  5. 32
    0
      cs236/labs/lab03.cpp
  6. 361
    0
      cs236/lexer/lexi.cpp
  7. 44
    0
      cs236/lexer/lexi.h
  8. 11
    0
      cs236/lexer/token.cpp
  9. 16
    0
      cs236/lexer/token.h
  10. 56
    0
      cs236/lexer/util.h
  11. 14
    0
      cs236/parser/fact.h
  12. 13
    0
      cs236/parser/parameter.h
  13. 200
    0
      cs236/parser/parser.cpp
  14. 49
    0
      cs236/parser/parser.h
  15. 83
    0
      cs236/parser/predicate.h
  16. 14
    0
      cs236/parser/query.h
  17. 27
    0
      cs236/parser/rule.h
  18. 14
    0
      cs236/parser/scheme.h
  19. 10
    0
      cs236/rdbms/Tuple.h
  20. 45
    0
      cs236/rdbms/db.h
  21. 15
    0
      cs236/rdbms/relation.h
  22. 11
    0
      cs236/rdbms/schema.h
  23. 14
    0
      cs236/submission/lab02/fact.h
  24. 30
    0
      cs236/submission/lab02/lab02.cpp
  25. 361
    0
      cs236/submission/lab02/lexi.cpp
  26. 44
    0
      cs236/submission/lab02/lexi.h
  27. 13
    0
      cs236/submission/lab02/parameter.h
  28. 200
    0
      cs236/submission/lab02/parser.cpp
  29. 49
    0
      cs236/submission/lab02/parser.h
  30. 83
    0
      cs236/submission/lab02/predicate.h
  31. 14
    0
      cs236/submission/lab02/query.h
  32. 27
    0
      cs236/submission/lab02/rule.h
  33. 14
    0
      cs236/submission/lab02/scheme.h
  34. 11
    0
      cs236/submission/lab02/token.cpp
  35. 16
    0
      cs236/submission/lab02/token.h
  36. 56
    0
      cs236/submission/lab02/util.h
  37. 21
    0
      cs236/tests/lab01/input.txt
  38. 21
    0
      cs236/tests/lab01/test1.txt
  39. 17
    0
      cs236/tests/lab02/in/in21.txt
  40. 16
    0
      cs236/tests/lab02/in/in22.txt
  41. 19
    0
      cs236/tests/lab02/in/in23.txt
  42. 19
    0
      cs236/tests/lab02/in/in24.txt
  43. 18
    0
      cs236/tests/lab02/in/in25.txt
  44. 16
    0
      cs236/tests/lab02/in/in26.txt
  45. 14
    0
      cs236/tests/lab02/in/in27.txt
  46. 21
    0
      cs236/tests/lab02/in/in28.txt
  47. 19
    0
      cs236/tests/lab02/out/out21.txt
  48. 3
    0
      cs236/tests/lab02/out/out22.txt
  49. 3
    0
      cs236/tests/lab02/out/out23.txt
  50. 3
    0
      cs236/tests/lab02/out/out24.txt
  51. 3
    0
      cs236/tests/lab02/out/out25.txt
  52. 9
    0
      cs236/tests/lab02/out/out26.txt
  53. 3
    0
      cs236/tests/lab02/out/out27.txt
  54. 3
    0
      cs236/tests/lab02/out/out28.txt
  55. 15
    0
      cs236/tests/lab03/in/in40.txt
  56. 11
    0
      cs236/tests/lab03/out/out40.txt

+ 63
- 0
cs236/Makefile View File

@@ -0,0 +1,63 @@
1
+CXXFLAGS= -Wall -g -std=c++0x -I .
2
+
3
+lexor_objs=labs/lab01.o \
4
+		   lexer/lexi.o \
5
+		   lexer/token.o
6
+
7
+parser_objs=labs/lab02.o \
8
+		    lexer/lexi.o \
9
+		    lexer/token.o \
10
+			parser/parser.o \
11
+
12
+rdbms_objs=labs/lab03.o \
13
+		   lexer/lexi.o \
14
+		   lexer/token.o \
15
+		   parser/parser.o \
16
+
17
+lab01=bin/lab01
18
+lab02=bin/lab02
19
+lab03=bin/lab03
20
+
21
+all: $(lab01) $(lab02) $(lab03)
22
+
23
+$(lab01): $(lexor_objs)
24
+	$(CXX) $(CXXFLAGS) $(lexor_objs) -o $@
25
+
26
+$(lab02): $(parser_objs)
27
+	$(CXX) $(CXXFLAGS) $^ -o $@
28
+
29
+$(lab03): $(rdbms_objs)
30
+	$(CXX) $(CXXFLAGS) $^ -o $@
31
+
32
+labs/lab01.o: labs/lab01.cpp lexer/util.h lexer/lexi.h lexer/token.h
33
+lexer/lexi.o: lexer/lexi.cpp lexer/lexi.h
34
+lexer/token.o: lexer/token.h lexer/token.cpp
35
+
36
+labs/lab02.o: labs/lab02.cpp lexer/util.h lexer/lexi.h lexer/token.h \
37
+           	  parser/scheme.h parser/fact.h parser/rule.h parser/query.h\
38
+			  parser/predicate.h parser/parameter.h parser/parser.o
39
+
40
+labs/lab03.o: labs/lab02.cpp lexer/util.h lexer/lexi.h lexer/token.h \
41
+           	  parser/scheme.h parser/fact.h parser/rule.h parser/query.h\
42
+			  parser/predicate.h parser/parameter.h parser/parser.o rdbms/db.h \
43
+			  rdbms/relation.h rdbms/schema.h rdbms/Tuple.h
44
+parser/parser.o: parser/parser.h parser/parser.cpp
45
+
46
+clean:
47
+	@rm -vf **/*.o
48
+	@rm -vf $(EXE)
49
+	@rm -vf **/*.1
50
+	@rm -vf **/*.0
51
+	@rm -vf test
52
+	@rm -rvf **/*.dSYM
53
+	@rm -vf output.txt
54
+	@rm -vf bin/*
55
+
56
+drun: main
57
+	gdb ./main
58
+
59
+valgrind: $(EXE)
60
+	valgrind --tool=memcheck --leak-check=yes ./$(EXE) input.txt output.txt
61
+
62
+pmc: **/*.h **/*.cpp
63
+	pmccabe **/*.h **/*.cpp

+ 0
- 0
cs236/bin/.hgdir View File


+ 19
- 0
cs236/labs/lab01.cpp View File

@@ -0,0 +1,19 @@
1
+#include <vector>
2
+#include "lexer/lexi.h"  
3
+#include "lexer/util.h"
4
+#include "lexer/token.h"
5
+
6
+const string usage = "usage: app <input> <output>";
7
+                       
8
+int main(int argc, char* argv[]) {
9
+    if(argc != 3) {
10
+        cerr << usage << endl;
11
+        return 1;
12
+    }
13
+    get_file_name(argv[1]);
14
+    vector<string> data = open_file(argv[1]);
15
+    lexi l;
16
+    string temp = argv[2];
17
+    l.lexical_analyzer(data, temp);
18
+    cout << "getting called here in lab 1" << endl;
19
+}

+ 30
- 0
cs236/labs/lab02.cpp View File

@@ -0,0 +1,30 @@
1
+#include <vector>
2
+#include "lexer/lexi.h"  
3
+#include "lexer/util.h"
4
+#include "lexer/token.h"
5
+#include "parser/parser.h"
6
+
7
+const string usage = "usage: app <input> <output>";
8
+                       
9
+int main(int argc, char* argv[]) {
10
+    if(argc != 3) {
11
+        cerr << usage << endl;
12
+        return 1;
13
+    }
14
+    get_file_name(argv[1]);
15
+    vector<string> data = open_file(argv[1]);
16
+    lexi l;
17
+    string temp = argv[2];
18
+    vector<token> s = l.lexical_analyzer(data, temp);
19
+    parser p;
20
+    p.tokens = s;
21
+    try {
22
+        p.check_datalog();
23
+        string out = p.out();
24
+        write_file(out, argv[2]);
25
+    } catch(string str) {
26
+        stringstream s;
27
+        s << "Failure!\n  " << str;
28
+        write_file(s.str(), argv[2]);
29
+    }
30
+}

+ 32
- 0
cs236/labs/lab03.cpp View File

@@ -0,0 +1,32 @@
1
+#include <vector>
2
+#include "lexer/lexi.h"  
3
+#include "lexer/util.h"
4
+#include "lexer/token.h"
5
+#include "parser/parser.h"
6
+#include "rdbms/db.h"
7
+
8
+const string usage = "usage: app <input> <output>";
9
+                       
10
+int main(int argc, char* argv[]) {
11
+    if(argc != 3) {
12
+        cerr << usage << endl;
13
+        return 1;
14
+    }
15
+    get_file_name(argv[1]);
16
+    vector<string> data = open_file(argv[1]);
17
+    lexi l;
18
+    string temp = argv[2];
19
+    vector<token> s = l.lexical_analyzer(data, temp);
20
+    parser p;
21
+    p.tokens = s;
22
+    try {
23
+        p.check_datalog();
24
+        string out = p.out();
25
+        write_file(out, argv[2]);
26
+    } catch(string str) {
27
+        stringstream s;
28
+        s << "Failure!\n  " << str;
29
+        write_file(s.str(), argv[2]);
30
+    }
31
+    db database(p);
32
+}

+ 361
- 0
cs236/lexer/lexi.cpp View File

@@ -0,0 +1,361 @@
1
+#include "lexi.h"
2
+
3
+vector<token> lexi::lexical_analyzer(vector<string> data, string file_name) {
4
+    string cur_string;
5
+    string next_character;
6
+    for(unsigned int i = 0; i < data.size(); i++) {
7
+        for(unsigned int j = 0; j < data[i].size(); j ++) {
8
+            cur_string = data[i].at(j);
9
+            if(j < data[i].size() - 1) {
10
+                next_character = data[i].at(j + 1);
11
+            }
12
+            else {
13
+                next_character = "";
14
+            }
15
+            string state = determiner(cur_string, next_character);
16
+            simple_state(data, state, cur_string, next_character, i, j);
17
+            simple_state_string(data, state, cur_string, next_character, i, j);
18
+            if(state == "start of string") {
19
+                string token_symbol = string_finder(data, i, j);
20
+                if(token_symbol != "error") {
21
+                    token_symbol.erase(0,1);
22
+                    token t("STRING", token_symbol, i + 1);
23
+                    tokens.push_back(t);
24
+                }
25
+                else {
26
+                    write_to_file(file_name, i + 1);
27
+                    return tokens;
28
+                }
29
+            }
30
+            simple_comment(data, state, cur_string, next_character, i, j);
31
+            simple_id(data, state, cur_string, next_character, i, j);
32
+            if(state == "error") {
33
+                write_to_file(file_name, i + 1);
34
+                return tokens;
35
+            }
36
+        }
37
+    }
38
+    write_to_file(file_name);
39
+    return tokens;
40
+}
41
+
42
+void lexi::write_to_file(string file_name, int line) {
43
+    ofstream myfile;
44
+    myfile.open(file_name.c_str());
45
+    myfile << "Error on line " << line << endl;
46
+    myfile.close();
47
+}
48
+
49
+void lexi::write_to_file(string file_name) {
50
+    ofstream myfile;
51
+    myfile.open(file_name.c_str());
52
+    for(unsigned int i = 0; i < tokens.size(); i++) {
53
+        if(i < tokens.size()) {
54
+            myfile << tokens[i] << endl;
55
+        }
56
+        else {
57
+            myfile << tokens[i];
58
+        }
59
+    }
60
+    myfile << "Total Tokens = " << tokens.size();
61
+    myfile << endl;
62
+    myfile.close();
63
+}
64
+
65
+
66
+bool lexi::simple_comment(vector<string> & data, string state, string cur_string, string next_character, int i, int j) {
67
+    if(state == "comment") {
68
+        string token_symbol = comment_finder(data, i, j);
69
+    }
70
+    return true;
71
+}
72
+
73
+bool lexi::simple_id(vector<string> & data, string state, string cur_string, string next_character, int i, int j) {
74
+    if(state == "id") {
75
+        string token_symbol = id_finder(data, i, j);
76
+        if(token_symbol != "error") {
77
+            token t("ID", token_symbol, i + 1);
78
+            tokens.push_back(t);
79
+        }
80
+    }
81
+    return true;
82
+}
83
+
84
+bool lexi::simple_state_string(vector<string> & data, string state, string cur_string, string next_character, int i, int j) {
85
+    if(state == "simple_string") {
86
+        string token_symbol = det_type_simple_string(data, i, j);
87
+        if(token_symbol != "wrong") {
88
+            string token_type = type_simple_string(token_symbol);
89
+            token t(token_type, token_symbol, i + 1);
90
+            tokens.push_back(t);
91
+        }
92
+        else {
93
+            string token_symbol = id_finder(data, i, j);
94
+            if(token_symbol != "error") {
95
+                token t("ID", token_symbol, i + 1);
96
+                tokens.push_back(t);
97
+            }
98
+        }
99
+    }
100
+    return true;
101
+}
102
+
103
+bool lexi::simple_state(vector<string> & data, string state, string cur_string, string next_character, int i, int j) {
104
+    if(state == "simple") {
105
+        string token_symbol = type_simple(cur_string, next_character);
106
+        if(next_character == "-") {
107
+            data[i].replace(j,2, " ");
108
+        }
109
+        else {
110
+            data[i].replace(j,1, " ");
111
+        }
112
+        string token_id = type_simple_caps(cur_string, next_character);
113
+        token t(token_id, token_symbol, i + 1);
114
+        tokens.push_back(t);
115
+    }
116
+    return true;
117
+}
118
+
119
+string lexi::determiner(string cur_string, string next_character) {
120
+    if(det_help_simple(cur_string, next_character)) {
121
+        return "simple";
122
+    }
123
+    else if(det_help_simple_string(cur_string, next_character)) {
124
+        return "simple_string";
125
+    }
126
+    else if(det_help_id(cur_string)) {
127
+        return "id";
128
+    }
129
+    else if(cur_string == "'") {
130
+        return "start of string";
131
+    }
132
+    else if(cur_string == "#") {
133
+        return "comment";
134
+    }
135
+    else {
136
+        string temp = incorrect(cur_string);
137
+        return temp;
138
+    }
139
+    return "";
140
+}
141
+
142
+bool lexi::det_help_id(string cur_string) {
143
+    if(('A' <= cur_string[0] && cur_string[0] <= 'Z') ||
144
+     ('a' <= cur_string[0] && cur_string[0] <= 'z')) {
145
+        return true;
146
+    }
147
+    return false;
148
+}
149
+
150
+bool lexi::quick_help(string a, string b) {
151
+    if(a == "S" && b == "c") {
152
+        return true;
153
+    }
154
+    return false;
155
+}
156
+
157
+bool lexi::det_help_simple_string(string cur_string, string next_character) {
158
+    if(quick_help(cur_string, next_character)) {
159
+        return true;
160
+    }
161
+    else if((cur_string == "Q" && next_character == "u")) {
162
+        return true;
163
+    }
164
+    else if((cur_string == "R" && next_character == "u")) { 
165
+        return true;
166
+    }
167
+    else if((cur_string == "F" && next_character == "a")) {
168
+        return true;
169
+    }
170
+    return false;
171
+}
172
+
173
+bool lexi::det_help_simple(string cur_string, string next_character) {
174
+    if(cur_string == "," || cur_string == "." || cur_string == "?" ||
175
+            cur_string == "(" || cur_string == ")" || cur_string == ":") {
176
+        type_simple(cur_string, next_character);
177
+        return true;
178
+    }
179
+    return false;
180
+}
181
+
182
+string lexi::incorrect(string cur_string) {
183
+    if(cur_string == " " || cur_string == "\t") {
184
+        return "fine";
185
+    }
186
+    else if(!(('A' <= cur_string[0] && cur_string[0] <= 'Z') ||
187
+     ('a' <= cur_string[0] && cur_string[0] <= 'z'))) {
188
+        return "error";
189
+    }
190
+    return " ";
191
+}
192
+
193
+string lexi::id_finder(vector<string> & data, int a, int b) {
194
+    string cur_string;
195
+    string next_character;
196
+    for(unsigned int j = b; j < data[a].size(); j++) {
197
+        cur_string += data[a].at(j);
198
+        if(j < data[a].size() - 1) {
199
+            next_character = data[a].at(j + 1);
200
+        }
201
+        else {
202
+            next_character = "!";
203
+        }
204
+        if(is_char_valid(next_character[0]) || next_character == "!") {
205
+            data[a].replace(data[a].begin() + b, data[a].begin() + j + 1, " ");
206
+            return cur_string;
207
+        }
208
+    }
209
+    return " ";
210
+}
211
+
212
+string lexi::comment_finder(vector<string> & data, int i, int b) {
213
+    string cur_string;
214
+    string next_character;
215
+    for(unsigned int j = b; j < data[i].size(); j++) {
216
+        cur_string += data[i].at(j);
217
+        if(j < data[i].size() - 1) {
218
+            next_character = data[i].at(j + 1);
219
+        }
220
+        else {
221
+            next_character = "!";
222
+        }
223
+        if((j > data[i].size()) - 1 && next_character != "!") {
224
+            data[i].replace(data[i].begin() + b, data[i].end(), " ");
225
+            return cur_string;
226
+        }
227
+    }
228
+    return "error";
229
+}
230
+
231
+string lexi::string_finder(vector<string> & data, int a, int b) {
232
+    string cur_string;
233
+    string next_character;
234
+    b = data[a].find('\'');
235
+    for(unsigned int j = b; j < data[a].size(); j++) {
236
+        cur_string += data[a].at(j);
237
+        if(j < data[a].size() - 1) {
238
+            next_character = data[a].at(j + 1);
239
+        }
240
+        if(next_character == "'") {
241
+            data[a].replace(data[a].begin() + b, data[a].begin() + j + 2, " ");
242
+            data[a].insert(data[a].begin() + b, ' ');
243
+            return cur_string;
244
+        }
245
+    }
246
+    return "error";
247
+}
248
+
249
+string lexi::type_simple_caps(string symbol, string next_symbol) {
250
+    if(symbol == ",") {
251
+        return "COMMA";
252
+    }
253
+    else if(symbol == ".") {
254
+        return "PERIOD";
255
+    }
256
+    else if(symbol == "?") {
257
+        return "Q_MARK";
258
+    }
259
+    else if(symbol == "(") {
260
+        return "LEFT_PAREN";
261
+    }
262
+    else if(symbol == ")") {
263
+        return "RIGHT_PAREN";
264
+    }
265
+    else if(symbol == ":") {
266
+        if(next_symbol == "-") {
267
+            return "COLON_DASH";
268
+        }
269
+        return "COLON";
270
+    }
271
+    return "";
272
+}
273
+
274
+string lexi::type_simple(string symbol, string next_symbol) {
275
+    if(symbol == ",") {
276
+        return ",";
277
+    }
278
+    else if(symbol == ".") {
279
+        return ".";
280
+    }
281
+    else if(symbol == "?") {
282
+        return "?";
283
+    }
284
+    else if(symbol == "(") {
285
+        return "(";
286
+    }
287
+    else if(symbol == ")") {
288
+        return ")";
289
+    }
290
+    else if(symbol == ":") {
291
+        if(next_symbol == "-") {
292
+            return ":-";
293
+        }
294
+        return ":";
295
+    }
296
+    return "";
297
+}
298
+
299
+string lexi::det_type_simple_string(vector<string> & data, int i, int b) {
300
+    string cur_string;
301
+    string next_character;
302
+    string special_case;
303
+    if(b > 0) {
304
+        special_case = data[i].at(b -1);
305
+    }
306
+    for(unsigned int j = b; j < data[i].size(); j++) {
307
+        cur_string += data[i].at(j);
308
+        if(j < data[i].size() - 1) {
309
+            next_character = data[i].at(j + 1);
310
+        }
311
+        else {
312
+            next_character = "!";
313
+        }
314
+        if((is_simple_string(cur_string)) && (is_char_valid(next_character.at(0))) && (is_char_valid(special_case[0]))) {
315
+            data[i].replace(data[i].begin() + b, data[i].begin() + j + 1, " ");
316
+            return cur_string;
317
+        }
318
+    }
319
+    return "wrong";
320
+}
321
+
322
+bool lexi::is_char_valid(char next_character) {
323
+    if(!(('A' <= next_character && next_character <= 'Z') ||
324
+     ('a' <= next_character && next_character <= 'z') || 
325
+     ('0' <= next_character && next_character <= '9')) || (next_character == '\'')) {
326
+        return true;
327
+    }
328
+    return false;
329
+}
330
+
331
+bool lexi::is_simple_string(string simple_com) {
332
+    if(simple_com == "Schemes") {
333
+        return true;
334
+    }
335
+    else if(simple_com == "Facts") {
336
+        return true;
337
+    }
338
+    else if(simple_com == "Rules") {
339
+        return true;
340
+    }
341
+    else if(simple_com == "Queries") {
342
+        return true;
343
+    }
344
+    return false;
345
+}
346
+
347
+string lexi::type_simple_string(string simple_com) {
348
+    if(simple_com == "Schemes") {
349
+        return "SCHEMES";
350
+    }
351
+    else if(simple_com == "Facts") {
352
+        return "FACTS";
353
+    }
354
+    else if(simple_com == "Rules") {
355
+        return "RULES";
356
+    }
357
+    else if(simple_com == "Queries") {
358
+        return "QUERIES";
359
+    }
360
+    return "";
361
+}

+ 44
- 0
cs236/lexer/lexi.h View File

@@ -0,0 +1,44 @@
1
+#ifndef __LEXI_H__                          
2
+#define __LEXI_H__                          
3
+                                              
4
+#include <iostream>
5
+#include <fstream>
6
+#include <vector>
7
+#include "token.h"
8
+#include <cctype>  
9
+#include <string>  
10
+#include <sstream> 
11
+
12
+                                              
13
+using namespace std;                          
14
+                                              
15
+class lexi {                                             
16
+    public:
17
+        lexi(){}
18
+        vector<token> tokens;
19
+        string determiner(string, string);
20
+        vector<token> lexical_analyzer(vector<string>, string);
21
+        string type_simple(string, string);
22
+        string type_simple_string(string);
23
+        bool is_simple_string(string);
24
+        string det_type_simple_string(vector<string>&, int, int);
25
+        bool is_char_valid(char);
26
+        string string_finder(vector<string>&, int, int);
27
+        string comment_finder(vector<string>&, int, int);
28
+        string id_finder(vector<string>&, int, int);
29
+        string incorrect(string);
30
+        bool det_help_simple(string, string);
31
+        bool det_help_simple_string(string, string);
32
+        bool quick_help(string, string);
33
+        bool det_help_id(string);
34
+        string type_simple_caps(string, string);
35
+        bool simple_state(vector<string>&, string, string, string, int, int);
36
+        bool simple_state_string(vector<string>&, string, string, string, int, int);
37
+        bool simple_id(vector<string>&, string, string, string, int, int);
38
+        bool simple_comment(vector<string>&, string, string, string, int, int);
39
+        void write_to_file(string);
40
+        void write_to_file(string, int);
41
+};                                            
42
+                                              
43
+#endif                                        
44
+

+ 11
- 0
cs236/lexer/token.cpp View File

@@ -0,0 +1,11 @@
1
+#include "token.h"
2
+
3
+token::token(string type, string character, int line_num) :           
4
+                        type(type), character(character), line_num(line_num) {}
5
+
6
+ostream & operator<<(ostream & os, token tk) {
7
+    os << "(" << tk.type                          
8
+    << ",\"" << tk.character                      
9
+    << "\"," << tk.line_num << ")";                                            
10
+    return os;                                             
11
+}                                                          

+ 16
- 0
cs236/lexer/token.h View File

@@ -0,0 +1,16 @@
1
+#ifndef __TOKEN_H__                          
2
+#define __TOKEN_H__                          
3
+
4
+#include <iostream>
5
+
6
+using namespace std;
7
+                                              
8
+class token {
9
+    public:
10
+        token(string, string, int);
11
+        string type;
12
+        string character;
13
+        int line_num;
14
+        friend ostream & operator<<(ostream & os, token tk);
15
+};
16
+#endif                                        

+ 56
- 0
cs236/lexer/util.h View File

@@ -0,0 +1,56 @@
1
+#ifndef __UTIL_H__                          
2
+#define __UTIL_H__                          
3
+#include <vector>
4
+#include <iostream>
5
+#include <fstream>
6
+
7
+vector<string> open_file(string file_name) {
8
+    ifstream myfile;
9
+    vector<string> data;
10
+    myfile.open(file_name.c_str());
11
+    string temp;
12
+    while(!myfile.eof()) {
13
+        getline(myfile, temp);
14
+        data.push_back(temp);
15
+    }
16
+    myfile.close();
17
+    return data;
18
+}
19
+
20
+bool get_file_name(string input) {
21
+    bool file_correct = false;    
22
+    string input_file_name;
23
+    while(!file_correct) {                        
24
+        ifstream inputs(input.c_str());           
25
+        if(inputs.good()) {                       
26
+            input_file_name = input;              
27
+            file_correct = true;                  
28
+            open_file(input_file_name);
29
+            return true;
30
+        }                                         
31
+        else {                                    
32
+            cerr << "incorrect file name" << endl;
33
+            return false;
34
+        }                                         
35
+    }                                             
36
+    return false;
37
+}
38
+
39
+void write_file(string output, string file_name) {
40
+    ofstream myfile;
41
+    myfile.open(file_name.c_str());
42
+    myfile << output << "\n";
43
+}
44
+
45
+void write_file(vector<string> output, string file_name) {
46
+    ofstream myfile;
47
+    myfile.open(file_name.c_str());
48
+    for(unsigned int i = 0; i < output.size(); i++) {
49
+        if(i != output.size() -1) {
50
+            myfile << output[i] << "\n";
51
+        }
52
+        myfile << output[i];
53
+    }
54
+}
55
+
56
+#endif                                        

+ 14
- 0
cs236/parser/fact.h View File

@@ -0,0 +1,14 @@
1
+#ifndef __FACT_H__
2
+#define __FACT_H__
3
+
4
+#include "predicate.h"
5
+#include <vector>
6
+#include <sstream>
7
+
8
+using namespace std;
9
+
10
+class fact {
11
+    public:
12
+        vector<predicate> pred_facts;
13
+};
14
+#endif

+ 13
- 0
cs236/parser/parameter.h View File

@@ -0,0 +1,13 @@
1
+#ifndef __PARAMETER_H__
2
+#define __PARAMETER_H__
3
+
4
+#include <iostream>
5
+
6
+using namespace std;
7
+
8
+class parameter {
9
+    public:
10
+        string param;
11
+        string type;
12
+};
13
+#endif

+ 200
- 0
cs236/parser/parser.cpp View File

@@ -0,0 +1,200 @@
1
+#include "parser.h"
2
+
3
+string parser::get_token() {
4
+    string type = tokens[0].type;
5
+    return type;
6
+}
7
+
8
+void parser::check_datalog() {
9
+    match("SCHEMES");
10
+    match("COLON");
11
+    if(get_token() == "FACTS") {
12
+        error();
13
+    }
14
+    check_schemelist(get_token());
15
+    match("FACTS");
16
+    match("COLON");
17
+    check_factlist(get_token());
18
+    match("RULES");
19
+    match("COLON");
20
+    check_rulelist(get_token());
21
+    match("QUERIES");
22
+    match("COLON");
23
+    check_querylist(get_token());
24
+    out();
25
+}
26
+
27
+string parser::out() {
28
+    stringstream s;
29
+    s << "Success!" << endl;
30
+    s << "Schemes(" << schemelist.size() << "):" << endl;
31
+    for(unsigned int i = 0; i < schemelist.size(); i++) {
32
+        s << "  " << schemelist[i].toString();
33
+    }
34
+    s << "Facts(" << factlist.size() << "):" << endl;
35
+    for(unsigned int i = 0; i < factlist.size(); i++) {
36
+        s << "  " << factlist[i].toString(false);
37
+    }
38
+    s << "Rules(" << rulelist.size() << "):" << endl;
39
+    for(unsigned int i = 0; i < rulelist.size(); i++) {
40
+        s << "  " << rulelist[i].toString();
41
+    }
42
+    s << "Queries(" << querylist.size() << "):" << endl;
43
+    double a = 0;
44
+    for(unsigned int i = 0; i < querylist.size(); i++) {
45
+        s << "  " << querylist[i].toString(a);
46
+    }
47
+    s << "Domain(" << domain.size() << "):" << endl;
48
+    for (auto it=domain.cbegin(); it != domain.cend(); ++it) {
49
+        s << "  '" << *it << "'" << endl;
50
+    }
51
+    return s.str();
52
+}
53
+
54
+void parser::check_schemelist(string type) {
55
+    if(type == "FACTS") {
56
+        return;
57
+    }
58
+    else {
59
+        check_scheme(type);
60
+        check_schemelist(get_token());
61
+    }
62
+}
63
+
64
+void parser::check_scheme(string type) {
65
+    schemelist.push_back(check_predicate(type));
66
+}
67
+
68
+void parser::check_factlist(string type) {
69
+    if(type == "RULES") {
70
+        return;
71
+    }
72
+    else {
73
+        check_fact(type);
74
+        check_factlist(get_token());
75
+    }
76
+}
77
+
78
+void parser::check_fact(string type) {
79
+    factlist.push_back(check_predicate(type));
80
+    match("PERIOD");
81
+}
82
+
83
+void parser::check_rulelist(string type) {
84
+    if(type == "QUERIES") {
85
+        return;
86
+    }
87
+    else {
88
+        check_rule(type);
89
+        check_rulelist(get_token());
90
+    }
91
+}
92
+
93
+void parser::check_rule(string type) {
94
+    rule r;
95
+    r.head = check_predicate(type);
96
+    match("COLON_DASH");
97
+    check_predicate_list(get_token(), r);
98
+    match("PERIOD");
99
+    rulelist.push_back(r);
100
+}
101
+
102
+void parser::check_querylist(string type) {
103
+    check_query(type);
104
+    if(tokens.empty()) {
105
+        return;
106
+    }
107
+    else {
108
+        check_querylist(get_token());
109
+    }
110
+}
111
+
112
+void parser::check_query(string type) {
113
+    querylist.push_back(check_predicate(type));
114
+    match("Q_MARK");
115
+}
116
+
117
+void parser::check_predicate_list(string type, rule& r) {
118
+    r.pred_rule.push_back(check_predicate(type));
119
+    if(get_token() == "COMMA") {
120
+        match("COMMA");
121
+        check_predicate_list(get_token(), r);
122
+    }
123
+    else {
124
+        return;
125
+    }
126
+}
127
+
128
+predicate parser::check_predicate(string type) {
129
+    predicate pred;
130
+    pred.id  = tokens[0].character;
131
+    match("ID");
132
+    match("LEFT_PAREN");
133
+    if(get_token() == "RIGHT_PAREN") {
134
+        error();
135
+    }
136
+    check_parameterlist(get_token(), pred);
137
+    match("RIGHT_PAREN");
138
+    return pred;
139
+}
140
+
141
+void parser::check_parameterlist(string type, predicate& pred) {
142
+    if(type == "RIGHT_PAREN") {
143
+        return;
144
+    }
145
+    else {
146
+        check_parameter(type, pred);
147
+        if(get_token() == "COMMA") {
148
+            match("COMMA");
149
+            if(get_token() == "RIGHT_PAREN") {
150
+                error();
151
+            }
152
+            check_parameterlist(get_token(), pred);
153
+        }
154
+        else {
155
+            return;
156
+        }
157
+    }
158
+}
159
+
160
+void parser::check_parameter(string type, predicate& pred) {
161
+    parameter para;
162
+    if(type == "STRING") {
163
+        domain.insert(tokens[0].character);
164
+        para.param = tokens[0].character;
165
+        para.type = tokens[0].type;
166
+        pred.pred_list.push_back(para);
167
+        match("STRING");
168
+        return;
169
+    }
170
+    else if(type == "ID") {
171
+        para.param = tokens[0].character;
172
+        para.type = tokens[0].type;
173
+        pred.pred_list.push_back(para);
174
+        match("ID");
175
+        return;
176
+    }
177
+    else {
178
+        error();
179
+    }
180
+}
181
+
182
+void parser::match(string type) {
183
+    if(get_token() == type) {
184
+        if(tokens.empty()) {
185
+            error();
186
+        }
187
+        else {
188
+            tokens.erase(tokens.begin());
189
+        }
190
+    }
191
+    else {
192
+        error();
193
+    }
194
+}
195
+
196
+void parser::error() {
197
+    stringstream oss;
198
+    oss << tokens[0] << endl;
199
+    throw oss.str();
200
+}

+ 49
- 0
cs236/parser/parser.h View File

@@ -0,0 +1,49 @@
1
+#ifndef __PARSER_H__
2
+#define __PARSER_H__
3
+
4
+#include <iostream>
5
+#include <sstream>
6
+#include <vector>
7
+#include <set>
8
+
9
+#include "../lexer/token.h"
10
+#include "scheme.h"
11
+#include "fact.h"
12
+#include "rule.h"
13
+#include "query.h"
14
+#include "predicate.h"
15
+#include "parameter.h"
16
+
17
+using namespace std;
18
+
19
+class parser {
20
+    public:
21
+        parser() {}
22
+        vector<token> tokens;
23
+        set<string> domain;
24
+        vector<predicate> schemelist;
25
+        vector<predicate> factlist;
26
+        vector<predicate> querylist;
27
+        vector<predicate> predlist;
28
+        vector<rule> rulelist;
29
+
30
+        string get_token();
31
+        void check_datalog();
32
+        void check_schemelist(string);
33
+        void check_scheme(string);
34
+        void check_factlist(string);
35
+        void check_fact(string);
36
+        void check_rulelist(string);
37
+        void check_rule(string);
38
+        void check_querylist(string);
39
+        void check_query(string);
40
+        void check_predicate_list(string, rule&);
41
+        predicate check_predicate(string);
42
+        void check_parameterlist(string type, predicate&);
43
+        void check_parameter(string, predicate&);
44
+        void match(string);
45
+        void error();
46
+        string out();
47
+
48
+};
49
+#endif

+ 83
- 0
cs236/parser/predicate.h View File

@@ -0,0 +1,83 @@
1
+#ifndef __PREDICATE_H__
2
+#define __PREDICATE_H__
3
+
4
+#include "parameter.h"
5
+#include <vector>
6
+#include <iostream>
7
+
8
+using namespace std;
9
+
10
+class predicate {
11
+    public:
12
+        string id;
13
+        vector<parameter> pred_list;
14
+        string toString() {
15
+            //schemes
16
+            stringstream s;
17
+            s << id << "(";
18
+            for(unsigned int i = 0; i < pred_list.size(); i++) {
19
+                s << pred_list[i].param;
20
+                if(i < pred_list.size()-1) {
21
+                    s << ",";
22
+                }
23
+            }
24
+            s << ")\n";
25
+            return s.str();
26
+        }
27
+        string toString(bool a) {
28
+            //facts
29
+            stringstream s;
30
+            s << id << "(";
31
+            for(unsigned int i = 0; i < pred_list.size(); i++) {
32
+                s << "'" << pred_list[i].param << "'";
33
+                if(i < pred_list.size()-1) {
34
+                    s << ",";
35
+                }
36
+            }
37
+            s << ").\n";
38
+            return s.str();
39
+        }
40
+        string toString(double a) {
41
+            //query
42
+            stringstream s;
43
+            s << id << "(";
44
+            for(unsigned int i = 0; i < pred_list.size(); i++) {
45
+                if(pred_list[i].type == "STRING") {
46
+                    s << "'" << pred_list[i].param << "'";
47
+                    if(i < pred_list.size()-1) {
48
+                        s << ",";
49
+                    }
50
+                }
51
+                if(pred_list[i].type == "ID") {
52
+                    s << pred_list[i].param;
53
+                    if(i < pred_list.size()-1) {
54
+                        s << ",";
55
+                    }
56
+                }
57
+            }
58
+            s << ")?\n";
59
+            return s.str();
60
+        }
61
+        string toString(int a) {
62
+            //rules
63
+            stringstream s;
64
+            s << id << "(";
65
+            for(unsigned int i = 0; i < pred_list.size(); i++) {
66
+                if(pred_list[i].type == "STRING") {
67
+                    s << "'" << pred_list[i].param << "'";
68
+                    if(i < pred_list.size()-1) {
69
+                        s << ",";
70
+                    }
71
+                }
72
+                if(pred_list[i].type == "ID") {
73
+                    s << pred_list[i].param;
74
+                    if(i < pred_list.size()-1) {
75
+                        s << ",";
76
+                    }
77
+                }
78
+            }
79
+            s << ")";
80
+            return s.str();
81
+        }
82
+};
83
+#endif

+ 14
- 0
cs236/parser/query.h View File

@@ -0,0 +1,14 @@
1
+#ifndef __QUERY_H__
2
+#define __QUERY_H__
3
+
4
+#include "predicate.h"
5
+#include <vector>
6
+#include <iostream>
7
+
8
+using namespace std;
9
+
10
+class query {
11
+    public:
12
+        vector<predicate> pred_queries;
13
+};
14
+#endif

+ 27
- 0
cs236/parser/rule.h View File

@@ -0,0 +1,27 @@
1
+#ifndef __RULE_H__
2
+#define __RULE_H__
3
+
4
+#include "predicate.h"
5
+#include <vector>
6
+#include <iostream>
7
+
8
+using namespace std;
9
+
10
+class rule {
11
+    public:
12
+        predicate head;
13
+        vector<predicate> pred_rule;
14
+        string toString() {
15
+            stringstream s;
16
+            s << head.toString(1) << " :- ";
17
+            for(unsigned int i = 0; i < pred_rule.size(); i++) {
18
+                s << pred_rule[i].toString(1);
19
+                if(i < pred_rule.size()-1) {
20
+                    s << ",";
21
+                }
22
+            }
23
+            s << ".\n";
24
+            return s.str();
25
+        }
26
+};
27
+#endif

+ 14
- 0
cs236/parser/scheme.h View File

@@ -0,0 +1,14 @@
1
+#ifndef __SCHEME_H__
2
+#define __SCHEME_H__
3
+
4
+#include "predicate.h"
5
+#include <vector>
6
+#include <iostream>
7
+
8
+using namespace std;
9
+
10
+class scheme {
11
+    public:
12
+        vector<predicate> pred_schemes;
13
+};
14
+#endif

+ 10
- 0
cs236/rdbms/Tuple.h View File

@@ -0,0 +1,10 @@
1
+#ifndef __TUPLE_H__
2
+#define __TUPLE_H__
3
+
4
+#include <vector>
5
+#include <string>
6
+
7
+class Tuple: public vector<string> {
8
+};
9
+
10
+#endif

+ 45
- 0
cs236/rdbms/db.h View File

@@ -0,0 +1,45 @@
1
+#ifndef __DB_H__
2
+#define __DB_H__
3
+
4
+#include "parser/parser.h"
5
+#include "relation.h"
6
+
7
+class db {
8
+    public:
9
+        db(parser incoming) {
10
+            p = incoming;
11
+            setup();
12
+        }
13
+        parser p;
14
+        vector<relation> relations;
15
+
16
+
17
+        void setup() {
18
+            for(unsigned int i = 0; i < p.schemelist.size(); i++) {
19
+                relation r;
20
+                r.name = p.schemelist[i].id;
21
+                for(unsigned int j = 0; j < p.schemelist[i].pred_list.size(); j++) {
22
+                    r.schemas.s.push_back(p.schemelist[i].pred_list[j].param);
23
+                }
24
+                for(unsigned int k = 0; k < p.factlist.size(); k++) {
25
+                    if(r.name == p.factlist[k].id) {
26
+                        Tuple t;
27
+                        for(unsigned int l = 0; l < p.factlist[k].pred_list.size(); k++) {
28
+                            t.push_back(p.factlist[k].pred_list[l].param);
29
+                        }
30
+                        r.tuples.insert(t);
31
+                    }
32
+                }
33
+            }
34
+        }
35
+
36
+        //set<string> domain;
37
+        //vector<predicate> schemelist;
38
+        //vector<predicate> factlist;
39
+        //vector<predicate> querylist;
40
+        //vector<predicate> predlist;
41
+        //vector<rule> rulelist;
42
+
43
+};
44
+
45
+#endif

+ 15
- 0
cs236/rdbms/relation.h View File

@@ -0,0 +1,15 @@
1
+#ifndef __RELATION_H__
2
+#define __RELATION_H__
3
+
4
+#include "Tuple.h"
5
+#include "schema.h"
6
+#include <set>
7
+
8
+class relation {
9
+    public:
10
+        string name;
11
+        schema schemas;
12
+        set<Tuple> tuples;
13
+};
14
+
15
+#endif

+ 11
- 0
cs236/rdbms/schema.h View File

@@ -0,0 +1,11 @@
1
+#ifndef __SCHEMA_H__
2
+#define __SCHEMA_H__
3
+
4
+#include "Tuple.h"
5
+
6
+class schema {
7
+    public:
8
+        Tuple s;
9
+};
10
+
11
+#endif

+ 14
- 0
cs236/submission/lab02/fact.h View File

@@ -0,0 +1,14 @@
1
+#ifndef __FACT_H__
2
+#define __FACT_H__
3
+
4
+#include "predicate.h"
5
+#include <vector>
6
+#include <sstream>
7
+
8
+using namespace std;
9
+
10
+class fact {
11
+    public:
12
+        vector<predicate> pred_facts;
13
+};
14
+#endif

+ 30
- 0
cs236/submission/lab02/lab02.cpp View File

@@ -0,0 +1,30 @@
1
+#include <vector>
2
+#include "lexi.h"  
3
+#include "util.h"
4
+#include "token.h"
5
+#include "parser.h"
6
+
7
+const string usage = "usage: app <input> <output>";
8
+                       
9
+int main(int argc, char* argv[]) {
10
+    if(argc != 3) {
11
+        cerr << usage << endl;
12
+        return 1;
13
+    }
14
+    get_file_name(argv[1]);
15
+    vector<string> data = open_file(argv[1]);
16
+    lexi l;
17
+    string temp = argv[2];
18
+    vector<token> s = l.lexical_analyzer(data, temp);
19
+    parser p;
20
+    p.tokens = s;
21
+    try {
22
+        p.check_datalog();
23
+        string out = p.out();
24
+        write_file(out, argv[2]);
25
+    } catch(string str) {
26
+        stringstream s;
27
+        s << "Failure!\n  " << str;
28
+        write_file(s.str(), argv[2]);
29
+    }
30
+}

+ 361
- 0
cs236/submission/lab02/lexi.cpp View File

@@ -0,0 +1,361 @@
1
+#include "lexi.h"
2
+
3
+vector<token> lexi::lexical_analyzer(vector<string> data, string file_name) {
4
+    string cur_string;
5
+    string next_character;
6
+    for(unsigned int i = 0; i < data.size(); i++) {
7
+        for(unsigned int j = 0; j < data[i].size(); j ++) {
8
+            cur_string = data[i].at(j);
9
+            if(j < data[i].size() - 1) {
10
+                next_character = data[i].at(j + 1);
11
+            }
12
+            else {
13
+                next_character = "";
14
+            }
15
+            string state = determiner(cur_string, next_character);
16
+            simple_state(data, state, cur_string, next_character, i, j);
17
+            simple_state_string(data, state, cur_string, next_character, i, j);
18
+            if(state == "start of string") {
19
+                string token_symbol = string_finder(data, i, j);
20
+                if(token_symbol != "error") {
21
+                    token_symbol.erase(0,1);
22
+                    token t("STRING", token_symbol, i + 1);
23
+                    tokens.push_back(t);
24
+                }
25
+                else {
26
+                    write_to_file(file_name, i + 1);
27
+                    return tokens;
28
+                }
29
+            }
30
+            simple_comment(data, state, cur_string, next_character, i, j);
31
+            simple_id(data, state, cur_string, next_character, i, j);
32
+            if(state == "error") {
33
+                write_to_file(file_name, i + 1);
34
+                return tokens;
35
+            }
36
+        }
37
+    }
38
+    write_to_file(file_name);
39
+    return tokens;
40
+}
41
+
42
+void lexi::write_to_file(string file_name, int line) {
43
+    ofstream myfile;
44
+    myfile.open(file_name.c_str());
45
+    myfile << "Error on line " << line << endl;
46
+    myfile.close();
47
+}
48
+
49
+void lexi::write_to_file(string file_name) {
50
+    ofstream myfile;
51
+    myfile.open(file_name.c_str());
52
+    for(unsigned int i = 0; i < tokens.size(); i++) {
53
+        if(i < tokens.size()) {
54
+            myfile << tokens[i] << endl;
55
+        }
56
+        else {
57
+            myfile << tokens[i];
58
+        }
59
+    }
60
+    myfile << "Total Tokens = " << tokens.size();
61
+    myfile << endl;
62
+    myfile.close();
63
+}
64
+
65
+
66
+bool lexi::simple_comment(vector<string> & data, string state, string cur_string, string next_character, int i, int j) {
67
+    if(state == "comment") {
68
+        string token_symbol = comment_finder(data, i, j);
69
+    }
70
+    return true;
71
+}
72
+
73
+bool lexi::simple_id(vector<string> & data, string state, string cur_string, string next_character, int i, int j) {
74
+    if(state == "id") {
75
+        string token_symbol = id_finder(data, i, j);
76
+        if(token_symbol != "error") {
77
+            token t("ID", token_symbol, i + 1);
78
+            tokens.push_back(t);
79
+        }
80
+    }
81
+    return true;
82
+}
83
+
84
+bool lexi::simple_state_string(vector<string> & data, string state, string cur_string, string next_character, int i, int j) {
85
+    if(state == "simple_string") {
86
+        string token_symbol = det_type_simple_string(data, i, j);
87
+        if(token_symbol != "wrong") {
88
+            string token_type = type_simple_string(token_symbol);
89
+            token t(token_type, token_symbol, i + 1);
90
+            tokens.push_back(t);
91
+        }
92
+        else {
93
+            string token_symbol = id_finder(data, i, j);
94
+            if(token_symbol != "error") {
95
+                token t("ID", token_symbol, i + 1);
96
+                tokens.push_back(t);
97
+            }
98
+        }
99
+    }
100
+    return true;
101
+}
102
+
103
+bool lexi::simple_state(vector<string> & data, string state, string cur_string, string next_character, int i, int j) {
104
+    if(state == "simple") {
105
+        string token_symbol = type_simple(cur_string, next_character);
106
+        if(next_character == "-") {
107
+            data[i].replace(j,2, " ");
108
+        }
109
+        else {
110
+            data[i].replace(j,1, " ");
111
+        }
112
+        string token_id = type_simple_caps(cur_string, next_character);
113
+        token t(token_id, token_symbol, i + 1);
114
+        tokens.push_back(t);
115
+    }
116
+    return true;
117
+}
118
+
119
+string lexi::determiner(string cur_string, string next_character) {
120
+    if(det_help_simple(cur_string, next_character)) {
121
+        return "simple";
122
+    }
123
+    else if(det_help_simple_string(cur_string, next_character)) {
124
+        return "simple_string";
125
+    }
126
+    else if(det_help_id(cur_string)) {
127
+        return "id";
128
+    }
129
+    else if(cur_string == "'") {
130
+        return "start of string";
131
+    }
132
+    else if(cur_string == "#") {
133
+        return "comment";
134
+    }
135
+    else {
136
+        string temp = incorrect(cur_string);
137
+        return temp;
138
+    }
139
+    return "";
140
+}
141
+
142
+bool lexi::det_help_id(string cur_string) {
143
+    if(('A' <= cur_string[0] && cur_string[0] <= 'Z') ||
144
+     ('a' <= cur_string[0] && cur_string[0] <= 'z')) {
145
+        return true;
146
+    }
147
+    return false;
148
+}
149
+
150
+bool lexi::quick_help(string a, string b) {
151
+    if(a == "S" && b == "c") {
152
+        return true;
153
+    }
154
+    return false;
155
+}
156
+
157
+bool lexi::det_help_simple_string(string cur_string, string next_character) {
158
+    if(quick_help(cur_string, next_character)) {
159
+        return true;
160
+    }
161
+    else if((cur_string == "Q" && next_character == "u")) {
162
+        return true;
163
+    }
164
+    else if((cur_string == "R" && next_character == "u")) { 
165
+        return true;
166
+    }
167
+    else if((cur_string == "F" && next_character == "a")) {
168
+        return true;
169
+    }
170
+    return false;
171
+}
172
+
173
+bool lexi::det_help_simple(string cur_string, string next_character) {
174
+    if(cur_string == "," || cur_string == "." || cur_string == "?" ||
175
+            cur_string == "(" || cur_string == ")" || cur_string == ":") {
176
+        type_simple(cur_string, next_character);
177
+        return true;
178
+    }
179
+    return false;
180
+}
181
+
182
+string lexi::incorrect(string cur_string) {
183
+    if(cur_string == " " || cur_string == "\t") {
184
+        return "fine";
185
+    }
186
+    else if(!(('A' <= cur_string[0] && cur_string[0] <= 'Z') ||
187
+     ('a' <= cur_string[0] && cur_string[0] <= 'z'))) {
188
+        return "error";
189
+    }
190
+    return " ";
191
+}
192
+
193
+string lexi::id_finder(vector<string> & data, int a, int b) {
194
+    string cur_string;
195
+    string next_character;
196
+    for(unsigned int j = b; j < data[a].size(); j++) {
197
+        cur_string += data[a].at(j);
198
+        if(j < data[a].size() - 1) {
199
+            next_character = data[a].at(j + 1);
200
+        }
201
+        else {
202
+            next_character = "!";
203
+        }
204
+        if(is_char_valid(next_character[0]) || next_character == "!") {
205
+            data[a].replace(data[a].begin() + b, data[a].begin() + j + 1, " ");
206
+            return cur_string;
207
+        }
208
+    }
209
+    return " ";
210
+}
211
+
212
+string lexi::comment_finder(vector<string> & data, int i, int b) {
213
+    string cur_string;
214
+    string next_character;
215
+    for(unsigned int j = b; j < data[i].size(); j++) {
216
+        cur_string += data[i].at(j);
217
+        if(j < data[i].size() - 1) {
218
+            next_character = data[i].at(j + 1);
219
+        }
220
+        else {
221
+            next_character = "!";
222
+        }
223
+        if((j > data[i].size()) - 1 && next_character != "!") {
224
+            data[i].replace(data[i].begin() + b, data[i].end(), " ");
225
+            return cur_string;
226
+        }
227
+    }
228
+    return "error";
229
+}
230
+
231
+string lexi::string_finder(vector<string> & data, int a, int b) {
232
+    string cur_string;
233
+    string next_character;
234
+    b = data[a].find('\'');
235
+    for(unsigned int j = b; j < data[a].size(); j++) {
236
+        cur_string += data[a].at(j);
237
+        if(j < data[a].size() - 1) {
238
+            next_character = data[a].at(j + 1);
239
+        }
240
+        if(next_character == "'") {
241
+            data[a].replace(data[a].begin() + b, data[a].begin() + j + 2, " ");
242
+            data[a].insert(data[a].begin() + b, ' ');
243
+            return cur_string;
244
+        }
245
+    }
246
+    return "error";
247
+}
248
+
249
+string lexi::type_simple_caps(string symbol, string next_symbol) {
250
+    if(symbol == ",") {
251
+        return "COMMA";
252
+    }
253
+    else if(symbol == ".") {
254
+        return "PERIOD";
255
+    }
256
+    else if(symbol == "?") {
257
+        return "Q_MARK";
258
+    }
259
+    else if(symbol == "(") {
260
+        return "LEFT_PAREN";
261
+    }
262
+    else if(symbol == ")") {
263
+        return "RIGHT_PAREN";
264
+    }
265
+    else if(symbol == ":") {
266
+        if(next_symbol == "-") {
267
+            return "COLON_DASH";
268
+        }
269
+        return "COLON";
270
+    }
271
+    return "";
272
+}
273
+
274
+string lexi::type_simple(string symbol, string next_symbol) {
275
+    if(symbol == ",") {
276
+        return ",";
277
+    }
278
+    else if(symbol == ".") {
279
+        return ".";
280
+    }
281
+    else if(symbol == "?") {
282
+        return "?";
283
+    }
284
+    else if(symbol == "(") {
285
+        return "(";
286
+    }
287
+    else if(symbol == ")") {
288
+        return ")";
289
+    }
290
+    else if(symbol == ":") {
291
+        if(next_symbol == "-") {
292
+            return ":-";
293
+        }
294
+        return ":";
295
+    }
296
+    return "";
297
+}
298
+
299
+string lexi::det_type_simple_string(vector<string> & data, int i, int b) {
300
+    string cur_string;
301
+    string next_character;
302
+    string special_case;
303
+    if(b > 0) {
304
+        special_case = data[i].at(b -1);
305
+    }
306
+    for(unsigned int j = b; j < data[i].size(); j++) {
307
+        cur_string += data[i].at(j);
308
+        if(j < data[i].size() - 1) {
309
+            next_character = data[i].at(j + 1);
310
+        }
311
+        else {
312
+            next_character = "!";
313
+        }
314
+        if((is_simple_string(cur_string)) && (is_char_valid(next_character.at(0))) && (is_char_valid(special_case[0]))) {
315
+            data[i].replace(data[i].begin() + b, data[i].begin() + j + 1, " ");
316
+            return cur_string;
317
+        }
318
+    }
319
+    return "wrong";
320
+}
321
+
322
+bool lexi::is_char_valid(char next_character) {
323
+    if(!(('A' <= next_character && next_character <= 'Z') ||
324
+     ('a' <= next_character && next_character <= 'z') || 
325
+     ('0' <= next_character && next_character <= '9')) || (next_character == '\'')) {
326
+        return true;
327
+    }
328
+    return false;
329
+}
330
+
331
+bool lexi::is_simple_string(string simple_com) {
332
+    if(simple_com == "Schemes") {
333
+        return true;
334
+    }
335
+    else if(simple_com == "Facts") {
336
+        return true;
337
+    }
338
+    else if(simple_com == "Rules") {
339
+        return true;
340
+    }
341
+    else if(simple_com == "Queries") {
342
+        return true;
343
+    }
344
+    return false;
345
+}
346
+
347
+string lexi::type_simple_string(string simple_com) {
348
+    if(simple_com == "Schemes") {
349
+        return "SCHEMES";
350
+    }
351
+    else if(simple_com == "Facts") {
352
+        return "FACTS";
353
+    }
354
+    else if(simple_com == "Rules") {
355
+        return "RULES";
356
+    }
357
+    else if(simple_com == "Queries") {
358
+        return "QUERIES";
359
+    }
360
+    return "";
361
+}

+ 44
- 0
cs236/submission/lab02/lexi.h View File

@@ -0,0 +1,44 @@
1
+#ifndef __LEXI_H__                          
2
+#define __LEXI_H__                          
3
+                                              
4
+#include <iostream>
5
+#include <fstream>
6
+#include <vector>
7
+#include "token.h"
8
+#include <cctype>  
9
+#include <string>  
10
+#include <sstream> 
11
+
12
+                                              
13
+using namespace std;                          
14
+                                              
15
+class lexi {                                             
16
+    public:
17
+        lexi(){}
18
+        vector<token> tokens;
19
+        string determiner(string, string);
20
+        vector<token> lexical_analyzer(vector<string>, string);
21
+        string type_simple(string, string);
22
+        string type_simple_string(string);
23
+        bool is_simple_string(string);
24
+        string det_type_simple_string(vector<string>&, int, int);
25
+        bool is_char_valid(char);
26
+        string string_finder(vector<string>&, int, int);
27
+        string comment_finder(vector<string>&, int, int);
28
+        string id_finder(vector<string>&, int, int);
29
+        string incorrect(string);
30
+        bool det_help_simple(string, string);
31
+        bool det_help_simple_string(string, string);
32
+        bool quick_help(string, string);
33
+        bool det_help_id(string);
34
+        string type_simple_caps(string, string);
35
+        bool simple_state(vector<string>&, string, string, string, int, int);
36
+        bool simple_state_string(vector<string>&, string, string, string, int, int);
37
+        bool simple_id(vector<string>&, string, string, string, int, int);
38
+        bool simple_comment(vector<string>&, string, string, string, int, int);
39
+        void write_to_file(string);
40
+        void write_to_file(string, int);
41
+};                                            
42
+                                              
43
+#endif                                        
44
+

+ 13
- 0
cs236/submission/lab02/parameter.h View File

@@ -0,0 +1,13 @@
1
+#ifndef __PARAMETER_H__
2
+#define __PARAMETER_H__
3
+
4
+#include <iostream>
5
+
6
+using namespace std;
7
+
8
+class parameter {
9
+    public:
10
+        string param;
11
+        string type;
12
+};
13
+#endif

+ 200
- 0
cs236/submission/lab02/parser.cpp View File

@@ -0,0 +1,200 @@
1
+#include "parser.h"
2
+
3
+string parser::get_token() {
4
+    string type = tokens[0].type;
5
+    return type;
6
+}
7
+
8
+void parser::check_datalog() {
9
+    match("SCHEMES");
10
+    match("COLON");
11
+    if(get_token() == "FACTS") {
12
+        error();
13
+    }
14
+    check_schemelist(get_token());
15
+    match("FACTS");
16
+    match("COLON");
17
+    check_factlist(get_token());
18
+    match("RULES");
19
+    match("COLON");
20
+    check_rulelist(get_token());
21
+    match("QUERIES");
22
+    match("COLON");
23
+    check_querylist(get_token());
24
+    out();
25
+}
26
+
27
+string parser::out() {
28
+    stringstream s;
29
+    s << "Success!" << endl;
30
+    s << "Schemes(" << schemelist.size() << "):" << endl;
31
+    for(unsigned int i = 0; i < schemelist.size(); i++) {
32
+        s << "  " << schemelist[i].toString();
33
+    }
34
+    s << "Facts(" << factlist.size() << "):" << endl;
35
+    for(unsigned int i = 0; i < factlist.size(); i++) {
36
+        s << "  " << factlist[i].toString(false);
37
+    }
38
+    s << "Rules(" << rulelist.size() << "):" << endl;
39
+    for(unsigned int i = 0; i < rulelist.size(); i++) {
40
+        s << "  " << rulelist[i].toString();
41
+    }
42
+    s << "Queries(" << querylist.size() << "):" << endl;
43
+    double a = 0;
44
+    for(unsigned int i = 0; i < querylist.size(); i++) {
45
+        s << "  " << querylist[i].toString(a);
46
+    }
47
+    s << "Domain(" << domain.size() << "):" << endl;
48
+    for (auto it=domain.cbegin(); it != domain.cend(); ++it) {
49
+        s << "  '" << *it << "'" << endl;
50
+    }
51
+    return s.str();
52
+}
53
+
54
+void parser::check_schemelist(string type) {
55
+    if(type == "FACTS") {
56
+        return;
57
+    }
58
+    else {
59
+        check_scheme(type);
60
+        check_schemelist(get_token());
61
+    }
62
+}
63
+
64
+void parser::check_scheme(string type) {
65
+    schemelist.push_back(check_predicate(type));
66
+}
67
+
68
+void parser::check_factlist(string type) {
69
+    if(type == "RULES") {
70
+        return;
71
+    }
72
+    else {
73
+        check_fact(type);
74
+        check_factlist(get_token());
75
+    }
76
+}
77
+
78
+void parser::check_fact(string type) {
79
+    factlist.push_back(check_predicate(type));
80
+    match("PERIOD");
81
+}
82
+
83
+void parser::check_rulelist(string type) {
84
+    if(type == "QUERIES") {
85
+        return;
86
+    }
87
+    else {
88
+        check_rule(type);
89
+        check_rulelist(get_token());
90
+    }
91
+}
92
+
93
+void parser::check_rule(string type) {
94
+    rule r;
95
+    r.head = check_predicate(type);
96
+    match("COLON_DASH");
97
+    check_predicate_list(get_token(), r);
98
+    match("PERIOD");
99
+    rulelist.push_back(r);
100
+}
101
+
102
+void parser::check_querylist(string type) {
103
+    check_query(type);
104
+    if(tokens.empty()) {
105
+        return;
106
+    }
107
+    else {
108
+        check_querylist(get_token());
109
+    }
110
+}
111
+
112
+void parser::check_query(string type) {
113
+    querylist.push_back(check_predicate(type));
114
+    match("Q_MARK");
115
+}
116
+
117
+void parser::check_predicate_list(string type, rule& r) {
118
+    r.pred_rule.push_back(check_predicate(type));
119
+    if(get_token() == "COMMA") {
120
+        match("COMMA");
121
+        check_predicate_list(get_token(), r);
122
+    }
123
+    else {
124
+        return;
125
+    }
126
+}
127
+
128
+predicate parser::check_predicate(string type) {
129
+    predicate pred;
130
+    pred.id  = tokens[0].character;
131
+    match("ID");
132
+    match("LEFT_PAREN");
133
+    if(get_token() == "RIGHT_PAREN") {
134
+        error();
135
+    }
136
+    check_parameterlist(get_token(), pred);
137
+    match("RIGHT_PAREN");
138
+    return pred;
139
+}
140
+
141
+void parser::check_parameterlist(string type, predicate& pred) {
142
+    if(type == "RIGHT_PAREN") {
143
+        return;
144
+    }
145
+    else {
146
+        check_parameter(type, pred);
147
+        if(get_token() == "COMMA") {
148
+            match("COMMA");
149
+            if(get_token() == "RIGHT_PAREN") {
150
+                error();
151
+            }
152
+            check_parameterlist(get_token(), pred);
153
+        }
154
+        else {
155
+            return;
156
+        }
157
+    }
158
+}
159
+
160
+void parser::check_parameter(string type, predicate& pred) {
161
+    parameter para;
162
+    if(type == "STRING") {
163
+        domain.insert(tokens[0].character);
164
+        para.param = tokens[0].character;
165
+        para.type = tokens[0].type;
166
+        pred.pred_list.push_back(para);
167
+        match("STRING");
168
+        return;
169
+    }
170
+    else if(type == "ID") {
171
+        para.param = tokens[0].character;
172
+        para.type = tokens[0].type;
173
+        pred.pred_list.push_back(para);
174
+        match("ID");
175
+        return;
176
+    }
177
+    else {
178
+        error();
179
+    }
180
+}
181
+
182
+void parser::match(string type) {
183
+    if(get_token() == type) {
184
+        if(tokens.empty()) {
185
+            error();
186
+        }
187
+        else {
188
+            tokens.erase(tokens.begin());
189
+        }
190
+    }
191
+    else {
192
+        error();
193
+    }
194
+}
195
+
196
+void parser::error() {
197
+    stringstream oss;
198
+    oss << tokens[0] << endl;
199
+    throw oss.str();
200
+}

+ 49
- 0
cs236/submission/lab02/parser.h View File

@@ -0,0 +1,49 @@
1
+#ifndef __PARSER_H__
2
+#define __PARSER_H__
3
+
4
+#include <iostream>
5
+#include <sstream>
6
+#include <vector>
7
+#include <set>
8
+
9
+#include "token.h"
10
+#include "scheme.h"
11
+#include "fact.h"
12
+#include "rule.h"
13
+#include "query.h"
14
+#include "predicate.h"
15
+#include "parameter.h"
16
+
17
+using namespace std;
18
+
19
+class parser {
20
+    public:
21
+        parser() {}
22
+        vector<token> tokens;
23
+        set<string> domain;
24
+        vector<predicate> schemelist;
25
+        vector<predicate> factlist;
26
+        vector<predicate> querylist;
27
+        vector<predicate> predlist;
28
+        vector<rule> rulelist;
29
+
30
+        string get_token();
31
+        void check_datalog();
32
+        void check_schemelist(string);
33
+        void check_scheme(string);
34
+        void check_factlist(string);
35
+        void check_fact(string);
36
+        void check_rulelist(string);
37
+        void check_rule(string);
38
+        void check_querylist(string);
39
+        void check_query(string);
40
+        void check_predicate_list(string, rule&);
41
+        predicate check_predicate(string);
42
+        void check_parameterlist(string type, predicate&);
43
+        void check_parameter(string, predicate&);
44
+        void match(string);
45
+        void error();
46
+        string out();
47
+
48
+};
49
+#endif

+ 83
- 0
cs236/submission/lab02/predicate.h View File

@@ -0,0 +1,83 @@
1
+#ifndef __PREDICATE_H__
2
+#define __PREDICATE_H__
3
+
4
+#include "parameter.h"
5
+#include <vector>
6
+#include <iostream>
7
+
8
+using namespace std;
9
+
10
+class predicate {
11
+    public:
12
+        string id;
13
+        vector<parameter> pred_list;
14
+        string toString() {
15
+            //schemes
16
+            stringstream s;
17
+            s << id << "(";
18
+            for(unsigned int i = 0; i < pred_list.size(); i++) {
19
+                s << pred_list[i].param;
20
+                if(i < pred_list.size()-1) {
21
+                    s << ",";
22
+                }
23
+            }
24
+            s << ")\n";
25
+            return s.str();
26
+        }
27
+        string toString(bool a) {
28
+            //facts
29
+            stringstream s;
30
+            s << id << "(";
31
+            for(unsigned int i = 0; i < pred_list.size(); i++) {
32
+                s << "'" << pred_list[i].param << "'";
33
+                if(i < pred_list.size()-1) {
34
+                    s << ",";
35
+                }
36
+            }
37
+            s << ").\n";
38
+            return s.str();
39
+        }
40
+        string toString(double a) {
41
+            //query
42
+            stringstream s;
43
+            s << id << "(";
44
+            for(unsigned int i = 0; i < pred_list.size(); i++) {
45
+                if(pred_list[i].type == "STRING") {
46
+                    s << "'" << pred_list[i].param << "'";
47
+                    if(i < pred_list.size()-1) {
48
+                        s << ",";
49
+                    }
50
+                }
51
+                if(pred_list[i].type == "ID") {
52
+                    s << pred_list[i].param;
53
+                    if(i < pred_list.size()-1) {
54
+                        s << ",";
55
+                    }
56
+                }
57
+            }
58
+            s << ")?\n";
59
+            return s.str();
60
+        }
61
+        string toString(int a) {
62
+            //rules
63
+            stringstream s;
64
+            s << id << "(";
65
+            for(unsigned int i = 0; i < pred_list.size(); i++) {
66
+                if(pred_list[i].type == "STRING") {
67
+                    s << "'" << pred_list[i].param << "'";
68
+                    if(i < pred_list.size()-1) {
69
+                        s << ",";
70
+                    }
71
+                }
72
+                if(pred_list[i].type == "ID") {
73
+                    s << pred_list[i].param;
74
+                    if(i < pred_list.size()-1) {
75
+                        s << ",";
76
+                    }
77
+                }
78
+            }
79
+            s << ")";
80
+            return s.str();
81
+        }
82
+};
83
+#endif

+ 14
- 0
cs236/submission/lab02/query.h View File

@@ -0,0 +1,14 @@
1
+#ifndef __QUERY_H__
2
+#define __QUERY_H__
3
+
4
+#include "predicate.h"
5
+#include <vector>
6
+#include <iostream>
7
+
8
+using namespace std;
9
+
10
+class query {
11
+    public:
12
+        vector<predicate> pred_queries;
13
+};
14
+#endif

+ 27
- 0
cs236/submission/lab02/rule.h View File

@@ -0,0 +1,27 @@
1
+#ifndef __RULE_H__
2
+#define __RULE_H__
3
+
4
+#include "predicate.h"
5
+#include <vector>
6
+#include <iostream>
7
+
8
+using namespace std;
9
+
10
+class rule {
11
+    public:
12
+        predicate head;
13
+        vector<predicate> pred_rule;
14
+        string toString() {
15
+            stringstream s;
16
+            s << head.toString(1) << " :- ";
17
+            for(unsigned int i = 0; i < pred_rule.size(); i++) {
18
+                s << pred_rule[i].toString(1);
19
+                if(i < pred_rule.size()-1) {
20
+                    s << ",";
21
+                }
22
+            }
23
+            s << ".\n";
24
+            return s.str();
25
+        }
26
+};
27
+#endif

+ 14
- 0
cs236/submission/lab02/scheme.h View File

@@ -0,0 +1,14 @@
1
+#ifndef __SCHEME_H__
2
+#define __SCHEME_H__
3
+
4
+#include "predicate.h"
5
+#include <vector>
6
+#include <iostream>
7
+
8
+using namespace std;
9
+
10
+class scheme {
11
+    public:
12
+        vector<predicate> pred_schemes;
13
+};
14
+#endif

+ 11
- 0
cs236/submission/lab02/token.cpp View File

@@ -0,0 +1,11 @@
1
+#include "token.h"
2
+
3
+token::token(string type, string character, int line_num) :           
4
+                        type(type), character(character), line_num(line_num) {}
5
+
6
+ostream & operator<<(ostream & os, token tk) {
7
+    os << "(" << tk.type                          
8
+    << ",\"" << tk.character                      
9
+    << "\"," << tk.line_num << ")";                                            
10
+    return os;                                             
11
+}                                                          

+ 16
- 0
cs236/submission/lab02/token.h View File

@@ -0,0 +1,16 @@
1
+#ifndef __TOKEN_H__                          
2
+#define __TOKEN_H__                          
3
+
4
+#include <iostream>
5
+
6
+using namespace std;
7
+                                              
8
+class token {
9
+    public:
10
+        token(string, string, int);
11
+        string type;
12
+        string character;
13
+        int line_num;
14
+        friend ostream & operator<<(ostream & os, token tk);
15
+};
16
+#endif                                        

+ 56
- 0
cs236/submission/lab02/util.h View File

@@ -0,0 +1,56 @@
1
+#ifndef __UTIL_H__                          
2
+#define __UTIL_H__                          
3
+#include <vector>
4
+#include <iostream>
5
+#include <fstream>
6
+
7
+vector<string> open_file(string file_name) {
8
+    ifstream myfile;
9
+    vector<string> data;
10
+    myfile.open(file_name.c_str());
11
+    string temp;
12
+    while(!myfile.eof()) {
13
+        getline(myfile, temp);
14
+        data.push_back(temp);
15
+    }
16
+    myfile.close();
17
+    return data;
18
+}
19
+
20
+bool get_file_name(string input) {
21
+    bool file_correct = false;    
22
+    string input_file_name;
23
+    while(!file_correct) {                        
24
+        ifstream inputs(input.c_str());           
25
+        if(inputs.good()) {                       
26
+            input_file_name = input;              
27
+            file_correct = true;                  
28
+            open_file(input_file_name);
29
+            return true;
30
+        }                                         
31
+        else {                                    
32
+            cerr << "incorrect file name" << endl;
33
+            return false;
34
+        }                                         
35
+    }                                             
36
+    return false;
37
+}
38
+
39
+void write_file(string output, string file_name) {
40
+    ofstream myfile;
41
+    myfile.open(file_name.c_str());
42
+    myfile << output << "\n";
43
+}
44
+
45
+void write_file(vector<string> output, string file_name) {
46
+    ofstream myfile;
47
+    myfile.open(file_name.c_str());
48
+    for(unsigned int i = 0; i < output.size(); i++) {
49
+        if(i != output.size() -1) {
50
+            myfile << output[i] << "\n";
51
+        }
52
+        myfile << output[i];
53
+    }
54
+}
55
+
56
+#endif                                        

+ 21
- 0
cs236/tests/lab01/input.txt View File

@@ -0,0 +1,21 @@
1
+
2
+
3
+Schemes:
4
+    student(Name, ID, Address, Major)
5
+
6
+Facts:
7
+    Student('North', '51', '10 Main', 'CS').
8
+#   |student('Reagan', '52', '11 Maple', 'CS').|
9
+    student('Reagan', '52', '11 Maple', 'CS').
10
+    student('Clinton', '53', '12 Ashton', 'MATH').
11
+
12
+Rules:
13
+    major(Name,Major):-student(Name,ID,Address,Major).
14
+
15
+Queries:
16
+    major('North',Major)?
17
+    major(Name,'MATH')?
18
+#|    major(Name,'MATH')?
19
+#    major(Name,'MATH')? |#
20
+#   |major(Name,'MATH')?|
21
+

+ 21
- 0
cs236/tests/lab01/test1.txt View File

@@ -0,0 +1,21 @@
1
+
2
+
3
+Schemes:
4
+    student(Name, ID, Address, Major)
5
+
6
+Facts:
7
+    Student('North', '51', '10 Main', 'CS').
8
+#   |student('Reagan', '52', '11 Maple', 'CS').|
9
+    student('Reagan', '52', '11 Maple', 'CS').
10
+    student('Clinton', '53', '12 Ashton', 'MATH').
11
+
12
+Rules:
13
+    major(Name,Major):-student(Name,ID,Address,Major).
14
+
15
+Queries:
16
+    major('North',Major)?
17
+    major(Name,'MATH')?
18
+#|    major(Name,'MATH')?
19
+#    major(Name,'MATH')? |#
20
+#   |major(Name,'MATH')?|
21
+

+ 17
- 0
cs236/tests/lab02/in/in21.txt View File

@@ -0,0 +1,17 @@
1
+
2
+
3
+Schemes:
4
+  snap(S,N,A,P)
5
+  HasSameAddress(X,Y)
6
+
7
+Facts:
8
+  snap('12345','C. Brown','12 Apple','555-1234').
9
+  snap('33333','Snoopy','12 Apple','555-1234').
10
+
11
+Rules:
12
+  HasSameAddress(X,'Y') :- snap(A,X,B,C),snap(D,Y,'B',E).
13
+
14
+Queries:
15
+  HasSameAddress('Snoopy',Who)?
16
+
17
+

+ 16
- 0
cs236/tests/lab02/in/in22.txt View File

@@ -0,0 +1,16 @@
1
+Schemes:
2
+    snap(S,N,A,P) 
3
+    NameHasID(N,S)
4
+ 
5
+Facts:
6
+    snap('12345','C. Brown','12 Apple','555-1234').
7
+    snap('67890','L. Van Pelt','34 Pear','555-5678').
8
+ 
9
+Rules:
10
+    NameHasID(N,S) :- snap(S,N,A,P)?
11
+ 
12
+Queries:
13
+    NameHasID('Snoopy',Id)?
14
+ 
15
+
16
+

+ 19
- 0
cs236/tests/lab02/in/in23.txt View File

@@ -0,0 +1,19 @@
1
+
2
+
3
+Schemes:
4
+  bob(A,B)
5
+
6
+Facts:
7
+  bob('4','2').
8
+
9
+Rules:
10
+  bob(A,B) :- bob(B,A).
11
+
12
+Queries:
13
+  bob(X,Y)?
14
+
15
+
16
+# extra tokens at end of file
17
+
18
+????
19
+

+ 19
- 0
cs236/tests/lab02/in/in24.txt View File

@@ -0,0 +1,19 @@
1
+
2
+
3
+Schemes:
4
+  bob(A,B)
5
+
6
+Facts:
7
+  bob('4','2').
8
+
9
+Rules:
10
+  bob(A,B) :- bob(B,A).
11
+
12
+
13
+# wrong punctuation for query
14
+
15
+Queries:
16
+  bob(X,Y).
17
+
18
+
19
+

+ 18
- 0
cs236/tests/lab02/in/in25.txt View File

@@ -0,0 +1,18 @@
1
+
2
+
3
+Schemes:
4
+  bob(A,B)
5
+
6
+# missing punctuation on fact
7
+
8
+Facts:
9
+  bob('4','2')
10
+
11
+Rules:
12
+  bob(A,B) :- bob(B,A).
13
+
14
+Queries:
15
+  bob(X,Y)?
16
+
17
+
18
+

+ 16
- 0
cs236/tests/lab02/in/in26.txt View File

@@ -0,0 +1,16 @@
1
+
2
+
3
+Schemes:
4
+  bob(A,B)
5
+
6
+# fact/rule lists should be optional
7
+
8
+Facts:
9
+
10
+Rules:
11
+
12
+Queries:
13
+  bob(X,Y)?
14
+
15
+
16
+

+ 14
- 0
cs236/tests/lab02/in/in27.txt View File

@@ -0,0 +1,14 @@
1
+
2
+
3
+# scheme/query lists should be required
4
+
5
+Schemes:
6
+
7
+Facts:
8
+
9
+Rules:
10
+
11
+Queries:
12
+
13
+
14
+

+ 21
- 0
cs236/tests/lab02/in/in28.txt View File

@@ -0,0 +1,21 @@
1
+
2
+
3
+Schemes:
4
+  bob(A,B)
5
+
6
+Facts:
7
+  bob('4','2').
8
+
9
+# rules section should exist
10
+# queries should come after rules
11
+
12
+Queries:
13
+  bob(X,Y)?
14
+
15
+# facts can't be repeated
16
+
17
+Facts:
18
+  bob('4','4').
19
+
20
+
21
+

+ 19
- 0
cs236/tests/lab02/out/out21.txt View File

@@ -0,0 +1,19 @@
1
+Success!
2
+Schemes(2):
3
+  snap(S,N,A,P)
4
+  HasSameAddress(X,Y)
5
+Facts(2):
6
+  snap('12345','C. Brown','12 Apple','555-1234').
7
+  snap('33333','Snoopy','12 Apple','555-1234').
8
+Rules(1):
9
+  HasSameAddress(X,Y) :- snap(A,X,B,C),snap(D,Y,B,E).
10
+Queries(1):
11
+  HasSameAddress('Snoopy',Who)?
12
+Domain(6):
13
+  '12 Apple'
14
+  '12345'
15
+  '33333'
16
+  '555-1234'
17
+  'C. Brown'
18
+  'Snoopy'
19
+

+ 3
- 0
cs236/tests/lab02/out/out22.txt View File

@@ -0,0 +1,3 @@
1
+Failure!
2
+  (Q_MARK,"?",10)
3
+

+ 3
- 0
cs236/tests/lab02/out/out23.txt View File

@@ -0,0 +1,3 @@
1
+Failure!
2
+  (Q_MARK,"?",18)
3
+

+ 3
- 0
cs236/tests/lab02/out/out24.txt View File

@@ -0,0 +1,3 @@
1
+Failure!
2
+  (PERIOD,".",16)
3
+

+ 3
- 0
cs236/tests/lab02/out/out25.txt View File

@@ -0,0 +1,3 @@
1
+Failure!
2
+  (RULES,"Rules",11)
3
+

+ 9
- 0
cs236/tests/lab02/out/out26.txt View File

@@ -0,0 +1,9 @@
1
+Success!
2
+Schemes(1):
3
+  bob(A,B)
4
+Facts(0):
5
+Rules(0):
6
+Queries(1):
7
+  bob(X,Y)?
8
+Domain(0):
9
+

+ 3
- 0
cs236/tests/lab02/out/out27.txt View File

@@ -0,0 +1,3 @@
1
+Failure!
2
+  (FACTS,"Facts",7)
3
+

+ 3
- 0
cs236/tests/lab02/out/out28.txt View File

@@ -0,0 +1,3 @@
1
+Failure!
2
+  (QUERIES,"Queries",12)
3
+

+ 15
- 0
cs236/tests/lab03/in/in40.txt View File

@@ -0,0 +1,15 @@
1
+Schemes:
2
+  SK(A,B)
3
+Facts:
4
+  SK('a','c').
5
+  SK('b','c').
6
+  SK('b','b').
7
+  SK('b','c').
8
+Rules:
9
+  DoNothing(Z) :- Stuff(Z).
10
+Queries:
11
+  SK(A,'c')?
12
+  SK('b','c')?
13
+  SK(X,X)?
14
+  SK(A,B)?
15
+

+ 11
- 0
cs236/tests/lab03/out/out40.txt View File

@@ -0,0 +1,11 @@
1
+SK(A,'c')? Yes(2)
2
+  A='a'
3
+  A='b'
4
+SK('b','c')? Yes(1)
5
+SK(X,X)? Yes(1)
6
+  X='b'
7
+SK(A,B)? Yes(3)
8
+  A='a', B='c'
9
+  A='b', B='b'
10
+  A='b', B='c'
11
+

Loading…
Cancel
Save