-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscan.cpp
275 lines (263 loc) · 5.58 KB
/
scan.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/****************************************************/
/* File: scan.c */
/* The scanner implementation for the CMINUS compiler*/
/* Compiler Construction: Principles and Practice */
/* Kenneth C. Louden */
/****************************************************/
#include "globals.h"
#include "util.h"
#include "scan.h"
/* states in scanner DFA */
typedef enum
{
START, INRCOM, INLCOM, INCOMMENT, INNUM, INID, DONE, INNEQ, WAITEQ, WAITBACK
}
StateType;
/* lexeme of identifier or reserved word */
char tokenString[MAXTOKENLEN + 1];
/* BUFLEN = length of the input buffer for
source code lines */
#define BUFLEN 256
static char lineBuf[BUFLEN]; /* holds the current line */
static int linepos = 0; /* current position in LineBuf */
static int bufsize = 0; /* current size of buffer string */
static int EOF_flag = FALSE; /* corrects ungetNextChar behavior on EOF */
/* getNextChar fetches the next non-blank character
from lineBuf, reading in a new line if lineBuf is
exhausted */
static int getNextChar(void) {
if (!(linepos < bufsize)) {
lineno++;
if (fgets(lineBuf, BUFLEN - 1, source)) {
if (EchoSource) fprintf(listing, "%4d: %s", lineno, lineBuf);
bufsize = strlen(lineBuf);
linepos = 0;
return lineBuf[linepos++];
}
else {
EOF_flag = TRUE;
return EOF;
}
}
else return lineBuf[linepos++];
}
/* ungetNextChar backtracks one character
in lineBuf */
static void ungetNextChar(void) {
if (!EOF_flag) linepos--;
}
/* lookup table of reserved words */
static struct {
char* str;
TokenType tok;
} reservedWords[MAXRESERVED] = {
{(char*)"if",IF},{(char*)"int",INT},{(char*)"else",ELSE},{(char*)"return",RETURN},
{(char*)"void",VOID},{(char*)"while",WHILE}
};
/* lookup an identifier to see if it is a reserved word */
/* uses linear search */
static TokenType reservedLookup(char* s) {
int i;
for (i = 0; i < MAXRESERVED; i++)
if (!strcmp(s, reservedWords[i].str))
return reservedWords[i].tok;
return ID;
}
/****************************************/
/* the primary function of the scanner */
/****************************************/
/* function getToken returns the
* next token in source file
*/
TokenType getToken() {
int tokenStringpos = 0;
TokenType currentToken;
StateType state = START;
bool save;
int waiteqbuffer = 0;
while (state != DONE) {
int c = getNextChar();
save = true;
switch (state) {
case START:
if (isdigit(c)) {
state = INNUM;
}
else if (isalpha(c)) {
state = INID;
}
else if (c == '~') {
state = INNEQ;
}
else if ((c == ' ') || (c == '\t') || (c == '\n')) {
save = false;
}
else if ((c == '>') || (c == '<') || (c == '=')) {
state = WAITEQ;
waiteqbuffer = c;
}
else if (c == '/') {
state = INLCOM;
}
else {
state = DONE;
switch (c) {
case EOF:
save = false;
currentToken = ENDFILE;
break;
case '+':
currentToken = PLUS;
break;
case',':
currentToken = COMMA;
break;
case'[':
currentToken = LBRK;
break;
case']':
currentToken = RBRK;
break;
case'{':
currentToken = LBRACE;
break;
case'}':
currentToken = RBRACE;
break;
case'-':
currentToken = MINUS;
break;
case'*':
currentToken = TIMES;
break;
case'(':
currentToken = LPAREN;
break;
case')':
currentToken = RPAREN;
break;
case';':
currentToken = SEMI;
break;
default:
currentToken = ERROR;
break;
}
}
break;
case INLCOM:
save = false;
if (c == '*') {
tokenStringpos--;
tokenString[tokenStringpos] = 0;
state = INCOMMENT;
}
else {
state = DONE;
ungetNextChar();
currentToken = OVER;
}
break;
case INCOMMENT:
save = false;
if (c == EOF) {
currentToken = ENDFILE;
}
else if (c == '*') {
state = WAITBACK;
}
break;
case WAITBACK:
save = false;
if (c == '*')
state = WAITBACK;
else if (c == '/') {
state = START;
}
else {
state = INCOMMENT;
}
break;
case WAITEQ:
state = DONE;
if (c == '=') {
if (waiteqbuffer == '<') {
currentToken = LTE;
}
else if (waiteqbuffer == '>') {
currentToken = GTE;
}
else if (waiteqbuffer == '=') {
currentToken = EQ;
}
else {
currentToken = ERROR;
}
}
else {
save = false;
ungetNextChar();
if (waiteqbuffer == '<') {
currentToken = LT;
}
else if (waiteqbuffer == '>') {
currentToken = GT;
}
else if (waiteqbuffer == '=') {
currentToken = ASSIGN;
}
else {
currentToken = ERROR;
}
}
break;
case INNEQ:
state = DONE;
if (c == '=') {
state = DONE;
currentToken = NEQ;
}
else {
ungetNextChar();
save = false;
currentToken = ERROR;
}
break;
case INNUM:
if (!isdigit(c)) {
state = DONE;
ungetNextChar();
save = false;
currentToken = NUM;
}
break;
case INID:
if (!isalpha(c)) {
state = DONE;
ungetNextChar();
save = false;
currentToken = ID;
}
break;
case DONE:
default:
fprintf(listing, "ÀèǬ¡ 2019141460301 Scanner Bug:state=%d\n", state);
state = DONE;
currentToken = ERROR;
break;
}
if (save && tokenStringpos < MAXTOKENLEN)
tokenString[tokenStringpos++] = (char)c;
if (state == DONE) {
tokenString[tokenStringpos] = '\0';
if (currentToken == ID) {
currentToken = reservedLookup(tokenString);
}
}
}
if (TraceScan) {
fprintf(listing, "\t%d: ", lineno);
printToken(currentToken, tokenString);
}
return currentToken;
}