-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuccompiler.l
169 lines (156 loc) · 9.11 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
/*
João Luís Gaspar Lopes
Rúben Telmo Domingues Leal
*/
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sym_table.h"
#include "code_gen.h"
#include "y.tab.h"
int line = 1, column = 1;
int comment_line, comment_column;
int flag_lexical = 0, flag_syntax = 0, flag_error = 0, flag_tree = 0;
%}
%X MULTICOMMENT
CHAR "char"
ELSE "else"
WHILE "while"
IF "if"
INT "int"
SHORT "short"
DOUBLE "double"
RETURN "return"
VOID "void"
AND "&&"
OR "||"
BITWISEAND "&"
BITWISEOR "|"
BITWISEXOR "^"
MUL "*"
COMMA ","
DIV "/"
EQ "=="
NE "!="
GE ">="
GT ">"
LE "<="
LT "<"
ASSIGN "="
NOT "!"
LBRACE "{"
LPAR "("
RBRACE "}"
RPAR ")"
MINUS "-"
PLUS "+"
MOD "%"
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"|"["|"]"|"--"|"++"
ID ([a-zA-Z_])([a-zA-Z0-9_])*
INTLIT [0-9]+
CHRLIT \'([^\n\r\'\\]|(\\(n|t|\\|\'|\"|[0-7]{1,3})))\'
INVCHRLIT \'([^\n\r\'\\]*|(\\[^\n\r]))*\'
UNTCHRLIT \'([^\n\r\'\\]|\\([^\n\r]))*(\\)?
REALLIT (([0-9]+)"."([0-9]+)?([eE][+-]?[0-9]+)?)|"."[0-9]+([eE][+-]?[0-9]+)?|[0-9]+([eE][+-]?[0-9]+)
/*...definições...*/
%%
"/*" {BEGIN MULTICOMMENT; comment_line = line; comment_column = column; column+=yyleng;}
<MULTICOMMENT>\n|\r|\r\n {line++; column = 1;}
<MULTICOMMENT><<EOF>> {printf("Line %d, col %d: unterminated comment\n", comment_line, comment_column); column++; BEGIN 0;}
<MULTICOMMENT>"*/" {column+=yyleng; BEGIN 0;}
<MULTICOMMENT>. {column+=yyleng;}
\n|\r|\r\n {line++; column=1;}
\t|" " {column++;}
"//"[^\n\r]* {column+=yyleng;}
<<EOF>> {column++; return 0;}
{RESERVED} {if(flag_lexical != 0){printf("RESERVED(%s)\n",yytext);} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return RESERVED;};}
{CHAR} {if(flag_lexical != 0){printf("CHAR\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return CHAR;};}
{ELSE} {if(flag_lexical != 0){printf("ELSE\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return ELSE;};}
{WHILE} {if(flag_lexical != 0){printf("WHILE\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return WHILE;};}
{IF} {if(flag_lexical != 0){printf("IF\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return IF;};}
{INT} {if(flag_lexical != 0){printf("INT\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return INT;};}
{SHORT} {if(flag_lexical != 0){printf("SHORT\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return SHORT;};}
{DOUBLE} {if(flag_lexical != 0){printf("DOUBLE\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return DOUBLE;};}
{RETURN} {if(flag_lexical != 0){printf("RETURN\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return RETURN;};}
{VOID} {if(flag_lexical != 0){printf("VOID\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return VOID;};}
{BITWISEAND} {if(flag_lexical != 0){printf("BITWISEAND\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return BITWISEAND;};}
{BITWISEOR} {if(flag_lexical != 0){printf("BITWISEOR\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return BITWISEOR;};}
{BITWISEXOR} {if(flag_lexical != 0){printf("BITWISEXOR\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return BITWISEXOR;};}
{AND} {if(flag_lexical != 0){printf("AND\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return AND;};}
{OR} {if(flag_lexical != 0){printf("OR\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return OR;};}
{ASSIGN} {if(flag_lexical != 0){printf("ASSIGN\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return ASSIGN;};}
{NOT} {if(flag_lexical != 0){printf("NOT\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return NOT;};}
{MUL} {if(flag_lexical != 0){printf("MUL\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return MUL;};}
{COMMA} {if(flag_lexical != 0){printf("COMMA\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return COMMA;};}
{DIV} {if(flag_lexical != 0){printf("DIV\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return DIV;};}
{EQ} {if(flag_lexical != 0){printf("EQ\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return EQ;};}
{NE} {if(flag_lexical != 0){printf("NE\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return NE;};}
{GE} {if(flag_lexical != 0){printf("GE\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return GE;};}
{GT} {if(flag_lexical != 0){printf("GT\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return GT;};}
{LE} {if(flag_lexical != 0){printf("LE\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return LE;};}
{LT} {if(flag_lexical != 0){printf("LT\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return LT;};}
{LBRACE} {if(flag_lexical != 0){printf("LBRACE\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return LBRACE;};}
{LPAR} {if(flag_lexical != 0){printf("LPAR\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return LPAR;};}
{RBRACE} {if(flag_lexical != 0){printf("RBRACE\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return RBRACE;};}
{RPAR} {if(flag_lexical != 0){printf("RPAR\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return RPAR;};}
{MINUS} {if(flag_lexical != 0){printf("MINUS\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return MINUS;};}
{PLUS} {if(flag_lexical != 0){printf("PLUS\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return PLUS;};}
{MOD} {if(flag_lexical != 0){printf("MOD\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return MOD;};}
{SEMI} {if(flag_lexical != 0){printf("SEMI\n");} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return SEMI;};}
{CHRLIT} {if(flag_lexical != 0){printf("CHRLIT(%s)\n",yytext);} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return CHRLIT;};}
{UNTCHRLIT} {printf("Line %d, col %d: unterminated char constant\n",line,column); column+=yyleng;}
{INVCHRLIT} {printf("Line %d, col %d: invalid char constant (%s)\n",line,column,yytext); column+=yyleng;}
{INTLIT} {if(flag_lexical != 0){printf("INTLIT(%s)\n",yytext);} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return INTLIT;};}
{ID} {if(flag_lexical != 0){printf("ID(%s)\n",yytext);} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return ID;};}
{REALLIT} {if(flag_lexical != 0){printf("REALLIT(%s)\n",yytext);} column+=yyleng; if(flag_syntax != 0){yylval.token = create_token(line, column, yytext); return REALLIT;};}
. {printf("Line %d, col %d: illegal character (%c)\n",line,column,yytext[0]); column+=yyleng;}
%%
int main(int argc, char **argv){
if (argc > 1){
if(strcmp(argv[1], "-l") == 0 || strcmp(argv[1], "-1") == 0) {
flag_lexical = 1;
yylex();
} else if(strcmp(argv[1], "-1") == 0) {
yylex();
} else if(strcmp(argv[1], "-t") == 0) {
flag_tree = 1;
flag_syntax = 1;
yyparse();
} else if(strcmp(argv[1], "-2") == 0) {
flag_syntax = 1;
yyparse();
} else if(strcmp(argv[1], "-s") == 0) {
flag_syntax = 1;
yyparse();
if(!flag_error){
Symbol_Table *global = create_default_table();
parse_table(root, global);
print_all_tables(global);
print_annotated_tree(root, 0);
}
}
} else {
flag_syntax = 1;
yyparse();
global = create_default_table();
parse_table(root, global);
if(!flag_error){
//LLVM IR GENERATED HERE
code_gen(root);
}
}
if(!flag_error && flag_tree){
print_tree(root, 0);
}
clear(root);
return 0;
}
int yywrap(){
return 1;
}
void yyerror(char *s){
flag_error = 1;
printf("Line %d, col %d: %s: %s\n", line, (int)(column-yyleng), s, yytext);
}