-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuccompiler.l
executable file
·208 lines (191 loc) · 6 KB
/
uccompiler.l
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
%{
//Miguel Machado 2014214547
//Teresa Salazar 2015234237
#include "y.tab.h"
#define PRINTF if(print_tokens == 1) printf
#include "ast.h"
#include "symbol_table.h"
#include "semantics.h"
#include "generate.h"
int line = 1;
int col = 1;
int beginning_line;
int beginning_col;
int print_tokens;
int errors = 0;
%}
%X COMMENT
%X COMMENT2
CHAR char
ELSE else
WHILE while
IF if
INT int
SHORT short
DOUBLE double
RETURN return
VOID void
BITWISEAND "&"
BITWISEOR "|"
BITWISEXOR "^"
AND "&&"
ASSIGN "="
MUL "*"
COMMA ","
DIV "/"
EQ "=="
GE ">="
GT ">"
LBRACE "{"
LE "<="
LPAR "("
LT "<"
MINUS "-"
MOD "%"
NE "!="
NOT "!"
OR "||"
PLUS "+"
RBRACE "}"
RPAR ")"
SEMI ";"
RESERVED "auto"|"break"|"case"|"const"|"continue"|"default"|"do"|"enum"|"extern"|"float"|"for"|"goto"|"inline"|"long"|"register"|"restrict"|"signed"|"sizeof"|"static"|"struct"|"switch"|"typedef"|"union"|"unsigned"|"volatile"|"_Bool"|"_Complex"|"_Imaginary"|"["|"]"|"++"|"--"
NUMBER [0-9]
LETTER [a-zA-Z]
ANYCHAR [^'\n\\]
ESCAPE \\(n|t|\\|\'|\"|([0-7]{1,3}))
EXPOENTE (E|e)(\+|\-)*{NUMBER}+
ID ({LETTER}|_)({LETTER}|{NUMBER}|_)*
INTLIT {NUMBER}+
CHRLIT \'{ANYCHAR}\'|\'{ESCAPE}\'
REALLIT ({NUMBER}+\.{NUMBER}*{EXPOENTE}*)|(\.{NUMBER}+{EXPOENTE}*)|({NUMBER}+{EXPOENTE})
CHRLIT_INV \'(\\.|{ANYCHAR})*\'
CHRLIT_UNT \'({ANYCHAR}|{ESCAPE})*(\\)*{ANYCHAR}*
%%
{CHAR} {PRINTF("CHAR\n"); col += yyleng; return CHAR;}
{ELSE} {PRINTF("ELSE\n"); col += yyleng; return ELSE;}
{WHILE} {PRINTF("WHILE\n"); col += yyleng; return WHILE;}
{IF} {PRINTF("IF\n"); col += yyleng; return IF;}
{INT} {PRINTF("INT\n"); col += yyleng; return INT;}
{SHORT} {PRINTF("SHORT\n"); col += yyleng; return SHORT;}
{DOUBLE} {PRINTF("DOUBLE\n"); col += yyleng; return DOUBLE;}
{RETURN} {PRINTF("RETURN\n"); col += yyleng; return RETURN;}
{VOID} {PRINTF("VOID\n"); col += yyleng; return VOID;}
{BITWISEAND} {PRINTF("BITWISEAND\n"); col += yyleng; return BITWISEAND;}
{BITWISEOR} {PRINTF("BITWISEOR\n"); col += yyleng; return BITWISEOR;}
{BITWISEXOR} {PRINTF("BITWISEXOR\n"); col += yyleng; return BITWISEXOR;}
{AND} {PRINTF("AND\n"); col += yyleng; return AND;}
{ASSIGN} {PRINTF("ASSIGN\n"); col += yyleng; return ASSIGN;}
{MUL} {PRINTF("MUL\n"); col += yyleng; return MUL;}
{COMMA} {PRINTF("COMMA\n"); col += yyleng; return COMMA;}
{DIV} {PRINTF("DIV\n"); col += yyleng; return DIV;}
{EQ} {PRINTF("EQ\n"); col += yyleng; return EQ;}
{GE} {PRINTF("GE\n"); col += yyleng; return GE;}
{GT} {PRINTF("GT\n"); col += yyleng; return GT;}
{LBRACE} {PRINTF("LBRACE\n"); col += yyleng; return LBRACE;}
{LE} {PRINTF("LE\n"); col += yyleng; return LE;}
{LPAR} {PRINTF("LPAR\n"); col += yyleng; return LPAR;}
{LT} {PRINTF("LT\n"); col += yyleng; return LT;}
{MINUS} {PRINTF("MINUS\n"); col += yyleng; return MINUS;}
{MOD} {PRINTF("MOD\n"); col += yyleng; return MOD;}
{NE} {PRINTF("NE\n"); col += yyleng; return NE;}
{NOT} {PRINTF("NOT\n"); col += yyleng; return NOT;}
{OR} {PRINTF("OR\n"); col += yyleng; return OR;}
{PLUS} {PRINTF("PLUS\n"); col += yyleng; return PLUS;}
{RBRACE} {PRINTF("RBRACE\n"); col += yyleng; return RBRACE;}
{RPAR} {PRINTF("RPAR\n"); col += yyleng; return RPAR;}
{SEMI} {PRINTF("SEMI\n"); col += yyleng; return SEMI;}
{RESERVED} {PRINTF("RESERVED(%s)\n", yytext); col += yyleng; return RESERVED;}
{INTLIT} {PRINTF("INTLIT(%s)\n", yytext); col += yyleng; yylval.value = strdup(yytext); return INTLIT;}
{ID} {PRINTF("ID(%s)\n", yytext); col += yyleng; yylval.value = strdup(yytext); return ID;}
{CHRLIT} {PRINTF("CHRLIT(%s)\n", yytext); col += yyleng; yylval.value = strdup(yytext); return CHRLIT;}
{REALLIT} {PRINTF("REALLIT(%s)\n", yytext); col += yyleng; yylval.value = strdup(yytext); return REALLIT;}
{CHRLIT_INV} {printf("Line %d, col %d: invalid char constant (%s)\n", line, col, yytext); col+=yyleng; return CHRLIT_INV;}
{CHRLIT_UNT} {printf("Line %d, col %d: unterminated char constant\n", line, col); col+=yyleng; return CHRLIT_UNT;}
\n {line++; col=1;}
\r {col+=yyleng;}
[ \t] {col+=yyleng;}
. {printf("Line %d, col %d: illegal character (%s)\n", line, col, yytext); col+=yyleng;}
"/*" {BEGIN COMMENT; beginning_col = col; beginning_line = line; col+=yyleng;}
<COMMENT><<EOF>> {BEGIN 0; printf("Line %d, col %d: unterminated comment\n", beginning_line, beginning_col); col+=yyleng;}
<COMMENT>"*/" {BEGIN 0; col+=yyleng;}
<COMMENT>. {col+=yyleng;}
<COMMENT>\n {line++; col=1;}
"//" {BEGIN COMMENT2;}
<COMMENT2>"\n" {BEGIN 0; line++; col=1;}
<COMMENT2>. {;}
%%
int main(int argc, char *argv[]) {
if (argc > 1) {
if (strcmp(argv[1], "-1") == 0) {
print_tokens = 0;
yyparse();
yylex();
}
if (strcmp(argv[1], "-l") == 0) {
print_tokens = 1;
yyparse();
yylex();
}
else if (strcmp(argv[1], "-2") == 0) {
print_tokens = 0;
yyparse();
destroy_ast(ast);
}
else if (strcmp(argv[1], "-t") == 0) {
print_tokens = 0;
yyparse();
if(errors == 0) {
print_ast(ast, 0);
destroy_ast(ast);
}
}
else if (strcmp(argv[1], "-3") == 0) {
print_tokens = 0;
yyparse();
if(errors == 0) {
current = create_table("Global", 1);
current->print = 1;
insert_default_functions(current);
check_program(ast);
destroy_ast(ast);
}
}
else if (strcmp(argv[1], "-s") == 0) {
print_tokens = 0;
yyparse();
if(errors == 0) {
current = create_table("Global", 1);
current->print = 1;
insert_default_functions(current);
check_program(ast);
show_tables();
print_ast(ast, 0);
destroy_tables(tables);
destroy_ast(ast);
}
}
}
else {
print_tokens = 0;
yyparse();
if(errors == 0) {
current = create_table("Global", 1);
current->print = 1;
insert_default_functions(current);
check_program(ast);
generate_code(ast);
destroy_tables(tables);
destroy_ast(ast);
}
}
return 0;
}
int yywrap() {
return 1;
}
void yyerror (char *s) {
errors = 1;
int aux_col = col - strlen(yytext);
printf("Line %d, col %d: %s: %s\n", line, aux_col, s, yytext);
}