-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascal_compiler.c
More file actions
114 lines (95 loc) · 2.72 KB
/
pascal_compiler.c
File metadata and controls
114 lines (95 loc) · 2.72 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
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include "tree.h"
#include "parsetree.h"
#include "symboltable.h"
#include "semantic_analyzer.h"
#include "code_generator.h"
#include "y.tab.h"
extern FILE * yyin;
extern int yyparse();
extern Tree * getOutputTree();
extern void setLexDebugMode(bool isDebugModeOn);
static char * inputPascalFileName = 0;
static FILE * outputAssemblyFile = 0;
static bool debugModeOn = false;
int main(int argc, char *argv[]){
outputAssemblyFile = stdout;
if(argc > 1) {
int i = 1;
while (i < argc) {
char * arg = argv[i];
if ( strcmp(arg,"-o") == 0 ) {
if ( outputAssemblyFile != stdout) {
fprintf(stderr,"Command Line Option \"-o\" is already specifies a file name for the output file\n");
exit (1);
}
if (argc == i) {
fprintf(stderr,"Command Line Option \"-o\" must have corresponding file name for the output file\n");
exit (1);
} else {
i++;
if( access( argv[i], F_OK ) != -1 ) {
if( access( argv[i], W_OK ) == -1 ) {
fprintf(stderr,"File \"%s\" does not exist or does not have proper permissions\n", argv[i]);
exit(1);
}
}
outputAssemblyFile = fopen (argv[i],"w");
}
} else if (strcmp(arg,"-d") == 0) {
debugModeOn = true;
setLexDebugMode(true);
} else {
if (inputPascalFileName) {
fprintf(stderr,"Extra Command Line Argument: \"%s\"\n", inputPascalFileName);
exit (1);
} else {
inputPascalFileName = argv[i];
if( access( inputPascalFileName, R_OK ) == -1 || access( inputPascalFileName, R_OK ) == -1 ) { // check file exists
fprintf(stderr, "File \"%s\" does not exist or does not have proper permissions\n", argv[i]);
exit(1);
}
}
}
i++;
}
}
if (inputPascalFileName) {
yyin = fopen( inputPascalFileName , "r" );
if(debugModeOn)
printf("\n\nSTART PARSING\n\n");
yyparse();
fclose( yyin );
} else {
if(debugModeOn)
printf("\n\nSTART PARSING\n\n");
yyparse();
}
Tree * tree = getOutputTree();
if(debugModeOn) {
printf("\n\nDONE PARSING\n\n");
printf("\n\nSYNTAX TREE:\n\n");
treePrint(tree);
printf("\n\nSTART ANALYZING SEMANTICS\n\n");
}
SymbolTable * symbolTable = analyzeSemantics(tree);
if(debugModeOn) {
printf("\n\nDONE ANALYZING\n\n");
printf("\n\nCLEAN TREE (WITH LABELING):\n\n");
treePrint(tree);
printf("\n\nSTART CODE GENERATION\n\n");
}
generateCode(outputAssemblyFile, tree, symbolTable);
if(debugModeOn) {
printf("\n\nEND CODE GENERATION\n\n");
printf("\n\nCLEAN TREE (WITH RE-LABELING):\n\n");
treePrint(tree);
}
if(debugModeOn)
printf("\n\nCOMPILATION SUCCESFUL\n");
// end program
return 0;
}