-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
113 lines (100 loc) · 2.27 KB
/
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
104
105
106
107
108
109
110
111
112
113
// Ernesto Vaz de Oliveira - 00302470
// Ricco Vasconcellos Constante Soares - 00307968
#define MAX_ASCII_CHAR_CODE 127
#include "y.tab.c"
#include "SymbolTable.h"
#include "AbstractSyntaxTree.h"
#include "Decompilation.h"
#include "SemanticAnalysis.h"
#include "IntermediaryCode.h"
#include "AssemblyGeneration.h"
#include <stdio.h>
int yylex();
extern char* yytext;
extern int lineCount;
extern SymbolTable* symbolTable;
extern FILE* yyin;
void initMe(void);
int getLineNumber(void);
int isRunning(void);
char* get_token_name(int char_code){
switch(char_code){
case 256:
return "KW_CHAR";
case 257:
return "KW_INT";
case 258:
return "KW_FLOAT";
case 261:
return "KW_IF";
case 263:
return "KW_ELSE";
case 264:
return "KW_WHILE";
case 267:
return "KW_READ";
case 268:
return "KW_PRINT";
case 269:
return "ASSIGNMENT";
case 270:
return "OPERATOR_LE";
case 271:
return "OPERATOR_GE";
case 272:
return "OPERATOR_EQ";
case 273:
return "OPERATOR_DIF";
case 280:
return "TK_IDENTIFIER";
case 281:
return "LIT_INTEGER";
case 282:
return "LIT_FLOAT";
case 285:
return "LIT_CHAR";
case 286:
return "LIT_STRING";
case 290:
return "TOKEN_ERROR";
}
return "FAULTY_TOKEN_CODE";
}
void pretty_print(int char_code){
if(char_code > MAX_ASCII_CHAR_CODE) {
char* token_name = get_token_name(char_code);
fprintf(stderr, "%s ", token_name);
}
else {
fprintf(stderr, "%c ", char_code);
}
}
int main(int argc, char** argv) {
char* filename = NULL;
if (argc >= 2) {
if (0==(yyin = fopen(argv[1],"r")))
{
printf("Cannot open file %s... \n",argv[1]);
exit(2);
}
if (argc == 3) filename = argv[2];
}
else{
exit(1);
}
initMe();
yyparse();
SyntaxTreeNode* syntaxTree = getSyntaxTreeRoot();
if(filename != NULL){
printSymbolTable(symbolTable);
printAST(syntaxTree, 0);
decompileAST(syntaxTree, filename);
}
analyzeSyntaxTree(syntaxTree);
fprintf(stderr, "Program successfully parsed!\n");
ThreeAddressCode* code = generateCode(syntaxTree, symbolTable);
printCodeList(code);
code = reverseCode(code);
generateAssembly(code, symbolTable);
exit(0);
}