-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver.c
184 lines (169 loc) · 5.49 KB
/
driver.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
/*
-=-=-=-=-=-=-=-=-=-=-=-=-=
BATCH 26
-=-=-=-=-=-=-=-=-=-=-=-=-=
AAYUSH AHUJA 2010A7PS023P
MAYANK GUPTA 2010A7PS022P
-=-=-=-=-=-=-=-=-=-=-=-=-=
driver.c
-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
#include<ctype.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include<malloc.h>
#include "lexerDef.h"
#include "lexer.h"
#include "parserDef.h"
#include "parser.h"
#include "symbolTable.h"
#include "codegen.h"
bool any_error =0;
int main(int argc, char *argv[])
{
FILE *g=fopen("grammar.txt", "r");
// FILE *p=fopen("parsetable.csv", "w");
FILE *tree;
// FILE *ast=fopen("ast.txt", "w");
if(g==NULL)
{
printf("Grammar file not found\n");
return 0;
}
int opt,i,fp,Gno, totalAllocatedMemory=0;
fp = open(argv[1],O_RDONLY);
if(fp==-1)
{
printf("input file not found\n");
return 0;
}
bool error = 0;
Table T[60][60];
grammar G[100];
sets S[60];
keyword kw[48];
keyword kn[200];
parseTree P,A;
for(i=0; i<48; i++)
kw[i].present=FALSE;
keywordTable kt = kw;
initkt(kt);
for(i=0; i<200; i++)
kn[i].present=FALSE;
keywordTable nt = kn;
initNt(nt);
Gno=createGrammar(g,G,nt);
initSets(S,G,Gno);
// createSets(s,S,nt);
initTable(T);
createParseTable(G,T,S,Gno);
// printTable(p, T);
variable GT[100]; //max 100 globals possible
funTable FT[100];
recTable RT[100];
tokenList list;
printf("\nLEVEL 4: AST, Symbol table, Type Checking, Semantic Rules modules work\n");
do
{
printf("\n 1 : Print the token list.\n 2 : Verify the syntactic correctness\n 3 : Print abstract syntax tree\n 4 : Print Symbol Table\n 5 : Verify Semantic correctness\n 6 : Generate Assembly Code\n\nSelect option->");
scanf("%d", &opt);
switch(opt)
{
case 1:
list=createTokenList(fp, kt);
printTokenList(kt, list);
break;
case 2:
P = parseInputSourceCode(fp, T, kt, G, &error, S);
if(!error)
{
printParseTree(P, tree);
// if(P!=NULL)
// printf("\nParse Tree generated and printed in file tree.txt\n");
}
break;
case 3:
P = parseInputSourceCode(fp, T, kt, G, &error, S);
if(error)
break;
//parseTree A;
A = createAbstractSyntaxTree(P);
if(!error)
{
printAST(A, tree, &totalAllocatedMemory);
if(A!=NULL)
printf("\nSize of AST is %d\n",totalAllocatedMemory);
}
break;
case 4:
P = parseInputSourceCode(fp, T, kt, G, &error, S);
if(error)
break;
A = createAbstractSyntaxTree(P);
initSymbolTable(GT,FT,RT);
createGlobalTable(GT,A);
createRecordTable(RT,A);
createSymbolTable( GT, FT, RT, A);
printGT(GT,RT);
//printRT(RT);
printFT(FT,RT);
break;
case 5:
P = parseInputSourceCode(fp, T, kt, G, &error, S);
if(error)
break;
A = createAbstractSyntaxTree(P);
initSymbolTable(GT,FT,RT);
createGlobalTable(GT,A);
createRecordTable(RT,A);
createSymbolTable( GT, FT, RT, A);
//printGT(GT,RT);
//printRT(RT);
//printFT(FT,RT);
char funname[50];
strcpy(funname," ");
typeParse(A,GT, FT,RT,funname);
if(any_error==0)
printf("Code compiles successfully..........\n");
break;
case 6:
P = parseInputSourceCode(fp, T, kt, G, &error, S);
if(error)
break;
A = createAbstractSyntaxTree(P);
initSymbolTable(GT,FT,RT);
createGlobalTable(GT,A);
createRecordTable(RT,A);
createSymbolTable( GT, FT, RT, A);
//printGT(GT,RT);
FILE *fp = fopen(argv[2],"w");
fprintf(fp, ".model small\n.stack\n" );
fprintf(fp, ".data\n" );
int hval = hash("_main",100);
for(i=0;i<100;i++)
{
if(GT[i].filled == 1)
fprintf(fp, "%s dw 0\n", GT[i].name);
if(FT[hval].table[i].filled == 1)
fprintf(fp, "%s dw 0\n", FT[hval].table[i].name);
}
fprintf(fp, ".code\n");
fprintf(fp,"%s" ,longtext);
fprintf(fp, "\n\n\nstart proc\n\n\n");
generateCode(GT,FT,RT,A,fp);
//fprintf(fp, "true:\nmov AX,0\n" );
//fprintf(fp, "false:\nmov AX,1\n" );
fprintf(fp, "ret\n\n\nstart endp\nend start\n" );
fclose(fp);
printf("Generated Assembly code in %s\n",argv[2]);
break;
default:
printf("\nPlease select a valid option\n");
}
}while(opt<1 || opt>6);
fclose(g);
// fclose(tree);
return 0;
}