-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiimp.y
185 lines (142 loc) · 4.75 KB
/
iimp.y
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
%error-verbose
%{
/*
PRENOM: Serigne Amsatou
NOM: SEYE
1) Interpréteur du langage IMP: ./iimp --intIMP
- Le fichier iimp.l
- Le fichier iimp.y
- Le fichier interpreterIMP.c (la fonction interprete(...))
- Le module iimp.h
- Le module environment.h
- Le module y.tab.h
2) Interpréteur du langage C3A: ./interpreterC3A
- Le fichier interpreterC3A.l (la fonction interpretC3A(...))
- Le module environment.h
- Le module bilquad.h
3) Compilateur du langage IMP: ./iimp --compIMP
- Le fichier compilerIMP.l (la fonction compileIMP(...))
- Le fichier iimp.y
- Le module iimp.h
- Le module environment.h
- Le module bilquad.h
- Le module y.tab.h
4) Compilateur du langage C3A: ./compiilerC3A
- Le fichier compilerC3A.l (la fonction main(...))
- Le module environment.h
- Le module bilquad.h
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "iimp.h"
#include "environment.h"
nodeType *constant(int value);
nodeType *identifier(char* val);
nodeType *operation(int lexeme, int nbOper, ...);
char *choice;
int yylex();
int yyerror(const char *s);
char *strdup(const char *s);
void usage(void);
%}
%union{
int ival;
char* ident;
struct nodeTypeTag *nTPtr;
};
%token Af Sk Se If Th El Wh Do Pl Mo Mu /* liste des terminaux */
%token<ival> I
%token<ident> V
%nonassoc Th
%nonassoc El
%left Pl Mo
%left Mu
%left '('
%type<nTPtr> C0 C E T F
%%
/*
E = Expressoin
T = Terme
F = Facteur
*/
// Faire un test pour exécuter soit l'interpréteur soit le compilateur de IMP
program : C { Environment environment = (Environment)malloc(sizeof(struct sEnvironment));
if(strcmp(choice, "--intIMP") == 0)
executeINT(&environment, $1);
else if(strcmp(choice, "--compIMP") == 0)
executeCOM(&environment, $1);
else
usage();
};
C0 : V Af E {$$ = operation(Af, 2, identifier($1), $3); }
| Sk { $$ = operation(Sk, 2, NULL, NULL); }
| '(' C ')' { $$ = $2; }
| If E Th C El C0 { $$ = operation(If, 3, $2, $4, $6); }
| Wh E Do C0 { $$ = operation(Wh, 2, $2, $4); };
C : C0 { $$ = $1; }
| C Se C0 { $$ = operation(Se, 2, $1, $3); };
E : E Pl T { $$ = operation(Pl, 2, $1, $3); } /* Une expression peut-être sous la forme d'une somme d'une expression à un terme */
| E Mo T { $$ = operation(Mo, 2, $1, $3); } /* Une expression peut-être sous la forme d'une différence d'une expression d'un terme */
| T { $$ = $1; }; /* Une expressoin peut être simplement un terme */
T : T Mu F { $$ = operation(Mu, 2, $1, $3); } /* Un terme peut être un produit ou un quotien d'un terme et un facteur */
| F { $$ = $1; }; /* Un terme peut être tout simplement un facteur */
F : '(' E ')' { $$ = $2; } /* Un facteur peu être une expression mise entre parenthèse (i.e (E) ) */
| I { $$ = constant($1); } /* Un facteur peut être une suite de chiffres, non vide, commençant par un chiffre non-nul */
| V { $$ = identifier($1); }; /* Un facteur peut être une variable */
%%
nodeType *constant(int val){
nodeType *term;
/* Allocate node */
if((term = malloc(sizeof(nodeType))) == NULL)
yyerror("ERROR: Out of memory !\n");
/* Copy information */
term->type = typeCons;
term->cons.value = val;
return term;
}
nodeType *identifier(char* val){
nodeType *term;
/* Allocate node */
if((term = malloc(sizeof(nodeType))) == NULL)
yyerror("ERROR: Out of memory !\n");
/* Copy information */
term->type = typeIdent;
term->ident.value = val;
return term;
}
nodeType *operation(int lexeme, int nbOper, ...){
va_list args;
nodeType *term;
/* Allocate node, extending operN array */
if((term = malloc(sizeof(nodeType))) == NULL)
yyerror("ERROR: Out of memory !\n");
if((term->oper.operN = malloc((nbOper) * sizeof(nodeType *))) == NULL)
yyerror("ERROR: Out of memory !\n");
/* Copy information */
term->type = typeOper;
term->oper.operT = lexeme;
term->oper.nbOperN = nbOper; // Number of operands
va_start(args, nbOper); // Initialization from the last defined and parameter known
for(int i = 0; i < nbOper; i++)
term->oper.operN[i] = va_arg(args, nodeType*); //The next nodeType* parameter is accessed
va_end(args); // The end
return term;
}
int yyerror(const char *s){
fprintf( stderr, "%s\n", s );
return 0;
}
void usage(){
perror("\nUnknown command !\nUsage: ./iimp [--intIMP] | [--compIMP] < inputs/<source>*.imp\n");
}
int main(int argc, char **argv){
if(argc != 2){
usage();
return EXIT_FAILURE;
}
choice = strdup(argv[1]);
yyparse();
return 0;
}