/
scanner.c
executable file
·325 lines (288 loc) · 9.25 KB
/
scanner.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
//
// scanner.c
// Lab3
//
// Created by Bryce Holton.
// Copyright (c) 2014 Bryce Holton. All rights reserved.
//
#include <stdio.h>
#include "scanner.h"
#include <ctype.h>
/*******************
Static functions needed for the scanner
You need to design the proper parameter list and
return types for functions with ???.
******************/
void skip_comment(char[]);
void skip_blanks(char[]);
static struct Token* get_word(char[], struct Token*);
static struct Token* get_number(char[], struct Token*);
static struct Token* get_string(char[], struct Token*);
static struct Token* get_special(char[], struct Token*);
static void downshift_word(char[]);
static BOOLEAN is_reserved_word(char[]);
typedef enum
{
LETTER, DIGIT, QUOTE, SPECIAL, EOF_CODE,
}
CharCode;
typedef struct
{
char *string;
TokenCode token_code;
}
RwStruct;
const RwStruct rw_table[9][10] = {
{{"do",DO},{"if",IF},{"in",IN},{"of",OF},{"or",OR},{"to",TO},{NULL,0}}, //Reserved words of size 2
{{"and",AND},{"div",DIV},{"end",END},{"for",FOR},{"mod",MOD},{"nil",NIL},{"not",NOT},{"set",SET},{"var",VAR},{NULL,0}}, //Reserved words of size 3
{{"case",CASE},{"else",ELSE},{"file",FFILE},{"goto",GOTO},{"then",THEN},{"type",TYPE},{"with",WITH},{NULL,0}}, //Reserved words of size 4
{{"array",ARRAY},{"begin",BEGIN},{"const",CONST},{"label",LABEL},{"until",UNTIL},{"while",WHILE},{NULL,0}}, //Reserved words of size 5
{{"downto",DOWNTO}, {"packed",PACKED},{"record",RECORD}, {"repeat",REPEAT},{NULL,0}}, // Reserved words of size 6
{{"program", PROGRAM},{NULL,0}}, // Reserved words of size 7
{{"function", FUNCTION},{NULL,0}}, // Reserved words of size 8
{{"procedure", PROCEDURE},{NULL,0}} // Reserved words of size 9
};
Token* get_token(char *stringtotoken)
{
char source_buffer[MAX_TOKEN_STRING_LENGTH];
char ch = source_buffer[0]; //This can be the current character you are examining during scanning.
int chint = (int)ch;
char token_string[MAX_TOKEN_STRING_LENGTH]; //Store your token here as you build it.
//if(sourceline[0]=='\n');
//{get_source_line(source_buffer);}
//struct stack * newptr=(struct stack*)malloc(sizeof(struct stack));
struct Token * token1 = (struct Token*)malloc(sizeof(Token));
//???; //I am missing the most important variable in the function, what is it? Hint: what should I return?
//2. figure out which case you are dealing with LETTER, DIGIT, QUOTE, EOF, or special, by examining ch
if (isdigit(chint))
{
if (isalpha(chint))
{
token1 =get_word(source_buffer,token1);
}
else
{
if (chint == 39)
token1 = get_string(source_buffer,token1);
}
token1 = get_number(source_buffer,token1);
}
else
token1 = get_special(source_buffer,token1);
//3. Call the appropriate function to deal with the cases in 2.
strcpy(sourceLine,source_buffer);
return token1; //What should be returned here?
}
void skip_blanks(char stringwithspaces[])
{
char *str=stringwithspaces;
int i;
for (i=0;stringwithspaces[i]==' ';i++)
{}
str=str+i;
strcpy(stringwithspaces,str);
}
void skip_comment(char stringwithcomment[])
{
/*
Write some code to skip past the comments in the program and return a pointer
to the first non blank character. Watch out for the EOF character.
*/
/*
char stringwithoutcomment[];
for(i = 0; i < strlen(stringwithcomment) - 2; i++)
{
if(stringwithcomment[i] == '/' && stringwithcomment[i+1] == '/')
stringwithoutcomment[i] = '\0';
else
stringwithoutcomment[i] = stringwithcomment[i];
}
return stringwithoutcomment;
*/
//Sorry this is wrong as comments are cretaed in pascel with '{' and end with '}'
if(stringwithcomment[0] == '{')
{
int i;
for(i = 0; stringwithcomment[i]!='}' || stringwithcomment[i]!='\n'; i++)
{}
strcpy(stringwithcomment,&stringwithcomment[i]);
}
}
static struct Token* get_word(char stringwithuppercase[], struct Token *addtoken)
{
/*
Write some code to Extract the word
*/
//Downshift the word, to make it lower case
/*
Write some code to Check if the word is a reserved word.
if it is not a reserved word its an identifier.
*/ int settokencode;
LiteralType setliteraltype = STRING_LIT;
int i,j;
char stringwithoutuppercase[80];
strcpy(stringwithoutuppercase, stringwithuppercase);
downshift_word(stringwithoutuppercase);
// int i,j;
for (i = 0; i < 9; i++)
{
for(j = 0; j < 1-0; j++)
{
if(strcmp(stringwithoutuppercase,rw_table[i][j].string) == 0)
settokencode = rw_table[i][j].token_code;
}
}
addtoken->tokenCode = settokencode;
addtoken->typeOfLiteral = setliteraltype;
strcpy((addtoken->LiteralValue),stringwithoutuppercase);
return addtoken;
}
static struct Token* get_number(char stringnum[], struct Token *addtoken)
{
//char WordNumber[80];
//char *saveptr;
//strcpy(WordNumber,strtok_r(stringnum,' ',saveptr));
/*
Write some code to Extract the number and convert it to a literal number.
*/
int numlength = strlen(stringnum);
char currentchar;
int i;
TokenCode settokencode = NUMBER;
LiteralType setliteraltype = INTEGER_LIT; // initialize LiteralType
addtoken->tokenCode = settokencode; // with default as integer
strcpy(addtoken->LiteralValue, stringnum);
for(i=0; i<numlength; i++)
{
currentchar = stringnum[i];
if(currentchar == 'e' || currentchar == '.') // check for real number
{
LiteralType setliteraltype = REAL_LIT;
i = i + numlength; // exits while loop
}
}
addtoken->typeOfLiteral = setliteraltype;
return addtoken;
}
static struct Token* get_string(char stringwithquotes[],struct Token *addstring)
{
char stringwithoutquotes[80];
int i;
/*
Write some code to Extract the string
*/
LiteralType setliteraltype = STRING_LIT;
TokenCode settokencode = STRING;
addstring->typeOfLiteral = setliteraltype;
addstring->tokenCode = settokencode;
//int strl = strlen(stringwithquotes)-2;
for(i = 0; i < strlen(stringwithoutquotes); i++)
{
stringwithoutquotes[i] = stringwithoutquotes[i+1];
}
stringwithoutquotes[i+1]='\0';
strcpy(addstring->LiteralValue,stringwithoutquotes);
return addstring;
}
static struct Token* get_special(char stringspecial[], struct Token *addtoken)
{
/*
Write some code to Extract the special token. Most are single-character
some are double-character. Set the token appropriately.
*/
int chint = stringspecial[0];
TokenCode settokencode;
switch(chint) // set TokenCode
{
case 94:
settokencode = UPARROW;
break;
case 42:
settokencode = STAR;
break;
case 40:
settokencode = LPAREN;
break;
case 41:
settokencode = RPAREN;
break;
case 45:
settokencode = MINUS;
break;
case 91:
settokencode = LBRACKET;
break;
case 93:
settokencode = RBRACKET;
break;
case 58:
if(stringspecial[1] == '=')
settokencode = COLONEQUAL;
else
settokencode = COLON;
break;
case 59:
settokencode = SEMICOLON;
break;
case 60:
if(stringspecial[1] == '=')
settokencode = LE;
else
settokencode = LT;
break;
case 62:
if(stringspecial[1] == '=')
settokencode = GE;
else
settokencode = GT;
break;
case 44:
settokencode = COMMA;
break;
case 46:
if(stringspecial[1] == '.')
settokencode = DOTDOT;
else
settokencode = PERIOD;
break;
case 47:
settokencode = SLASH;
break;
}
addtoken->tokenCode = settokencode;
strcpy(addtoken->LiteralValue, stringspecial);
return addtoken;
}
static void downshift_word(char stringuppercase[])
{
/*
Make all of the characters in the incoming word lower case.
*/
int i;
char stringlowercase[80];
strcpy(stringlowercase, stringuppercase);
for(i=0; i<strlen(stringlowercase); i++)
{
stringlowercase[i] = tolower(stringlowercase[i]);
}
strcpy(stringuppercase,stringlowercase);
// return stringlowercase;
}
static BOOLEAN is_reserved_word(char stringsearchword[])
{
/*
Examine the reserved word table and determine if the function input is a reserved word.
*/
int i,j;
//char * strings;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 1-0; j++)
{
//strings=rw_table[i][j]->string;
if(strcmp(stringsearchword,(rw_table[i][j].string)) == 0)
return TRUE;
}
}
return FALSE;
}