forked from rperlste/UniversalCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniversalCompiler.cpp
386 lines (355 loc) · 10.5 KB
/
UniversalCompiler.cpp
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
#include "UniversalCompiler.h"
UniversalCompiler::UniversalCompiler( const char* grammarFilePtr,
const char* programFilePtr,
const char* compiledFilePtr ){
if( grammarFilePtr != NULL ){
grammarFile.open( grammarFilePtr );
}
if( programFilePtr != NULL ){
programFile.open( programFilePtr );
}
if( compiledFilePtr != NULL ){
compiledFile.open( compiledFilePtr );
}
grammarAnalyzer = NULL;
predictSetAnalyzer = NULL;
parseTableGenerator = NULL;
scanner = NULL;
parser = NULL;
compiler = NULL;
}
UniversalCompiler::~UniversalCompiler(){
if( grammarAnalyzer != NULL )
delete grammarAnalyzer;
if( predictSetAnalyzer != NULL )
delete predictSetAnalyzer;
if( parseTableGenerator != NULL )
delete parseTableGenerator;
if( scanner != NULL )
delete scanner;
if( parser != NULL )
delete parser;
if( compiler != NULL )
delete compiler;
}
void UniversalCompiler::greeting(){
std::cout << " Welcome to Robert Perlstein's Universal Compiler!\n"
<< "This project was created under the guidance of Dr. Boris Stilman\n"
<< "of the University of Colorado in Denver.\n\n"
<< "A Universal Compiler takes a valid format of LL(1) BNF grammar.\n"
<< "This grammar is then analyzed, and generating tables are created\n"
<< "so that the program code can be scanned and compiled.\n\n"
<< "Note to users! The grammar and program code can be passed as\n"
<< "command line arguments, and should placed in the following order...\n"
<< "universal_compiler.exe grammar.dat program.dat\n"
<< "The user can also set these values during runtime.\n\n"
<< "Please enjoy the program!";
}
void UniversalCompiler::initialize(){
// Grammar file initialization.
if( !grammarFile ){
std::cout << "Please set the grammar file: ";
std::string grammarFileName;
std::cin >> grammarFileName;
if( !setGrammarFile( grammarFileName )){
while( true ){
std:: cout << "\nInvalid grammar file."
<< "Enter 'Y' if you would like to try again, anything else cancels: ";
char choice = ' ';
if( choice == 'Y' || choice == 'y' ){
std::cout << "Please set the grammar file: ";
std::cin >> grammarFileName;
if( setGrammarFile( grammarFileName ))
break;
}
else break;
}
}
}
// Program file inisitalization.
if( !programFile ){
std::cout << "Please set the program file: ";
std::string programFileName;
std::cin >> programFileName;
if( !setProgramFile( programFileName )){
while( true ){
std:: cout << "\nInvalid program file."
<< "Enter 'Y' if you would like to try again, anything else cancels: ";
char choice = ' ';
if( choice == 'Y' || choice == 'y' ){
std::cout << "Please set the program file: ";
std::cin >> programFileName;
if( setProgramFile( programFileName ))
break;
}
else break;
}
}
}
}
bool UniversalCompiler::setGrammarFile( const std::string& grammarFileName ){
if( grammarFile )
grammarFile.close();
grammarFile.open( grammarFileName );
return ( grammarFile ) ? true : false;
}
bool UniversalCompiler::setProgramFile( const std::string& programFileName ){
if( programFile )
programFile.close();
programFile.open( programFileName );
return ( programFile ) ? true : false;
}
bool UniversalCompiler::setCompiledFile( const std::string& compiledFileName ){
if( compiledFile )
compiledFile.close();
compiledFile.open( compiledFileName );
return ( compiledFile ) ? true : false;
}
// Runs the menu, giving access to the various
// componenets of the UC.
// Returns false if the user chose to exit the menu.
bool UniversalCompiler::menu(){
char choice;
while( true ){
std::cout << "\n\n >>>> UNIVERSAL COMPILER <<<<\n\n"
<< " Grammar file set: " << ((grammarFile.is_open()) ? " TRUE\n" : "FALSE\n")
<< " Program file set: " << ((programFile.is_open()) ? " TRUE\n" : "FALSE\n")
<< " Compiled file set: " << ((compiledFile.is_open()) ? "TRUE\n\n" : "FALSE\n\n")
<< " ------- Main Menu -------\n\n"
<< " 1 ... File Manager ( Set or view grammar and program files ).\n"
<< " 2 ... Grammar Analyzer\n"
<< " 3 ... Predict Set Analyzer\n"
<< " 4 ... LL(1) Parse Table Generator\n"
<< " 5 ... Scanner\n"
<< " 6 ... Parser\n"
<< " 7 ... Compiler\n"
<< " 8 ... View Compiled Code (not working yet)\n"
<< " 0 ... EXIT.\n\n"
<< " Enter an value from the menu: ";
std::cin >> choice;
if( choice < '0'
|| choice > '9' )
continue;
switch( choice )
{
case '1':
while( runFileManager() );
break;
case '2':
if( grammarFile.is_open() )
runGrammarAnalyzer(true);
else
std::cout << "\n\nGrammar file not found. Cannot run Grammar Analyzer.";
break;
case '3':
if( grammarFile.is_open() )
runPredictSetAnalyzer(true);
else
std::cout << "\n\nGrammar file not found. Cannot run Predict Set Analyzer.";
break;
case '4':
if( grammarFile.is_open() )
runParseTableGenerator(true);
else
std::cout << "\n\nC Cannot run Parse Table Generator.";
break;
case '5':
if( programFile.is_open() )
runScanner();
else
std::cout << "\n\nProgram file not found. Cannot run Scanner.";
break;
case '6':
if( grammarFile.is_open()
&& programFile.is_open() )
runParser(true);
else
std::cout << "\n\nProgram or grammar file not found. Cannot run Parser.";
break;
case '7':
if( grammarFile.is_open()
&& programFile.is_open() )
runCompiler(true);
else
std::cout << "\n\nProgram or grammar file not found. Cannot run Compiler.";
break;
case '8':
if( compiledFile.is_open() )
printFileToCout( compiledFile );
else
std::cout << "\n\nCompiled file not found.";
break;
case '0':
return false;
} // END MAIN MENU
std::cout << "\n\n_________________________________________________________";
return true;
}
}
bool UniversalCompiler::runFileManager(){
char choice;
while( true ){
std::cout << "\n\n ------- File Manager ------- \n\n"
<< " 1 ... Set grammar file.\n"
<< " 2 ... Set program file.\n"
<< " 3 ... Set compiled output file.\n"
<< " 4 ... View grammar file.\n"
<< " 5 ... View program file.\n"
<< " 0 ... Exit File Manager.\n\n"
<< " Enter an value from the menu: ";
std::cin >> choice;
switch( choice ){
case '1':
{
std::string grammarFileName;
std::cout << "\n\nEnter filename for grammar: ";
std::cin >> grammarFileName;
if( !setGrammarFile( grammarFileName ))
std::cout << "\n\nInvalid grammar file.";
break;
}
case '2':
{
std::string programFileName;
std::cout << "\n\nEnter filename for program: ";
std::cin >> programFileName;
if( !setProgramFile( programFileName ))
std::cout << "\n\nInvalid program file.";
break;
}
case '3':
{
std::string compiledFileName;
std::cout << "\n\nEnter filename for compiled output: ";
std::cin >> compiledFileName;
if( !setCompiledFile( compiledFileName ))
std::cout << "\n\nInvalid compiled file.";
break;
}
case '4':
{
if( !grammarFile.is_open() ){
std::cout << "\n\nGrammar file not found.";
break;
}
else{
printFileToCout( grammarFile );
break;
}
}
case '5':
{
if( !programFile.is_open() ){
std::cout << "\n\nProgram file not found.";
break;
}
else{
printFileToCout( programFile );
break;
}
}
case '0':
{
return false;
}
}
}
}
void UniversalCompiler::runGrammarAnalyzer( bool printOutput ){
if( grammarAnalyzer != NULL )
delete grammarAnalyzer;
grammarAnalyzer = new GrammarAnalyzer( &grammarFile );
grammarAnalyzer->analyzeGrammar();
if( printOutput ) {
std::cout << "\n\n ------- Grammar Analyzer -------\n\n";
grammarAnalyzer->printInputGrammar();
grammarAnalyzer->printProductions();
grammarAnalyzer->printDataTables();
}
}
void UniversalCompiler::runPredictSetAnalyzer( bool printOutput ){
if( predictSetAnalyzer != NULL )
delete predictSetAnalyzer;
runGrammarAnalyzer( false );
predictSetAnalyzer = new Predict( grammarAnalyzer->getGrammar() );
try{
predictSetAnalyzer->MarkLambda();
predictSetAnalyzer->FillFirstSet();
predictSetAnalyzer->FillFollowSet();
predictSetAnalyzer->FillPredictSet();
if( printOutput ){
std::cout << "\n\n ------- Predict Set Analyzer -------\n\n";
predictSetAnalyzer->PrintLookAheadSets();
}
} catch( IndexOutOfBounds e ){
std::cout << "There was an error while running predict: " << e.what();
} catch( ProductionNotFound e ){
std::cout << "There was an error while running predict: " << e.what();
}
}
void UniversalCompiler::runParseTableGenerator( bool printOutput ){
if( parseTableGenerator != NULL )
delete parseTableGenerator;
runPredictSetAnalyzer( false );
parseTableGenerator = new ParseTable( (*predictSetAnalyzer->getGrammar()) );
parseTableGenerator->GenerateTable();
if( printOutput ){
std::cout << "\n\n ------- Transition Table Generator -------\n\n";
parseTableGenerator->GetTableAsString();
}
}
void UniversalCompiler::runScanner(){
if( scanner != NULL )
delete scanner;
scanner = new Scanner( &programFile );
std::cout << "\n\n ------- Scanner Driver -------\n\n";
while( !scanner->isDoneScanning()
&& !scanner->hasError() ){
Token token = scanner->scannerDriver();
if( !scanner->hasError() ){
std::cout << token.getTokenTypeString();
if( token.getTokenValue().length() > 0 ) {
std::cout << "( \"" << token.getTokenValue() << "\" )\n";
} else {
std::cout << "()\n";
}
}
}
scanner->close();
}
void UniversalCompiler::runParser( bool printOutput ){
if( parser != NULL )
delete parser;
runParseTableGenerator( false );
parser = new ParserDriver( (*predictSetAnalyzer->getGrammar()), parseTableGenerator );
try{
parser->LLDriver( programFile, printOutput );
}
catch( SyntaxError e ){
std::cerr << "\n\n" << e.what();
}
catch( GrammarException e ){
std::cerr << "\n\nGrammar exception caught: " << e.what();
}
}
void UniversalCompiler::runCompiler( bool printOutput ){
if( compiler != NULL )
delete compiler;
runPredictSetAnalyzer( false );
runParseTableGenerator( false );
compiler = new LL1Compiler( (*predictSetAnalyzer->getGrammar()),
(*parseTableGenerator),
programFile,
compiledFile );
compiler->RunLL1Compiler( printOutput );
}
void UniversalCompiler::printFileToCout( std::fstream& file ){
std::string out;
file.clear();
file.seekg(0);
while( !file.eof() ){
getline(file, out);
std::cout << "\n" << out;
}
std::cout << "\n";
}