-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsprint1.lex
executable file
·211 lines (157 loc) · 4.49 KB
/
sprint1.lex
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
%option nounput
%option noyywrap
%{
//{n,k} 2n+1 elem par bloc et k
// <- p -> : {n,1}
// |
// <- p -> : {n,2}
// |
//###################################
//####### DECLARATION C ###########
//###################################
/* fichier dans lequel est defini les macros constantes */
#include "arbre.h"
#include "table_symbole.h"
#include "generationCI.h"
#include "genAssembleur.h"
#include "sprint1.tab.h"
#include <stdio.h>
/*----------- Variables globales --------------*/
Arbre ast;
ListeDefine listedef;
Symbole sym_Table[TAILLE_TABLE];
//Liste chainé stockant les strings
ConstString string_const = NULL;
char* name_id;
int return_value = 0;
%}
/*###################################
/*####### DECLARATION LEX ###########
/*###################################
/* --------- SPRINT 1 --------- */
MAIN "int main"
NOMBRE [0-9]*
COMMENT (\/\/.*[\n])|\/\*([^\*]|\n|\*[^\/])*\*\/
STRING \"([^\"\\]|\\.)*\"
RETURN return
/* --------- SPRINT 2 --------- */
ID [a-zA-Z_][a-zA-Z0-9_]*
INT "int"
INCREMENTPLUS "++"
INCREMENTMOINS "--"
/* --------- SPRINT 3 --------- */
IF "if"
ELSE "else"
EQUAL "=="
SUPP ">"
INF "<"
SUPPEQU ">="
INFEQU "<="
DIFFERENCE "!="
NOT "!"
AND "&&"
OR "||"
/* --------- SPRINT 4 --------- */
WHILE "while"
FOR "for"
DEFINE "#define"
/* --------- SPRINT 6 --------- */
STENCIL "stencil"
/*###################################*/
/*####### REGLE SYNTAXIQUE ##########*/
/*###################################*/
%%
{DEFINE} {return DEFINE;}
{IF} { return IF; }
{ELSE} { return ELSE;}
{WHILE} {printf("debut while \n");return WHILE;}
{FOR} {printf("debut for \n");return FOR;}
{STENCIL} {return STENCIL;}
{INT} {return INT;}
{COMMENT} {printf(" Commentaire %s\n",yytext);}
{MAIN} {return MAIN;}
{RETURN} {return RETURN;}
{NOMBRE} {yylval.nombre=atoi(yytext); return NOMBRE;}
{STRING} {yylval.string=strdup(yytext); return STRING;}
printf {return PRINTF;}
printi {return PRINTI;}
[\{\}\(\)\[\]\;] {printf("Envoie de : %s \n",yytext);return yytext[0];}
= {return yytext[0];}
[+-/*$] {return yytext[0];}
{ID} {snprintf(name_id,1024,"%sVAR",yytext);yylval.string=strdup(name_id); return ID;}
{ID}{INCREMENTMOINS} {yytext[strlen(yytext)-2]='\0';
snprintf(name_id,1024,"%sVAR",yytext);yylval.string=strdup(name_id);
return INCREMENTMOINSAFTER;}
{INCREMENTMOINS}{ID} {snprintf(name_id,1024,"%sVAR",yytext+2);yylval.string=strdup(name_id);return INCREMENTMOINSBEFORE;}
{ID}{INCREMENTPLUS} {yytext[strlen(yytext)-2]='\0';
snprintf(name_id,1024,"%sVAR",yytext);yylval.string=strdup(name_id);
return INCREMENTPLUSAFTER;}
{INCREMENTPLUS}{ID} {snprintf(name_id,1024,"%sVAR",yytext+2);yylval.string=strdup(name_id);return INCREMENTPLUSBEFORE;}
{EQUAL} {return EQUAL;}
{SUPP} {return SUPP;}
{INF} {return INF;}
{SUPPEQU} {return SUPPEQU;}
{INFEQU} {return INFEQU;}
{DIFFERENCE} {return DIFFERENCE;}
{NOT} {return NOT;}
{AND} {return AND;}
{OR} {return OR;}
. {;}
%%
//###################################
//##### CODE ADDITIONNEL C #########
//###################################
// Free the memory allocated for Lex when we are done.
void lex_free() {
yy_delete_buffer(YY_CURRENT_BUFFER);
free(yy_buffer_stack);
}
int main(int argc, char **argv )
{
if(argc != 2){
printf("utilisation : %s file",argv[0]);
return 1;
}
name_id = malloc(1024);
// Ouverture du fichier
yyin = fopen(argv[1],"r");
if(yyin == NULL){
printf("Erreur ouverture du fichier %s\n",argv[1]);
return 1;
}
yyparse();
fclose(yyin);
free(name_id);
if(return_value)
return_value;
printf("\n########## DEFINE ##########\n\n");
print_define(listedef);
replaceDefineInAST(ast,listedef);
free_define(listedef);
printf("\n########## AST ##########\n\n");
repaceIdInAST(ast);
ast_print(ast);
if(ast_semantique(ast,sym_Table)){
printf("erreur semantique \n");
return 1;
}
// Generation de quad aprés remplissage de l'AST et des constantes String
quad code = genCode(ast,sym_Table);
ast_free(ast);
printf("\n########## QUADS ##########\n\n");
print_quad(code);
printf("\n########## MIPS ##########\n\n");
// Generation du code assembleur dans le fichier donné
char* filePath = strcat(argv[1],"_Assembly.s");
FILE* file = fopen(filePath,"w");
// 1.Header
genAssembleur_header(sym_Table,file);
// 2.Generation a partir des quads
genAssembleur(code,sym_Table,file);
// Liberation de la memoire utilise
quad_free(code);
sym_delete_table(sym_Table);
lex_free();
fclose(file);
return return_value;
}