-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser-main.c
103 lines (74 loc) · 1.77 KB
/
parser-main.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
#include"parser-common.h"
int load(struct parseinfo *table);
int parse(struct parseinfo parseTable, char *);
int main(int argc, char **argv)
{
struct parseinfo parseTBL;
int ret;
int i,j,k;
char inputFile[256];
char *inputBuf = NULL;
int nbytes;
FILE *fp;
ret = load(&parseTBL);
if(ret)
{
printf("\n%s:%d load() returned 1.",__FILE__,__LINE__);
printf("\nPerhaps the file has been deleted, renamed or moved.\n");
exit(EXIT_FAILURE);
}
/* Print the parsing table */
printf("\n\n************************************** PARSE TABLE ******************************************\n");
printf("%10s","");
for(i=0;i<parseTBL.termcount;i++)
{
printf(" %10s ",parseTBL.terminals[i]);
}
printf("\n\n");
for(j=0 ; j<parseTBL.varcount ; j++)
{
printf("%10s",parseTBL.variables[j]);
for(k=0;k<parseTBL.termcount;k++)
{
printf(" %10s ",parseTBL.actions[j][k]);
}
printf("\n");
}
printf("\nEnter name of the file you wish to parse>>");
scanf("%s",inputFile);
fp = fopen(inputFile,"r");
if(!fp)
{
printf("\n%s:%d ",__FILE__,__LINE__);
perror(inputFile);
exit(EXIT_FAILURE);
}
/*
* @TODO: Implement incremental read from input file.
* Perhaps through double buffering.
*/
fileread(&inputBuf, inputFile , &nbytes );
for(i=0 ; i< nbytes-1 ; i++)
{
if(inputBuf[i] == '\t' || inputBuf[i] == '\n' )
{
inputBuf[i] = ' ';
}
if(inputBuf[i] == '$')
{
inputBuf[i+1] = '\0';
}
}
ret = parse(parseTBL,inputBuf);
if(ret == 0)
{
printf("\nParsing completed successfully.");
}
else if(ret == 1)
{
printf("\n%s:%d Function parse() returned %d.Parse could not complete",__FILE__,__LINE__,ret);
exit(EXIT_FAILURE);
}
printf("\n*** HAPPY HAPPY JOY JOY! ***\n");
exit(EXIT_SUCCESS);
}