-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexical.cpp
350 lines (337 loc) · 9.17 KB
/
lexical.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "lexical.h"
#include "external.h"
int w, n, p, e, d;
int Class;
int ICON;
double FCON;
int ForI = 1000; // Float or Integer flag
static int CurrentState;
char lc1;
char lc2;
char nextcha;
int flag=5000; // Positive or Negative flag
char *stri;
int linec=1;
int wf = 1000;
char *KeyWordTable[MAX_KEY_NUMBER]={ "if", "then", "else", "while", "begin", "end" , "int" , "for", "do" , KEY_WORD_END};
void word(FILE *fp,FILE *fw)
{
memset(TOKEN, 0, sizeof(TOKEN));
//char con[20];
wf=0;
char fir;
while ((fir = fgetc(fp)) != ';') {
if(fir==10) {linec++; continue;}
if (fir == ' '|| fir == '\r'|| fir == '\t'|| fir == '\n') continue; else if(fir!='#') fseek(fp,-1,1);
if(fir=='#') // delete comment
{
while(1)
{
fir = fgetc(fp);
if(fir=='#')
break;
}
continue;
}
scanner_example(fp,fw);
break;
}//while
if(fir==';') as=7;
if(wf==1) word(fp,fw);// skip error
}
void scanner_example(FILE *fp,FILE *fw)
{
char ch; int i, c;
ch = fgetc(fp);
if (ch>='a'&&ch<='z') /*it must be an identifer!*/
{
TOKEN[0] = ch; ch = fgetc(fp); i = 1;
while ((ch >= 'a'&&ch <= 'z')|| (ch >= '0'&&ch <= '9'))
{
TOKEN[i] = ch; i++;
ch = fgetc(fp);
}
TOKEN[i] = '\0';
c = lookup(TOKEN);
if (c == 0)
{
out(ID, TOKEN,fw);
strcpy(valuesta[idxofv],TOKEN);idxofv++;
}else out(c, " ",fw);
if(ch!=EOF) fseek(fp, -1, 1); /* retract*/
}
else
if(ch == '0') Octa(fp,fw);
else if ((ch >= '1'&&ch <= '9')||ch == '.')
{
ICON=0;
FCON=0;
n = 0; p = 0; e = 0; w = 0;
LEX(fp,fw,ch);
}
else
switch (ch)
{
case '<': ch = fgetc(fp);
if (ch == '=') out(LE, " ",fw);
else if (ch == '>') out(NE, " ",fw);
else
{
if(ch!=EOF) fseek(fp, -1, 1);
out(LT, " ",fw);
}
break;
case ':': fseek(fp, 1, 1); out(IS, " ",fw); break;
case '=': out(EQ, " ",fw); break;
case '+': out(PL, " ",fw); break;
case '*': out(MU, " ",fw); break;
case '/': out(DI, " ",fw); break;
case '&': out(AN, " ",fw); break;
case '|': out(OR, " ",fw); break;
case '(': out(LFBK, " ",fw); break;
case ')': out(RTBK, " ",fw); break;
case '>': ch = fgetc(fp);
if (ch == '=') out(GE, " ",fw);
else
{
if(ch!=EOF) fseek(fp, -1, 1);
out(GT, " ",fw);
}
break; // determine whether it is a minus sign or a negative sign
case '-':fseek(fp,-3,1); lc1=fgetc(fp); lc2=fgetc(fp);if(lc2=='='||lc2=='>'||lc2=='<'||lc2=='+'||lc2=='-'||lc2=='*'||lc2=='/'){flag=1; fseek(fp,1,1);ch=fgetc(fp);LEX(fp,fw,ch);} else {fseek(fp,-1,1);out(MI, " ",fw); stri="(MI, )\n"; fputs(stri,fw);} break;
case';':break;
default: report_error(); break;
}
return;
}
int GetChar(int j)
{
int c;
c = TOKEN[j];
if (c >= '0'&&c <= '9') { d = c - '0'; return DIGIT;}
if (c == '.') return POINT;
if (c == 'E' || c == 'e') return POWER;
if (c == '+') return PLUS;
if (c == '-') return MINUS;
return OTHER;
}
int EXECUTE(int state, int symbol,FILE* fw)
{
switch (state)
{
case 0: switch (symbol)
{
case DIGIT:n = 0; p = 0; e = 1; w = d; CurrentState = 1; Class = ClassNo; break;
case POINT:w = 0; n = 0; p = 0; e = 1; CurrentState = 3; Class = ClassNo; break;
default: HandleOtherWord(); Class = ClassOther;
CurrentState = EndState;
}
break;
case 1: switch (symbol)
{
case DIGIT:w = w * 10 + d; break;
case POINT:CurrentState = 2; break;
case POWER:CurrentState = 4; break;
default: ICON = w; CurrentState = EndState; if(flag==1) {ICON*=-1; flag=5000;} printf("(INT,%d)\n", ICON); as=6; itoa(ICON,valuesta[idxofv],10); idxofv++; fprintf(fw,"(INT,%d)\n",ICON);
}
break;
case 2: switch (symbol)
{
case DIGIT:n++; w = w * 10 + d; break;
case POWER:CurrentState = 4; break;
default: FCON = w * pow(10, e*p - n); CurrentState = EndState; if(flag==1) {FCON*=-1; flag=5000;} printf("(REAL,%g)\n", FCON); as=6; gcvt(FCON,5,valuesta[idxofv]); idxofv++; fprintf(fw,"(REAL,%g)\n",FCON);
}
break;
case 3:switch (symbol)
{
case DIGIT:n++; w = w * 10 + d; CurrentState = 2; break;
default: HandleError(); CurrentState = EndState;
}
break;
case 4:switch (symbol)
{
case DIGIT:p = p * 10 + d; CurrentState = 6; break;
case MINUS:e = -1; CurrentState = 5; break;
case PLUS:CurrentState = 5; break;
default:HandleError(); CurrentState = EndState;
}
break;
case 5:switch (symbol)
{
case DIGIT:p = p * 10 + d; CurrentState = 6; break;
default:HandleError(); CurrentState = EndState;
}
break;
case 6:switch (symbol)
{
case DIGIT:p = p * 10 + d; break;
default:FCON = w * pow(10, e*p - n); CurrentState = EndState; if(flag==1) {FCON*=-1; flag=5000;} printf("(REAL,%g)\n", FCON); as=6; gcvt(FCON,5,valuesta[idxofv]); idxofv++; fprintf(fw,"(REAL,%g)\n",FCON);
}
break;
}
return CurrentState;
}
int LEX(FILE *fp,FILE *fw,char cha)
{
for(int y=0;y<MAX;y++)
{
TOKEN[y]='\0';
}
int i = 0;
for (int j = 0;; j++) {
if ((cha <= '9'&&cha >= '0') || cha=='E'|| cha=='-'|| cha=='.')
{
TOKEN[i] = cha;
i++;
cha = fgetc(fp);
}
else break;
}
int ch;
CurrentState = 0;
j = 0;
while (CurrentState != EndState)
{
ch = GetChar(j);
EXECUTE(CurrentState, ch,fw);
j++;
}
if(cha!=EOF)
fseek(fp, -1, 1);
return Class;
}
int lookup(char *token)
{
int n = 0;
while (strcmp(KeyWordTable[n], KEY_WORD_END))
{
if (!strcmp(KeyWordTable[n], token))
{
return n + 1;
break;
}
n++;
}
return 0;
}
void wordforids(FILE *fp,FILE *fw)
{
char fir;
while (1) {
fir = fgetc(fp);
if (fir == ' '|| fir == '\r'|| fir == '\t'|| fir == '\n'||fir == 10) continue; else if(fir!='#') {fseek(fp,-1,1);break;}
if(fir=='#')
{
while(1)
{
fir = fgetc(fp);
if(fir=='#')
break;
}
continue;
}
break;
}//while
char ch; int i;
ch = fgetc(fp);
if (ch>='a'&&ch<='z') /*it must be a identifer!*/
{
TOKEN[0] = ch; ch = fgetc(fp); i = 1;
while ((ch >= 'a'&&ch <= 'z')|| (ch >= '0'&&ch <= '9'))
{
TOKEN[i] = ch; i++;
ch = fgetc(fp);
}
TOKEN[i] = '\0';
if(ch!=EOF) fseek(fp, -1, 1); /* retract*/
}
fprintf(fw,"(ID,%s)\n",TOKEN);
while (1) {
fir = fgetc(fp);
if (fir == ' '|| fir == '\r'|| fir == '\t'|| fir == '\n'||fir == 10) continue; else {fseek(fp,-1,1); break;}
if(fir=='#')
{
while(1)
{
fir = fgetc(fp);
if(fir=='#')
break;
}
continue;
}
break;
}
}
void out(int a,char*wordin,FILE *fw)
{
switch(a)
{
case ID:printf("(ID,%s)\n",wordin); as=6; fprintf(fw,"(ID,%s)\n",wordin);break;
case LT:printf("(LT,%s)\n",wordin); fprintf(fw,"(LT,%s)\n",wordin);break;
case LE:printf("(LE,%s)\n",wordin); fprintf(fw,"(LE,%s)\n",wordin);break;
case EQ:printf("(EQ,%s)\n",wordin); fprintf(fw,"(EQ,%s)\n",wordin);break;
case NE:printf("(NE,%s)\n",wordin); fprintf(fw,"(NE,%s)\n",wordin);break;
case GT:printf("(GT,%s)\n",wordin); fprintf(fw,"(GT,%s)\n",wordin);break;
case GE:printf("(GE,%s)\n",wordin); fprintf(fw,"(GE,%s)\n",wordin);break;
case IS:printf("(IS,%s)\n",wordin); fprintf(fw,"(IS,%s)\n",wordin);break;
case PL:printf("(PL,%s)\n",wordin); as=2; fprintf(fw,"(PL,%s)\n",wordin); break;
case MI:printf("(MI,%s)\n",wordin); as=3; fprintf(fw,"(MI,%s)\n",wordin); break;
case MU:printf("(MU,%s)\n",wordin); as=4; fprintf(fw,"(MU,%s)\n",wordin); break;
case AN:printf("(AN,%s)\n",wordin); fprintf(fw,"(AN,%s)\n",wordin);break;
case OR:printf("(OR,%s)\n",wordin); fprintf(fw,"(OR,%s)\n",wordin);break;
case DI:printf("(DI,%s)\n",wordin); as=5; fprintf(fw,"(DI,%s)\n",wordin);break;
case IF:printf("(IF,%s)\n",wordin); fprintf(fw,"(IF,%s)\n",wordin);break;
case THEN:printf("(THEN,%s)\n",wordin); fprintf(fw,"(THEN,%s)\n",wordin);break;
case ELSE:printf("(ELSE,%s)\n",wordin); fprintf(fw,"(ELSE,%s)\n",wordin);break;
case WHILE:printf("(WHILE,%s)\n",wordin); fprintf(fw,"(WHILE,%s)\n",wordin);break;
case BEGIN:printf("(BEGIN,%s)\n",wordin); fprintf(fw,"(BEGIN,%s)\n",wordin);break;
case END:printf("(END,%s)\n",wordin); fprintf(fw,"(END,%s)\n",wordin);break;
case INT:printf("(INT,%s)\n",wordin); fprintf(fw,"(INT,%s)\n",wordin);break;
case FOR:printf("(FOR,%s)\n",wordin); fprintf(fw,"(FOR,%s)\n",wordin);break;
case DO:printf("(DO,%s)\n",wordin); fprintf(fw,"(DO,%s)\n",wordin);break;
case LFBK:printf("(LFBK,%s)\n",wordin); as=0; fprintf(fw,"(LFBK,%s)\n",wordin); break;
case RTBK:printf("(RTBK,%s)\n",wordin); as=1; fprintf(fw,"(RTBK,%s)\n",wordin); break;
}
}
void Octa(FILE *fp,FILE *fw)
{
int j=0;
int i=0;
int sum=0;
char cha = fgetc(fp);
char wordin[20];
for(j=0;;j++){
if(cha <= '7' && cha >= '0'){
wordin[i] = cha-'0';
cha = fgetc(fp);
i++;
}
else break;
}
i--;
int c=i;
j=0;
for(;i>=0;i--){
wordin[i]*=pow(8,j);
j++;
}
for(i=0;i<=c;i++){
sum+=wordin[i];
}
fprintf(fw,"(OCTAL,%d)\n",sum);
printf("(OCTAL,%d)\n",sum);
fseek(fp, -1, 1);
}
void report_error()
{
printf("Error at line %d! \n",linec);
}
int HandleOtherWord(void)
{
return ClassOther;
}
int HandleError(void)
{
printf("Error at line %d! \n",linec);
return 0;
}