forked from chandrasekharanil/Assignment-2-Group-4-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.cpp
184 lines (163 loc) · 3.43 KB
/
a.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "a.h"
namespace iProlog
{
std::wstring Toks::IF = L"if";
std::wstring Toks::AND = L"and";
std::wstring Toks::DOT = L".";
std::wstring Toks::HOLDS = L"holds";
std::wstring Toks::LISTS = L"lists";
std::wstring Toks::IS = L"is";
Toks *Toks::makeToks(const std::wstring &s, bool const fromFile)
{
try
{
Reader *R;
if (fromFile)
{
R = new FileReader(s);
}
else
{
R = new StringReader(s);
}
Toks * const T = new Toks(R);
return T;
}
catch (const IOException &e)
{
printStackTrace();
return nullptr;
}
}
Toks::Toks(Reader *const reader) : StreamTokenizer(reader)
{
resetSyntax();
eolIsSignificant(false);
ordinaryChar(L'.');
ordinaryChars(L'!', L'/'); // 33-47
ordinaryChars(L':', L'@'); // 55-64
ordinaryChars(L'[', L'`'); // 91-96
ordinaryChars(L'{', L'~'); // 123-126
wordChars(L'_', L'_');
wordChars(L'a', L'z');
wordChars(L'A', L'Z');
wordChars(L'0', L'9');
slashStarComments(true);
slashSlashComments(true);
ordinaryChar(L'%');
}
std::wstring Toks::getWord()
{
std::wstring t = L"";
int c = TT_EOF;
try
{
c = nextToken();
while (std::isspace(c) && c != TT_EOF)
{
c = nextToken();
}
}
catch (const IOException &e)
{
return L"*** tokenizer error:" + t;
}
switch (c)
{
case TT_WORD:
{
constexpr wchar_t first = sval->charAt(0);
if (std::isupper(first) || L'_' == first)
{
t = L"v:" + sval;
}
else
{
try
{
constexpr int n = static_cast<Integer>(sval);
if (std::abs(n) < 1 << 28)
{
t = L"n:" + sval;
}
else
{
t = L"c:" + sval;
}
}
catch (const std::exception &e)
{
t = L"c:" + sval;
}
}
}
break;
case StreamTokenizer::TT_EOF:
{
t = L"";
}
break;
default:
{
t = L"" + StringHelper::toString(static_cast<wchar_t>(c));
}
}
return t;
}
std::vector<std::vector<std::vector<std::wstring>>> Toks::toSentences(const std::wstring &s, bool const fromFile)
{
const std::vector<std::vector<std::vector<std::wstring>>> Wsss = std::vector<std::vector<std::vector<std::wstring>>>();
std::vector<std::vector<std::wstring>> Wss;
std::vector<std::wstring> Ws;
Toks * const toks = makeToks(s, fromFile);
std::wstring t = L"";
while (L"" != (t = toks->getWord()))
{
if (DOT == t)
{
Wss.push_back(Ws);
Wsss.push_back(Wss);
Wss = std::vector<std::vector<std::wstring>>();
Ws = std::vector<std::wstring>();
}
else if ((L"c:" + IF)->equals(t))
{
Wss.push_back(Ws);
Ws = std::vector<std::wstring>();
}
else if ((L"c:" + AND)->equals(t))
{
Wss.push_back(Ws);
Ws = std::vector<std::wstring>();
}
else if ((L"c:" + HOLDS)->equals(t))
{
const std::wstring w = Ws[0];
Ws[0] = L"h:" + w.substr(2);
}
else if ((L"c:" + LISTS)->equals(t))
{
const std::wstring w = Ws[0];
Ws[0] = L"l:" + w.substr(2);
}
else if ((L"c:" + IS)->equals(t))
{
const std::wstring w = Ws[0];
Ws[0] = L"f:" + w.substr(2);
}
else
{
Ws.push_back(t);
}
}
return Wsss;
}
std::wstring Toks::toString(std::vector<void*> &Wsss)
{
return Arrays::deepToString(Wsss);
}
void main(std::vector<std::wstring> &args)
{
pp(toSentences(L"prog.nl", true));
}
}