-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
459 lines (409 loc) · 15.3 KB
/
parser.c
File metadata and controls
459 lines (409 loc) · 15.3 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
/*
BATCH NUMBER: 23
PRABODH AGARWAL 2012B1A7801P
DEEPANSHU SINGH 2012B3A7593P
parser.c:
void getGrammar(grammar *g);
void createParseTable(grammar g, table *t);
char* getFirstSet(char *lhs, char *rhs, char *set);
int parseInputSourceCode(FILE *sourceCodeFile, table tb, grammar g, parseTree *root, tokenInfo *t);
void printParseTree(parseTree *p, FILE *outfile);
void printParseTable(table t);
char * getFirstSet(char *, char *, char *);
char * getFollowSet(char *);
*/
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include "lexer.h"
#include "parseDef.h"
#include "helper_functions.h"
#include "parser.h"
/*
* read the grammar file and populate the data structure with respective enum
* values. 1st entry of row is the lhs of rule, 2nd entry is a count entry of number of elements
* on the rhs, 3rd entry onwards, the rhs of the grammar is stored. The values
* are stored as respective enums that can be obtained from lexerdef.h for
* terminals and parsrdef for non-terminals.
* The grammar.txt file stores rules in a form of 2 characters per rule, e.g. --
* <assignmentStmt> = a1
* <arithmetixExpression> = a2 and so on, hence read as 2 characters at once
*/
short compilation = 1; //successful compilation
void printExpectedTokens(nontermid state, table tb)
{
if(state==0)
return;
fprintf(stderr,"Expected tokens: ");
int j;
for(j = 0; j < MAX_TERMINALS; j++)
if(tb[state - NON_TERMINAL_OFFSET][j]!=-1)
fprintf(stderr,"%s\t",tokenName(j));
fprintf(stderr,"\n");
}
void getGrammar(grammar *g)
{
FILE *fp;
char token[20],tok[3];
fp = fopen("grammar.txt","r");
int rule,ruleLine = 0,i;
tok[2]='\0';
while(!feof(fp))
{
bzero(token,21);
fscanf(fp,"%s",token); //LHS
tok[0]=token[0];
tok[1]=token[1];
//retrieve enum value for given LHS token
gg[ruleLine][0]=grammar_to_enum(tok);
gg[ruleLine][1]=0;
rule=2;
//RHS
//fscanf(fp,"%s",token);
for(i=3;token[i]!='\0';i+=2)
{
tok[0]=token[i];
tok[1]=token[i+1];
//retrieve enum value for given RHS token
gg[ruleLine][rule++]=grammar_to_enum(tok);
}
gg[ruleLine++][1]=rule-2;//count of elements on RHS
}
fclose(fp);
}
/*
* Read the grammar and populate parse table. Given table entry, e.g.:
* table[enum of some nonterminal][enum of some terminal] = valid rule number in
* grammar g data struture or -1 in case no rule exists
*/
void createParseTable(grammar g, table *t)
{
int i,j,grammarEnum;
char *fi, *rulerhs, *rulelhs, *rhspart;
rhspart= (char *)malloc(sizeof(char)*3);
rulelhs = (char *)malloc(sizeof(char)*3);
rulerhs = (char *)malloc(sizeof(char)*MAX_RULE_SIZE);
fi = (char *)calloc(MAX_RULE_SIZE,sizeof(char));
rulelhs[2]='\0';
//i indexes to rules in order as given in grammar.txt
for(i=0;i<MAX_RULES;i++)
{
bzero(rulerhs,MAX_RULE_SIZE);
bzero(rulelhs,3);
bzero(rhspart,3);
strcat(rulelhs,enum_to_grammar(g[i][0]));
for(j=0;j<g[i][1];j++)
strcat(rulerhs,enum_to_grammar(g[i][j+2]));
//for rule A->x; for all t belonging to FIRST(x), table[A,t] = rule.
getFirstSet(rulelhs,rulerhs,fi);
if(fi!=NULL)
{
for(j=0;j<strlen(fi);j+=2)
{
rhspart[0]=fi[j];
rhspart[1]=fi[j+1];
grammarEnum = grammar_to_enum(rhspart);
grammarEnum-=grammarEnum>=NON_TERMINAL_OFFSET?NON_TERMINAL_OFFSET:0;
//tt is a macro
tt[g[i][0]-NON_TERMINAL_OFFSET][grammarEnum]=i+1;
}
}
}
}
char* getFirstSet(char *lhs, char *rhs, char *set)
{
int i,epsflag=0;
struct y **p;
//read the rule string char by char and get first sets.
bzero(set,20);
for(i=0;i<strlen(rhs);i+=2)
{
if(rhs[i]<65||rhs[i]>90)
{
//terminal
if(rhs[i]!='e'||rhs[i+1]!='2') //check for not epsilon
sprintf(set,"%s%c%c",set,rhs[i],rhs[i+1]);
else
epsflag=1;
break;
}
else
{
p = ptr[rhs[i]-65][rhs[i+1]-48];
strcat(set,p[1]->a);
if(p[1]->eps!=2) //A-->BC; if eps belongs to first(B) then append firct(C) else don't
break;
}
epsflag=1;//first(BC) contains eps, hence we need to add follow of A as well
}
if(epsflag)
strcat(set,ptr[lhs[0]-65][lhs[1]-48][2]->a); //add follow(A) from A-->BC
return set;
}
/*
* Make a parse tree by reading the source code file. The program's start state
* is at "program". As the lookahead tokens are recieved by getNextToken(), the parsetable
* is looked up using current state and lookahead and the rule number is retrieved, using these rules, parse table is created
* with the rules on right hand side forming sibling nodes at level of the tree
* and rules on lhs forming the parent node.
*/
int parseInputSourceCode(FILE *sourceCodeFile, table tb, grammar g, parseTree *root, tokenInfo *t)
{
int state, i,ruleNo, *rule, nochildren;
parseTree *tree, *node;
nontermid goTo;
short err = 0; //error flag
tree = NULL;
//in case errorneous token is recieved, notify appropriately and get next token
if(t->tokenClass == TK_ERROR)
{
fprintf(stderr,"error: line %llu:%llu %s\n",t->line_num,t->col,t->lexeme);
printExpectedTokens(root->nonterm, tb);
getNextToken(sourceCodeFile, t);
compilation = 0;
return -2;
}
//if state points at terminal, match with lookahead to determine correctness.
if(root->isTerminal)
{
//match the lookahead
//case of lookahead mismatch
if(root->term.tokenClass != t->tokenClass)
{
fprintf(stderr, "error: line %llu:%llu The token %s for lexeme <%s> does not match. The expected token here is %s\n", t->line_num, t->col, tokenName(t->tokenClass), t->lexeme, tokenName(root->term.tokenClass));
compilation = 0;
return -2;
}
else
{
//matching lookaheads, advance token
strcpy(root->term.lexeme,t->lexeme);
root->term.line_num=t->line_num;
//printf("\n%s\t%llu\t%s\t\tyes",root->term.lexeme,root->term.line_num,enum_to_grammar(root->term.tokenClass));
getNextToken(sourceCodeFile, t);
return 0;
}
}
else
{
//get current state of parser
state = root->nonterm;
state-=NON_TERMINAL_OFFSET;
//retrieve rule number based on current state and the lookahead
ruleNo = tb[state][t->tokenClass]-1; //array indexing
//no such rule!
//error happened because no rule exists for parser in current state with
//given lookup
if(ruleNo < 0)
{
fprintf(stderr, "error: line %llu:%llu Found unexpected token %s for lexeme <%s>\n",t->line_num, t->col, tokenName(t->tokenClass), t->lexeme);
printExpectedTokens(root->nonterm, tb);
compilation = 0;
return -1;
}
//found rule
else
{
/*
* the rule is of the form:
* lhs count(terms in rhs) rules on rhs
*/
rule = g[ruleNo];
//number of terms on rhs
nochildren = rule[1];
root->nochildren = nochildren;
if(nochildren>0)
node = root->children = (parseTree *)malloc(sizeof(parseTree)*nochildren);
for(i=0;i<nochildren;i++)
{
//initialize tree node
node[i].nochildren = 0;
node[i].children=NULL;
bzero(node[i].term.lexeme,MAX_LEXEME_SIZE);
//set node for non-terminal
if(rule[i+2]>=NON_TERMINAL_OFFSET)
{
node[i].isTerminal = 0;
node[i].nonterm = rule[i+2];
}
//set node for terminal
else
{
node[i].isTerminal = 1;
node[i].term.tokenClass = rule[i+2];
if(rule[2]==eps)
{
strcpy(node[i].term.lexeme,"eps");
node[i].term.line_num = t->line_num;
node[i].term.tokenClass = eps;
continue;
}
}
//printf("\n----\t----\t%s\t\t%s\tno",enum_to_grammar(node[i].nonterm),enum_to_grammar(node[i].nonterm));
//go to next state using the lookahead
if(parseInputSourceCode(sourceCodeFile, tb, g, node+i, t)==-1)
{
//error due to token mismatch or wrong state for given lookahead.
/*
* Error recovery technique mentioned below, succesfully
* detects errors. For the given grammar, Panic
* Error Recovery Mode missed a lot of syntax errors and
* hence can be deemed weaker than the technique described
* below.
*/
/*If error occurs inside a block, search for the end to that
* block. A block can be defined as a set of tokens which
* are complete. e.g.: A semi-colon terminated assignment
* statement is a complete block, any error found in this
* block will be successfully recovered by getting out of
* the assignment which can be done by consuming tokens
* ubtil a semicolon (;) is found. Similarly, errors in
* conditional blocks can be recovered from by consuming all
* tokens until <endif> is found and iterative stmt is
* recovered by consuming tokens until <endwhile> is found
*/
//unfold the tree to go to the start state of a given block
//in which error is found and then consume tokens until
//corresponding end state is found
if(node[i].nonterm == mainFunctions || node[i].nonterm == declaration || node[i].nonterm == assignmentStmt || node[i].nonterm == iterativeStmt || node[i].nonterm == funCallStmt || node[i].nonterm == conditionalStmt || node[i].nonterm == ioStmt || node[i].nonterm == typeDefinition || node[i].nonterm == returnStmt || node[i].nonterm == function)
{
err = -1;
break;
}
else
compilation = 0;
return -1;
}
}
//goTo contains the endstate of the block where an error is found,
//tokens are consumed until such endstate is not reached
if(err==-1)
{
if(node[i].nonterm == conditionalStmt)
goTo = TK_ENDIF;
else if(node[i].nonterm == iterativeStmt)
goTo = TK_ENDWHILE;
else if(node[i].nonterm == function)
goTo = TK_END;
else
goTo = TK_SEM;
while(t->tokenClass != goTo)
{
getNextToken(sourceCodeFile, t);
if(t->tokenClass == TK_ERROR)
fprintf(stderr,"error: line %llu: %s\n",t->line_num,t->lexeme);
else if (t->tokenClass == TK_EOF)
return 1;
}
getNextToken(sourceCodeFile, t);
}
}
}
return 1;
}
//print contents od parsetable
void printParseTable(table t)
{
int i,j;
for(i=0;i<MAX_NON_TERMINALS;i++)
{
for(j=0;j<MAX_TERMINALS;j++)
if(t[i][j]>0)
printf("For %s-->%s rule is %d\n",enum_to_grammar(i+NON_TERMINAL_OFFSET),enum_to_grammar(j),t[i][j]);
}
}
//print parse tree in depth first order
void printParseTree(parseTree *p, FILE *outfile)
{
int nochildren = p->nochildren, i;
parseTree *t;
if(p->nonterm<NON_TERMINAL_OFFSET)
return;
t = p->children;
for (i = 0; i < nochildren; i++,t++)
{
char n[100];
bzero(n,100);
if(t->term.tokenClass == TK_NUM || t->term.tokenClass == TK_RNUM)
strcpy(n , t->term.lexeme);
else
strcpy(n , "----");
if(t->isTerminal)
fprintf(outfile,"\n%20s%12llu\t%15s\t\t%16s\t\t\t\t\t %20s\t\tyes",t->term.lexeme,t->term.line_num,tokenName(t->term.tokenClass),n,tokenName(p->nonterm));
else
{
fprintf(outfile,"\n ----\t ---- ----%21s%24s\t\t%20s\t\t no",n,tokenName(t->nonterm),tokenName(p->nonterm));
printParseTree(t, outfile);
}
}
}
//construct an AST by removing useless nodes from the parse tree
void createAbstractSyntaxTree(parseTree *p, parseTree *ast) {
/* recursively constructs the abstract syntax tree
* by removing "useless" nodes from the parse tree
* the ast must be initialized to the program node
* every call constructs a subtree of the ast
* rooted at the current nonterminal
*/
int i, j, usefulChildrenCount = 0;
for(i = 0; i < p->nochildren; i++)
// all nonterminals are useful
if(!p->children[i].isTerminal || isUseful(p->children[i].term.tokenClass))
usefulChildrenCount++;
ast->nochildren = usefulChildrenCount;
ast->children = (parseTree *)malloc(ast->nochildren * sizeof(parseTree));
i = 0, j = 0;
while(i < p->nochildren) {
if(p->children[i].isTerminal) {
if(isUseful(p->children[i].term.tokenClass)) {
// copy ith node of p to jth node of ast
ast->children[j].isTerminal = 1;
ast->children[j].term.tokenClass = p->children[i].term.tokenClass;
strcpy(ast->children[j].term.lexeme, p->children[i].term.lexeme);
ast->children[j].term.line_num = p->children[i].term.line_num;
j++;
}
}
else {
// recursively construct the ast rooted here
ast->children[j].nonterm = p->children[i].nonterm;
createAbstractSyntaxTree(&p->children[i], &ast->children[j]);
j++;
}
i++;
}
}
/*
int main(int argc, char **args)
{
int i,j;
FILE *fp = fopen(args[1],"r");
FILE *outfile = fopen("outfile.txt","w");
grammar g;
table t;
parseTree *tree;
tokenInfo *to;
to = (tokenInfo *)malloc(sizeof(tokenInfo));
tree = (parseTree *)malloc(sizeof(parseTree));
tree->nonterm = program;
tree->isTerminal = 0;
tree->children = NULL;
for(i=0;i<MAX_NON_TERMINALS;i++)
for(j=0;j<MAX_TERMINALS;j++)
t[i][j]=-1;
h = hash_keywords();
getNextToken(fp,to);
populate();
getGrammar(&g);
createParseTable(g,&t);
printParseTable(t);
parseInputSourceCode(fp,t,g,tree,to);
printf("\nlexemeCurrentNode\tlineno\t\ttoken\t\tNodeSymbol\t\t parentNodeSymbol\t isLeafNode\tvalueIfNumber");
printParseTree(tree, outfile);
printf("\n");
fclose(fp);
fclose(outfile);
return 0;
}
*/