Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/europa/cse476/grammar.cpp
1074 views
1
// Copyright (c) 1998-99, Ed Schlunder
2
3
#include <fstream.h>
4
#include <iostream.h>
5
#include <string.h>
6
#include "grammar.h"
7
#include "gnode.h"
8
9
Grammar::Grammar(char *fileName) {
10
char buffer[1000];
11
ifstream inFile;
12
13
// specify that this is currently an empty grammar rule list
14
rootPtr = currPtr = NULL;
15
16
// try to open the grammar rules file
17
inFile.open(fileName);
18
if(!inFile) {
19
cout << "Grammar rules file can not be opened." << endl;
20
return;
21
}
22
23
while(inFile) {
24
inFile.getline(buffer, 999);
25
26
// ignore blank lines
27
if(strlen(buffer) < 2) continue;
28
trim(buffer);
29
insert(buffer);
30
}
31
}
32
33
void Grammar::insert(char *text) {
34
List *ruleLine = new List;
35
genericNode *obj;
36
char *tmp;
37
int length, level, beg, end;
38
39
length = strlen(text);
40
end = -1;
41
while(end < length) {
42
beg = end + 1;
43
44
// locate opening parenthesis
45
while(beg < length)
46
if(text[beg] == '(') break;
47
else beg++;
48
49
for(level = 0, end = beg + 1; end < length; end++) {
50
if((text[end] == ')') && (level == 0)) break;
51
52
// make sure that items encased within () are kept intact
53
if(text[end] == '(') level++;
54
if(text[end] == ')') level--;
55
}
56
57
end++;
58
tmp = new char[end - beg + 1];
59
strncpy(tmp, text + beg, end - beg);
60
tmp[end - beg] = 0;
61
62
if(strlen(tmp) == 0)
63
break;
64
65
// make a genericNode out of this phrasal object and add it to the ruleLine
66
// cout << '[' << tmp << ']' << endl;
67
obj = new genericNode(tmp);
68
ruleLine->InsertAfter(obj);
69
}
70
71
insert(ruleLine);
72
}
73
74
void Grammar::insert(List *ruleLine) {
75
grammarLine *tmp;
76
77
// cout << "grammarInsert: " << *ruleLine << endl;
78
if(rootPtr == NULL) {
79
rootPtr = new grammarLine;
80
rootPtr->line = ruleLine;
81
rootPtr->nextPtr = NULL;
82
}
83
else {
84
// find end of list
85
tmp = rootPtr;
86
while(tmp->nextPtr != NULL)
87
tmp = tmp->nextPtr;
88
89
// tack a new rule on the end of the list
90
tmp->nextPtr = new grammarLine;
91
tmp = tmp->nextPtr;
92
tmp->line = ruleLine;
93
tmp->nextPtr = NULL;
94
}
95
}
96
97
Grammar::~Grammar() {
98
99
}
100
101
List *Grammar::currLine(void) {
102
return currPtr->line;
103
}
104
105
void Grammar::goTop(void) {
106
currPtr = rootPtr;
107
}
108
109
bool Grammar::goNext(void) {
110
if(currPtr)
111
if(currPtr->nextPtr) {
112
currPtr = currPtr->nextPtr;
113
return true;
114
}
115
116
return false;
117
}
118
119
List *Grammar::findMatch(genericNode *obj) {
120
searchObj = obj;
121
currPtr = rootPtr;
122
123
return findNext();
124
}
125
126
List *Grammar::findNext(void) {
127
List *assign, *currLine;
128
129
// go through each rule in the grammar and try to match it with the given obj
130
while(currPtr) {
131
currLine = currPtr->line;
132
currLine->GoTop();
133
currLine->GoNext();
134
assign = unify(*currLine->currItemNode(), *searchObj);
135
136
currPtr = currPtr->nextPtr;
137
138
if(assign)
139
return substitute(currLine, assign);
140
}
141
142
return NULL;
143
}
144
145