-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.c
More file actions
215 lines (193 loc) · 5.13 KB
/
driver.c
File metadata and controls
215 lines (193 loc) · 5.13 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
/*
*Batch Number 6
*Abhinav Bhatia (2011A7PS371P)
*Mukul Bhutani (2011A7PS343P)
*/
#include "compiler.h"
#include "first.h"
#include "follow.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "AST.h"
#include "SymbolTable.h"
#include "typeChecker.h"
#include "codeGenerator.h"
#define GRAMMAR_INPUT_FILENAME "realGrammar.txt"
#define KEYWORDS_INPUT_FILENAME "keywords.txt"
#define DEFAULT_OUTPUT_PARSE_TREE_FILE "parseTreeOutFile.txt"
#define AST_RULES_FILENAME "astRules.txt"
#define PARSER_NOT_INVOKED 0
#define PARSING_FAILED 1
#define PARSED_SUCCESSFULLY 2
int parseOnlySourceCode(Parser* p, Lexer lex)
{
int result = parseInputSourceCode(p, lex);
if (result)
{
printf("\n\nThe input is syntactically correct. :)\n\n");
return PARSED_SUCCESSFULLY;
}
else
{
printf("\n\nThe input is NOT syntactically correct. :(\n\n");
return PARSING_FAILED;
}
}
int printOnlyParsetree(Parser* p, Lexer lex, char* outPutTreeFile, int isParsed)
{
if (isParsed==PARSER_NOT_INVOKED)
{
isParsed = parseOnlySourceCode(p, lex);
}
if (isParsed == PARSED_SUCCESSFULLY)
{
printParseTree(p->parseTree, outPutTreeFile);
return isParsed;
}
else
{
return isParsed;
}
}
int main(int argc, char** argv)
{
////////////////Basic Printing/////////////////////////
//printf("Submitted by Batch No. 06:\n%30s\t(%s)\n%30s\t(%s)\n\n\n", "Abhinav Bhatia", "2011A7PS371P", "Mukul Bhutani ", "2011A7PS343P");
printf("LEVEL %d: %s", 3, "AST/Symbol Table/Type checking/Symantic Rules modules work\n");
printf("Code generation implemented for most constructs\n\n");
////////////////Basic Printing End/////////////////////
FILE* pSourceFile = NULL;
int choice;
int isParsed = PARSER_NOT_INVOKED;
if (argc > 1)
{
pSourceFile = fopen(argv[1], "r");
}
else if (argc == 1)
{
printf("\nLess number of arguments ...Two arguments required....\n");
printf("1. Source Code File.\n");
printf("2. File for printing parse tree.\n");
char c;
scanf("%c", &c);
return 0;
}
if (pSourceFile == NULL)
{
printf("\nError: No Input File!\nExiting...........");
char c;
scanf("%c", &c);
return -1;
}
FILE* pKeywordsFile = fopen(KEYWORDS_INPUT_FILENAME, "r");
if (!pKeywordsFile)
{
printf("\nError: keywords file \"%s\" not found.\nExiting..........", KEYWORDS_INPUT_FILENAME);
char c;
scanf("%c", &c);
return -1;
}
FILE* grammarInputFile = fopen(GRAMMAR_INPUT_FILENAME, "r");
if (!grammarInputFile)
{
printf("\nError: Grammar file \"%s\" not found.\nExiting..........", GRAMMAR_INPUT_FILENAME);
char c;
scanf("%c", &c);
return -1;
}
FILE* astRulesFile = fopen(AST_RULES_FILENAME, "r");
if (!astRulesFile)
{
printf("\nError: AST Rules file \"%s\" not found.\nExiting..........", AST_RULES_FILENAME);
char c;
scanf("%c", &c);
return -1;
}
Parser parser;
Lexer lex = lexer_createNew(pSourceFile, pKeywordsFile);
printf("\n\nPlease enter your choice\n");
printf("1. Print list of tokens.\n");
printf("2. Verify syntactic correctness of input source code.\n");
printf("3. Print Abstract Syntax Tree.\n");
printf("4. Print Symbol Table.\n");
printf("5. Verify syntactic and semantic correctness.\n");
printf("6. Produce Assembly Code.\n");
printf("7. Exit.\n");
char flushChar;
scanf("%d%c", &choice, &flushChar);
switch (choice)
{
case 1:
lexer_runLexicalAnalyses(lex);
break;
case 2:
parser = parser_initialise(grammarInputFile, astRulesFile);
isParsed = printOnlyParsetree(&parser, lex, DEFAULT_OUTPUT_PARSE_TREE_FILE, isParsed);
break;
case 3:
parser = parser_initialise(grammarInputFile, astRulesFile);
isParsed = parseOnlySourceCode(&parser, lex);
if (isParsed == PARSED_SUCCESSFULLY)
{
Tree ast = createAst(parser);
printAST(ast);
}
break;
case 4:
parser = parser_initialise(grammarInputFile, astRulesFile);
isParsed = parseOnlySourceCode(&parser, lex);
if (isParsed == PARSED_SUCCESSFULLY)
{
Tree ast = createAst(parser);
createSymbolTables(ast);
turnOnReportingSemanticErrors(FALSE);
typeExtractorandChecker(ast);
printSymbolTable();
}
break;
case 5:
parser = parser_initialise(grammarInputFile, astRulesFile);
isParsed = parseOnlySourceCode(&parser, lex);
if (isParsed == PARSED_SUCCESSFULLY)
{
Tree ast = createAst(parser);
createSymbolTables(ast);
printf("\n\nRunning semantic analysis....");
turnOnReportingSemanticErrors(TRUE);
if (typeExtractorandChecker(ast))
{
printf("\n\nCode compiles successfully..........:)");
}
}
break;
case 6:
parser = parser_initialise(grammarInputFile, astRulesFile);
isParsed = parseOnlySourceCode(&parser, lex);
if (isParsed == PARSED_SUCCESSFULLY)
{
Tree ast = createAst(parser);
createSymbolTables(ast);
turnOnReportingSemanticErrors(TRUE);
if (typeExtractorandChecker(ast))
{
printf("\n\nCode compiles successfully..........:)");
printf("\n\nGenerating Code......");
FILE* asmm = fopen(argv[2], "w");
generateCode(ast, asmm);
printf("\n\nFinished code generation.");
//Call code generation here
}
}
break;
case 7:
return 0;
break;
default:
printf("Invalid Choice...please enter correct choice (1-7)");
break;
}
char c;
scanf("%c", &c);
return 0;
}