-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
330 lines (295 loc) · 6.38 KB
/
parser.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
#include "parser.h"
#include <unistd.h>
#define MAX_CONTENT_LENGTH 10000
#define MAX_LINE_LENGTH 1000
#define TOKEN_MAX 100
int CheckFileExistence(char* filename){
return access(filename, F_OK) != -1;
}
int readFile(const char *filename, char contents[MAX_CONTENT_LENGTH])
{
if(CheckFileExistence(filename)){
FILE *ifp;
char *mode = "r";
char line[MAX_LINE_LENGTH];
ifp = fopen(filename, mode);
while (fscanf(ifp, "%s", line) != EOF)
{
strcat(contents,line);
}
fclose(ifp);
return 0;
}
else{
printf("File doesn't exist!\n");
return 1;
}
}
bool isAssignment(char currentToken[TOKEN_MAX])
{
if (strstr(currentToken, "=") != NULL && strstr(currentToken, ";") != NULL)
{
return true;
}
return false;
}
Ptype isType(char currentToken[TOKEN_MAX])
{
if(!strcmp(currentToken, "int"))
{
return INT;
}
if(!strcmp(currentToken, "double"))
{
return DOUBLE;
}
if(!strcmp(currentToken, "float"))
{
return FLOAT;
}
if(!strcmp(currentToken, "char"))
{
return CHAR;
}
if(!strcmp(currentToken, "long"))
{
return LONG;
}
return NONE;
}
PblockType isBlock(char currentToken[TOKEN_MAX])
{
if(!strcmp(currentToken, "if"))
{
return IF;
}
if(!strcmp(currentToken, "for"))
{
return FOR;
}
if(!strcmp(currentToken, "while"))
{
return WHILE;
}
if(!strcmp(currentToken, "switch"))
{
return SWITCH;
}
return BLOCKNONE;
}
void resetString(char *str)
{
memset(str,'\0',sizeof(str));
}
Pfunction ParseFile(const char *filename, Pfunction *function,int *result)
{
bool outsideFunction = true;
char contents[MAX_CONTENT_LENGTH];
if(readFile(filename, contents) == 0)
{
List functions;
ListInit(&functions, sizeof(Pfunction));
char currentToken[TOKEN_MAX];
resetString(currentToken);
int i;
Ptype type;
Pblock *block = NULL;
Pvariable variable;
Paction action;
for (i = 0; i < strlen(contents); ++i)
{
int ch = contents[i];
strcat(currentToken, &ch);
type = isType(currentToken);
int b;
if(outsideFunction)
{
if(type != NONE)
{
function->type = type;
resetString(function->name);
for (b = 1; contents[i+b]!='('; ++b)
{
int ch = contents[i+b];
strcat(function->name, &ch);
}
if(!strcmp(function->name, "main"))
{
function->isMain = true;
}
i += b;
ListInit(&function->arguments, sizeof(Pvariable));
ListInit(&function->variables, sizeof(Pvariable));
ListInit(&function->actions, sizeof(Paction));
ListInit(&function->assignments, sizeof(char[50]));
ListInit(&function->blocks, sizeof(Pblock));
resetString(currentToken);
while(contents[i] != ')')
{
ch = contents[i];
strcat(currentToken, &ch);
type = isType(currentToken);
if(type != NONE)
{
variable.type = type;
for (b = 1; contents[i+b]!=')' && contents[i+b]!=','; ++b)
{
int ch = contents[i+b];
strcat(variable.name, &ch);
}
i+=b;
ListAdd(&function->arguments, &variable);
resetString(variable.name);
resetString(currentToken);
}
i++;
}
resetString(currentToken);
i+=1;
outsideFunction = false;
}
}
else
{
PblockType blockType = isBlock(currentToken);
if(type != NONE)
{
variable.type = type;
resetString(variable.name);
for(b = 1; contents[i+b] != ';' && contents[i+b] != '=';b++)
{
int ch = contents[i+b];
strcat(variable.name,&ch);
}
i+=b;
ListAdd(&function->variables,&variable);
resetString(currentToken);
action = INITVAR;
ListAdd(&function->actions, &action);
if(contents[i]=='=')
{
char assignment[50];
resetString(assignment);
strcat(assignment, variable.name);
strcat(assignment, "=");
i++;
while(contents[i]!=';')
{
ch = contents[i];
strcat(assignment, &ch);
i++;
}
ListAdd(&function->assignments,&assignment);
action = ASSIGNMENT;
ListAdd(&function->actions, &action);
}
}
else if(blockType != BLOCKNONE)
{
i+=2;
resetString(currentToken);
block = (Pblock*)malloc(sizeof(Pblock));
ListInit(&block->variables, sizeof(Pvariable));
ListInit(&block->actions, sizeof(Paction));
ListInit(&block->assignments, sizeof(char[50]));
ListInit(&block->blocks, sizeof(Pblock));
block->blockType = blockType;
switch(blockType)
{
case IF:
while(contents[i] != ')')
{
int ch = contents[i];
strcat(block->condition,&ch);
i++;
}
action = BLOCK;
ListAdd(&function->actions, &action);
break;
}
i+=1;
}
else if(!strcmp(currentToken,"}"))
{
if(block != NULL)
{
ListAdd(&function->blocks, block); // <--------------- SHIBAN AMPERSANT
action = BLOCKEND;
ListAdd(&function->actions, &action);
resetString(block->condition);
resetString(currentToken);
block = NULL;
}
else
{
return;
}
}
else if(isAssignment(currentToken))
{
currentToken[strlen(currentToken) - 1] = '\0';
if(block != NULL)
{
ListAdd(&block->assignments, ¤tToken);
action = ASSIGNMENT;
ListAdd(&block->actions, &action);
}
else
{
ListAdd(&function->assignments, ¤tToken);
action = ASSIGNMENT;
ListAdd(&function->actions, &action);
}
resetString(currentToken);
}
}
// printf("%s\n", currentToken);
// if(block != NULL)
// {
// }
}
}
// int mainPos = findMain(contents) + 7;
}
// Ptype checkStartOfType(char ch)
// {
// switch(ch)
// {
// case 'i':
// return INT;
// case 'c':
// return CHAR;
// case 'f':
// return FLOAT;
// case 'd':
// return DOUBLE;
// case 'l':
// return LONG;
// }
// }
// int findMain(char contents[MAX_CONTENT_LENGTH])
// {
// int pos;
// char *mainPtr;
// mainPtr = strstr(contents,"main");
// if(mainPtr!=NULL)
// {
// pos = mainPtr - contents;
// }
// return pos;
// }
// int
// Ptype expected = checkStartOfType(contents[i]);
// switch(expected)
// {
// case INT:
// if(contents[i+1] == 'n' && contents[i+2] == 't')
// case CHAR:
// return CHAR;
// case FLOAT:
// return FLOAT;
// case DOUBLE:
// return DOUBLE;
// case LONG:
// return LONG;
// }