-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.c
More file actions
475 lines (404 loc) · 14.7 KB
/
Copy pathparser.c
File metadata and controls
475 lines (404 loc) · 14.7 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
// Global parser error flag and line tracker
int parseErrorFlag = 0;
int line = 1;
// Checks for two consecutive INT_CONSTANTs, which may indicate a number over 100 digits
int isBiggerThan100Digit(Token* currentT, Token* nextT){
if (currentT->type == INT_CONSTANT && nextT->type == INT_CONSTANT){
parseErrorFlag = 1;
fprintf(stderr,"Line %d Error: Two INT_CONSTANT tokens found consecutively (likely a number > 100 digits)\n",nextT->line);
return 1;
}
return 0;
}
// Checks for two consecutive IDENTIFIERs, which may indicate a name longer than allowed
int isBiggerThan20Char(Token* currentT, Token* nextT){
if (currentT->type == IDENTIFIER && nextT->type == IDENTIFIER){
parseErrorFlag = 1;
fprintf(stderr, "Line %d Error: Two consecutive identifiers not allowed (possible long name)\n",nextT->line);
return 1;
}
return 0;
}
// Allocates and initializes a new Parser
Parser* createParser(Token* tokens,int count){
Parser* parser = malloc(sizeof(Parser));
parser ->tokens = tokens;
parser->count = count;
parser->pos = 0;
return parser;
}
// Frees parser memory
void freeParser(Parser* parser) {
free(parser);
}
// Returns current token without advancing
static Token* currentT(Parser* parser) {
if (parser->pos >= parser->count) return NULL;
return &parser->tokens[parser->pos];
}
// Advances parser and returns next token
static Token* nextT(Parser* parser) {
parser->pos++;
if (parser->pos >= parser->count) return NULL;
return &parser->tokens[parser->pos];
}
// Peeks at next token without advancing
static Token* peekNext(Parser* parser) {
if (parser->pos + 1 >= parser->count) return NULL;
return &parser->tokens[parser->pos + 1];
}
// Checks if current token matches expected type and value
static int isEquals(Parser* parser, TokenType expectedType, const char* expectedValue) {
Token* current = currentT(parser);
if (!current) return 0;
if (current->type == expectedType &&
(!expectedValue || strcmp(current->value, expectedValue) == 0)) {
return 1;
}
return 0;
}
// Parses a variable declaration with optional assignment.
// This parses a variable declaration in the form: number variable := value; or number variable;
AST* parseDeclaration(Parser* parser){
Token* ident = nextT(parser);
// Error if no identifier follows.
if(!ident || ident->type != IDENTIFIER ){
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error on line %d: Identifier expected after 'number'\n", ident ? ident->line : parser->tokens[parser->pos].line);
return NULL;
}
// Check for too long identifier.
if(isBiggerThan20Char(ident,peekNext(parser))){
return NULL;
}
AST* node = malloc(sizeof(AST));
if (!node) {
parseErrorFlag = 1;
fprintf(stderr, "Memory allocation failed for AST_DECLARATION\n");
return NULL;
}
node->type = AST_DECLARATION;
node->declaration.name = strdup(ident->value);
node->line = ident->line;
// Check if assignment exists.
Token* peek = peekNext(parser);
if(strcmp(peek->value, ":=") == 0){
nextT(parser);
Token* valueToken = nextT(parser);
if (!valueToken || (valueToken->type != INT_CONSTANT)&& valueToken->type != IDENTIFIER){
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error on line %d: Expected numeric value or variable after ':='\n", peek->line);
return NULL;
}
if(valueToken->type == INT_CONSTANT && isBiggerThan100Digit(valueToken,peekNext(parser))){
return NULL;
}
node->declaration.value = strdup(valueToken->value);
}
else {
node->declaration.value = strdup("0"); // No value assigned, default to "0"
}
// Expect ';' to end the statement
Token* eol = nextT(parser);
if(!eol || eol->type != ENDOFLINE){
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error on line %d: Missing ';' after declaration of '%s'\n", ident->line, ident->value);
return NULL;
}
nextT(parser);
return node;
}
// Parses assignment, increment, or decrement statements
AST* parserStmtWop(Parser* parser){
Token* ident = currentT(parser); // Variable name
Token* op = nextT(parser); // Operator (e.g., :=, +=, -=)
ASTtype opType;
if (!op || op->type != OPERATOR ) {
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error on line %d: Operator expected after identifier '%s'\n", op ? op->line : ident->line, ident->value);
return NULL;
}
else{ // Determine operation type based on the operator
if(strcmp(op->value,":=") == 0){
opType = AST_ASSIGNMENT;
}
else if(strcmp(op->value,"-=") == 0){
opType = AST_DECREMENT;
}
else if(strcmp(op->value,"+=") == 0){
opType = AST_INCREMENT;
}
}
// Parse the value to assign/add/subtract
Token* valueToken = nextT(parser);
if (!valueToken || (valueToken->type != INT_CONSTANT && valueToken->type != IDENTIFIER)) {
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error on line %d: Expected value after ':='\n", valueToken ? valueToken->line : op->line);
return NULL;
}
if(valueToken->type == INT_CONSTANT){
if(isBiggerThan100Digit(valueToken,peekNext(parser))){
parseErrorFlag = 1;
return NULL;
}
}
// Expect ';' at the end
Token* end = nextT(parser);
if (!end || end->type != ENDOFLINE) {
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error on line %d: Missing ';' after statement\n", end ? end->line : valueToken->line);
return NULL;
}
nextT(parser);
AST* node = malloc(sizeof(AST));
node->type = opType;
node->assignment.name = strdup(ident->value);
node->line = ident->line;
AST* valueNode = malloc(sizeof(AST));
if (valueToken->type == INT_CONSTANT) {
valueNode->type = AST_LITERAL;
valueNode->number_literal.value = strdup(valueToken->value);
} else {
valueNode->type = AST_VARIABLE;
valueNode->variable.name = strdup(valueToken->value);
}
node->assignment.value = valueNode;
return node;
}
// Parses a "write" statement with optional multiple expressions and 'and' keyword
AST* parserOutput(Parser* parser){
int counter = 0;
int capacity = 2;
AST** elementList = malloc(capacity*sizeof(AST));
Token* valueToPrint;
nextT(parser);
while (1) {
if (counter >= capacity) {
capacity *= 2;
elementList = realloc(elementList, capacity * sizeof(AST));
}
valueToPrint = currentT(parser);
if (!valueToPrint) {
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error: Expected expression after 'write' on line %d\n",
parser->tokens[parser->pos].line);
return NULL;
}
AST* element = malloc(sizeof(AST));
if (!element) return NULL;
// Determine the type of value to print
if (valueToPrint->type == STRING_CONSTANT) {
element->type = AST_LITERAL;
element->string_literal.value = valueToPrint->value;
} else if (valueToPrint->type == INT_CONSTANT) {
element->type = AST_LITERAL;
element->number_literal.value = valueToPrint->value;
} else if (valueToPrint->type == IDENTIFIER) {
element->type = AST_VARIABLE;
element->variable.name = valueToPrint->value;
} else if (valueToPrint->type == KEYWORDS && strcmp(valueToPrint->value, "newline") == 0) {
element->type = AST_NEWLINE;
element->newLine.value = valueToPrint->value;
} else {
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error on line %d: Unexpected token in write statement\n", valueToPrint->line);
return NULL;
}
elementList[counter++] = element;
// Handle 'and' keyword or statement end
Token* next = peekNext(parser);
if (next) {
if (next->type == KEYWORDS && strcmp(next->value, "and") == 0) {
nextT(parser);
nextT(parser);
continue;
}
else if (next->type == ENDOFLINE) {
nextT(parser);
break;
}
else if (next->type == IDENTIFIER || next->type == STRING_CONSTANT || next->type == INT_CONSTANT) {
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error on line %d: Missing 'and' between write elements\n", next->line);
return NULL;
}
else {
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error on line %d: ';' expected after write statement\n", next->line);
return NULL;
}
}
else { // Syntax error if something unexpected appears
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error: Unexpected end of input after write statement\n");
return NULL;
}
}
nextT(parser);
AST* node = malloc(sizeof(AST));
node->type = AST_WRITE;
node->write.count = counter;
node->write.elements = elementList;
node->line = valueToPrint->line;
return node;
}
// Parses a "repeat <n> times" loop with an optional block of statements inside {}
AST* parserLoop(Parser* parser){
Token* size = nextT(parser);
AST* times;
times = malloc(sizeof(AST));
// Parse repeat count (integer or variable)
if(!size){
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error: Size expected after 'repeat' on line %d\n", parser->tokens[parser->pos].line);
free(times);
return NULL;
}
else if(size->type == INT_CONSTANT){
times->type = AST_LITERAL;
times->number_literal.value = strdup(size->value);
}
else if(size->type == IDENTIFIER){
times->type = AST_VARIABLE;
times->variable.name = strdup(size->value);
}
else{
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error: Invalid size after 'repeat' on line %d\n", size->line);
free(times);
return NULL;
}
// Expect 'times' keyword
nextT(parser);
if(!isEquals(parser,KEYWORDS,"times")){
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error: 'times' keyword expected after size on line %d\n", size->line);
free(times);
return NULL;
}
Token* nextToken = nextT(parser);
if(!nextToken){
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error: Statement expected after 'times' on line %d\n", parser->tokens[parser->pos].line);
free(times);
return NULL;
}
AST* valueNode;
// Parse loop body: either a single statement or a block {...}
if(nextToken->type == OPEN_BLOCK){
nextT(parser);
int capacity = 1;
AST** statemntList = malloc(capacity * sizeof(AST));
int counter = 0;
while(currentT(parser) && currentT(parser)->type != CLOSE_BLOCK){ // Repeatedly parse statements inside block until '}' is found
if(counter >= capacity){
capacity *= 2;
statemntList = realloc(statemntList , capacity * sizeof(AST));
}
statemntList[counter++] = parseStatement(parser);
}
if(!currentT(parser)){
parseErrorFlag = 1;
fprintf(stderr, "Syntax Error: '}' expected to close block starting at line %d\n", nextToken->line);
free(times);
free(statemntList);
return NULL;
}
valueNode = malloc(sizeof(AST));
valueNode->type = AST_CODEBLOCK;
valueNode->codeblock.count = counter;
valueNode->codeblock.statements = statemntList;
valueNode->codeblock.position = 0;
}
else{
// single statement
valueNode = parseStatement(parser);
if (!valueNode) {
free(times);
return NULL;
}
}
nextT(parser);
AST* node = malloc(sizeof(AST));
if (!node) {
parseErrorFlag = 1;
fprintf(stderr, "Memory allocation failed\n");
return NULL;
}
node->type = AST_LOOP;
node->loop.times = times;
node->loop.body = valueNode;
node->line = size->line;
return node;
}
// Dispatch separation according to certain conditions.
AST* parseStatement(Parser* parser){
Token* token = currentT(parser);
if(!token){
return NULL;
}
if(token->type == KEYWORDS &&strcmp(token->value, "number") == 0){
return parseDeclaration(parser);
}
if(token->type ==IDENTIFIER){
return parserStmtWop(parser);
}
if(token->type == KEYWORDS && strcmp(token->value,"write") == 0){
return parserOutput(parser);
}
if(token->type == KEYWORDS && strcmp(token->value,"repeat") == 0){
return parserLoop(parser);
}
parseErrorFlag = 1;
fprintf(stderr, "Error on line %d: Unexpected token '%s'\n", token->line, token->value);
return NULL;
}
// Parses the entire program as a list of statements
AST* parseProgram(Parser* parser){
int capacity = 4 ;
AST** statements = malloc(capacity * sizeof(AST));
if (!statements) {
parseErrorFlag = 1;
fprintf(stderr, "Error: Memory allocation failed for statements array.\n");
return NULL;
}
int count = 0;
while (parser->pos < parser->count) {
AST* stmt = parseStatement(parser);
if (stmt) {
if (count >= capacity) {
capacity *= 2;
statements = realloc(statements, sizeof(AST) * capacity);
if (!statements) {
parseErrorFlag = 1;
fprintf(stderr, "Error: Memory reallocation failed for statements array.\n");
return NULL;
}
}
statements[count++] = stmt;
} else {
if (parser->pos < parser->count) {
Token* errToken = &parser->tokens[parser->pos];
} else {
fprintf(stderr, "Parse error: Unexpected end of input\n");
}
parseErrorFlag = 1;
break;
}
}
AST* node = malloc(sizeof(AST));
if( !node ){
parseErrorFlag = 1;
fprintf(stderr, "Error: Memory allocation failed for program AST.\n");
return NULL;
}
node->type = AST_PROGRAM;
node->programStatements.position = 0;
node->programStatements.statements = statements;
node->programStatements.count = count;
return node;
}