-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgettoken.cpp
258 lines (229 loc) · 6.34 KB
/
gettoken.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "trifle.h"
using namespace std;
char LineBuffer[MAX_LINE_LEN];
int CurLineNo;
int LineIndex;
char Chr;
Token CurToken;
Token PrevToken;
struct keyWd {
string name;
TokType keyId;
};
static struct keyWd KeyWdT[] = {
{"{", LBrace}, {"}", RBrace},
{"if", If}, {"then", Then},
{"while", While}, {"do", Do},
{"call", Call},{"return", Ret},
{"function", Func}, {"program", Prog},
{"int", Integer}, {"const", Const},
{"write", Write}, {"writeln",WriteLn}, {"writesp",WriteSp},
{"$dummy1",end_of_KeyWd},
{"+", Plus}, {"-", Minus}, {"*", Mult}, {"/", Div},
{"(", Lparen}, {")", Rparen},
{"=", Equal}, {"<", Lss}, {">", Gtr},
{"<>", NotEq}, {"<=", LssEq}, {">=", GtrEq},
{",", Comma}, {".", Period}, {";", Semicolon},
{":=", Assign},
{"$dummy2",end_of_KeySym}
};
string keyname[] {
"LBrace", "RBrace",
"If", "Then",
"While", "Do",
"Call", "Return", "Function", "Program",
"int", "Const",// "Odd",
"Write", "WriteLn", "WriteSp",
"end_of_KeyWd",
"Plus", "Minus",
"Mult", "Div",
"Lparen", "Rparen",
"Equal", "Lss", "Gtr",
"NotEq", "LssEq", "GtrEq",
"Comma", "Period", "Semicolon",
"Assign",
"end_of_KeySym",
"Id", "Num", "Nul",
"end_of_Token",
"Letter", "Digit", "Colon", "Others",
"EndOfFile"
};
ifstream fin;
static TokType getCharType(char chr)
{
if (chr >='0' && chr<='9') return Digit;
if ((chr >='A' && chr<='Z') || (chr >='a' && chr<='z')) return Letter;
switch (chr) {
case '{': return LBrace;
case '}': return RBrace;
case '+': return Plus;
case '-': return Minus;
case '*': return Mult;
case '/': return Div;
case '(': return Lparen;
case ')': return Rparen;
case '=': return Equal;
case '<': return Lss;
case '>': return Gtr;
case ',': return Comma;
case '.': return Period;
case ';': return Semicolon;
case ':': return Colon;
default: break;
}
return Others;
}
void init()
{
LineIndex = -1;
Chr = 0;
CurToken = Token();
}
void openSource(string fileName)
{
fin.open(fileName.c_str(), ios::in); // ios::in means 'read'
CurLineNo = 0;
}
void closeSource()
{
fin.close();
}
static char *removeComment(char *s)
{
int len = strlen(s);
char ws[MAX_LINE_LEN];
char chr = 0;
int j = 0;
for (int i = 0; i < len; i++) {
chr = s[i];
if ((chr == '/') & (i < len-1)) {
if (s[i+1] == '/') break;
}
ws[j++] = chr;
}
ws[j] = 0;
strcpy(s, ws);
return s;
}
static int nextChar()
{
int ch;
if (LineIndex == -1) {
fin.getline(LineBuffer, MAX_LINE_LEN);
CurLineNo += 1;
// cout << setw(3) << CurLineNo;
// cout << ": " << LineBuffer << endl;
LineIndex = 0;
removeComment(LineBuffer);
} else if (fin.eof()) return EOF;
ch = (int)LineBuffer[LineIndex++];
if (ch == 0) LineIndex = -1;
return ch;
}
Token nextToken()
{
PrevToken = CurToken;
TokType prvType = PrevToken.type;
int i = 0;
int num;
Token temp;
string name;
while (1) {
if (Chr==' ' || Chr==0x9 || Chr==0xa || Chr==0xd || Chr==0) ;
else break;
Chr = nextChar();
}
TokType cc = getCharType(Chr);
switch (cc) {
case LBrace:
case RBrace:
name.append(1, Chr);
Chr = nextChar();
for (i=0; i<end_of_KeyWd; i++) {
if (name == KeyWdT[i].name) { temp.type = KeyWdT[i].keyId; }
}
break;
case Letter:
do {
if (i < MAXNAME) name.append(1, Chr);
i++;
Chr = nextChar();
} while (getCharType(Chr)==Letter || getCharType(Chr)==Digit);
for (i=0; i<end_of_KeyWd; i++) {
if (name == KeyWdT[i].name) {
temp.type = KeyWdT[i].keyId;
CurToken = temp;
// cout << "Token : " << keyname[temp.type] << endl;
return temp;
}
}
temp.type = Id;
temp.name = name;
break;
case Digit:
num = 0;
do {
num = 10 * num + (Chr-'0');
i++;
Chr = nextChar();
} while (getCharType(Chr)==Digit);
temp.type = Num;
temp.value = num;
break;
case Minus:
if (prvType==Assign || prvType==Lparen || prvType==Comma || prvType==Plus ||
prvType==Minus || prvType==Mult || prvType==Div) {
Chr = nextChar();
num = 0;
do {
num = 10 * num + (Chr-'0');
i++;
Chr = nextChar();
} while (getCharType(Chr)==Digit);
temp.type = Num;
temp.value = -1 * num;
} else {
temp.type = Minus;
Chr = nextChar();
}
break;
case Colon:
if ((Chr = nextChar()) == '=') {
Chr = nextChar();
temp.type = Assign;
} else temp.type = Nul;
break;
case Lss:
if ((Chr = nextChar()) == '=') {
Chr = nextChar();
temp.type = LssEq;
} else if (Chr == '>') {
Chr = nextChar();
temp.type = NotEq;
} else temp.type = Lss;
break;
case Gtr:
if ((Chr = nextChar()) == '=') {
Chr = nextChar();
temp.type = GtrEq;
} else temp.type = Gtr;
break;
default:
temp.type = cc;
Chr = nextChar();
break;
}
CurToken = temp;
// cout << "Token : " << keyname[temp.type] << endl;
return temp;
}
Token checkAndgetToken(Token t, TokType k)
{
if (t.type == k) {
// cout << "expected " << keyname[t.type] << " and check OK!!" << endl;
return nextToken();
} else {
// cout << "expected " << keyname[t.type] << " but got " << keyname[k] << endl;
return t;
}
}