-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinylex.c
332 lines (284 loc) · 8.11 KB
/
tinylex.c
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
/*
@author robby manis
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "SymbolTable.h"
#include "stack.h"
#define MAX_LEN 10000
/* ******************************************************************** */
/* Global declarations variables */
/* ******************************************************************** */
int charClass; /*!< LETTER DIGIT OR UNKNOWN */
char lexeme[MAX_LEN]; /*!< Holds lexeme */
char nextChar; /*!< next character */
int lexLen; /*!< lex() */
int token; /*!< Current token see token codes */
int nextToken; /*!< Next token see token codes */
FILE *in_fp, *fopen(); /*!< input file */
/* ******************************************************************** */
/* Function declarations */
/* ******************************************************************** */
void addChar();
void getChar();
void getNonBlank();
int lex();
/* ******************************************************************** */
/* Character classes */
/* ******************************************************************** */
#define LETTER 0
#define DIGIT 1
#define UNKNOWN 99
/* ******************************************************************** */
/* Token codes */
/* ******************************************************************** */
#define INT_LIT 10
#define IDENT 11
#define ASSIGN_OP 20
#define ADD_OP 21
#define SUB_OP 22
#define MULT_OP 23
#define DIV_OP 24
#define LEFT_PAREN 25
#define RIGHT_PAREN 26
#define LEFT_BRACE 27
#define RIGHT_BRACE 28
#define COMMENT 29
#define PERIOD 30
#define QUOTE 31
#define L_CARROT 32
#define R_CARROT 33
#define AND 34
#define EOL 35
#define SPACE 36
/* ***************************************************************************** */
/* lookup - a function to lookup operators and parentheses and return the token */
/* ***************************************************************************** */
int lookup(char ch){
switch(ch){
case '\n':
addChar();
nextToken = EOL;
break;
case '&':
addChar();
nextToken = AND;
break;
case '<':
addChar();
nextToken = L_CARROT;
break;
case '>':
addChar();
nextToken = R_CARROT;
break;
case '"':
addChar();
nextToken = QUOTE;
break;
case '(':
addChar();
nextToken = LEFT_PAREN;
break;
case ')':
addChar();
nextToken = RIGHT_PAREN;
break;
case '{':
addChar();
nextToken = LEFT_BRACE;
break;
case '}':
addChar();
nextToken = RIGHT_BRACE;
break;
case '+':
addChar();
nextToken = ADD_OP;
break;
case '-':
addChar();
nextToken = SUB_OP;
break;
case '*':
addChar();
nextToken = MULT_OP;
break;
case '/':
addChar();
nextToken = DIV_OP;
break;
case '=':
addChar();
nextToken = ASSIGN_OP;
break;
case '#':
//addChar();
strcpy(lexeme, "#");
while(nextChar !='\n') {
getChar();
}
nextToken = COMMENT;
break;
case '.':
addChar();
nextToken = PERIOD;
break;
case ' ':
addChar();
nextToken = SPACE;
break;
default:
addChar();
nextToken = EOF;
break;
}
return nextToken;
}
/* ***************************************************************************** */
/* addChar - a function to add next char to lexeme */
/* ***************************************************************************** */
void addChar(){
if (lexLen <= MAX_LEN-2){
lexeme[lexLen++] = nextChar;
lexeme[lexLen] = 0;
}
else {
printf("Error - lexeme too long \n");
}
}
/* ***************************************************************************** */
/* getChar - gets the next char of input and determine its character class */
/* ***************************************************************************** */
void getChar(){
if ((nextChar = getc(in_fp)) != EOF){
if (isalpha(nextChar))
charClass = LETTER;
else if (isdigit(nextChar))
charClass = DIGIT;
else if (nextChar=='.')
charClass==PERIOD;
else charClass = UNKNOWN;
}
else
charClass = EOF;
}
/* ***************************************************************************** */
/* getNonBlank - class getChar until it returns a non-whitespace character */
/* ***************************************************************************** */
void getNonBlank(){
while (isspace(nextChar)) {
if(nextChar=='\n')
break;
getChar();
}
}
/* ******************************************************************** */
/* Parser Method */
/* ******************************************************************** */
void parse(char nextToken){
if(nextToken==IDENT){
insert(lexeme,'f',top());
printTable();
}
if(nextToken==INT_LIT){
printf("\t%f was pushed on stack\n",strtof(lexeme,NULL));
float val = strtof(lexeme,NULL);
push(val);
//printTable();
}
if(nextToken==ADD_OP){
eval('+');
}
}
/* ***************************************************************************** */
/* lex - a simple lexical analyzer for arithmetic expressions */
/* ***************************************************************************** */
int lex(){
lexLen = 0;
getNonBlank();
switch (charClass){
case LETTER:
/* parse identifiers */
addChar();
getChar();
while (charClass == LETTER || charClass == DIGIT){
addChar();
getChar();
}
//insert(lexeme,'f',0);
nextToken = IDENT;
break;
case DIGIT:
/* parse integer literals */
addChar();
getChar();
while (charClass == DIGIT){
addChar();
getChar();
if (charClass==PERIOD){
addChar();
getChar();
}
}
nextToken = INT_LIT;
break;
case UNKNOWN:
/* parenthese and operators */
lookup(nextChar);
getChar();
break;
case EOF:
/* EOF */
nextToken = EOF;
lexeme[0] = 'E';
lexeme[1] = 'O';
lexeme[2] = 'F';
lexeme[3] = 0;
break;
} /* end of switch */
printf("Next token is: %d, next lexeme is %s\n", nextToken, lexeme);
// if(nextToken==IDENT){
// insert(lexeme,'f',top());
// printTable();
// }
// if(nextToken==INT_LIT){
// printf("\t%f was pushed on stack\n",strtof(lexeme,NULL));
// float val = strtof(lexeme,NULL);
// push(val);
// //printTable();
// }
// if(nextToken==ADD_OP){
// eval('+');
// }
parse(nextToken);
return nextToken;
} /* end lex */
/* ******************************************************************** */
/* main driver */
/* ******************************************************************** */
main(int argc, char **argv) {
/* open the input data file and process contents */
if (argc != 2) {
printf("Usage: front <filename> \n");
exit(-1);
}
if ((in_fp = fopen(argv[1],"r")) == NULL)
printf("ERROR - cannot open %s in \n", argv[1]);
else {
initStack();
getChar();
do {
lex();
} while (nextToken != EOF);
//push(4);
//eval('+');
printf("\n%f",top());
freeStack();
//insert("C",'n',13);
//insert("B",'f',1.2);
printTable();
}
}