-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
256 lines (229 loc) · 7.51 KB
/
parser.y
File metadata and controls
256 lines (229 loc) · 7.51 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
%{
#include <stdio.h>
#include <string.h>
#include "hash_table.h"
#include "bool.h"
#ifdef PAR_DEBUG
#define DEBUG_PRINT_PAR(x) printf(x)
#define DEBUG_PRINT_EXPRESSION(x) printExpression(x)
#define DEBUG_MODE 1
#else
#define DEBUG_PRINT_PAR(x)
#define DEBUG_PRINT_EXPRESSION(x)
#define DEBUG_MODE 0
#endif
int readExitCodeType(EXIT_CODE, Type, Type);
int readExitCodeVariables(EXIT_CODE, char *, Type, Type);
void yyerror (char const *);
extern int yylineno;
extern int yylex();
Table table;
%}
%union{
char * valString;
Type type;
Statement * statement;
ExpressionStatement * expr;
InitiationList * initlist;
}
/*union{
char * valString;
Type type;
Expression * expr;
}*/
%code requires {
#include "hash_table.h"
#include "ExpressionStatement.h"
#include "statement.h"
}
%locations
%token <valString> IDENTIFIER
%token IMPRIMIR
%token IFTOK THENTOK ELSETOK
%token ASSIGNTOK
%token <type> TYPETOK //Enum?
%token <type> ARRAYTYPETOK
%token EQUALS
%token ASSIGEQUALS
%token <valString> STRING
%token PLUSTOK MINUSTOK BYTOK DIVIDETOK OPPARTH CLOSPARTH OPARRAY CLOSARRAY
%token OPINITLIST CLOSINITLIST
%token ORTOK ANDTOK XORTOK SITOK SIITOK LESSTOK LESS_EQTOK MORETOK MORE_EQTOK NOT_EQTOK DOBLE_EQUALSTOK
%token <expr> ENTERO DOBLE CARACTER PROPOSICION
%token HAZ MIENTRAS PUNTO COMMA REPITE VECES
%type <expr> expression literal o p q r s t u v w x y z f g h i
%type <statement> varDecl arrayDecl arrayAccessorAssign varAssign sentence sentences while_sentence conditional repeat
%type <initlist> initList ilist
%start S
%% /* grammar */
S:
/* %empty */ {DEBUG_PRINT_PAR("Empty\n");}
| sentences {exec(table,$1);};
;
sentences:
sentence {DEBUG_PRINT_PAR("Sentence:\n"); $$ = $1;}
| sentence sentences {DEBUG_PRINT_PAR("Sentence:\n"); $$ = join($1,$2);}
sentence:
varDecl
| arrayDecl
| arrayAccessorAssign
| varAssign | conditional | while_sentence | repeat {$$ = $1;}
| IMPRIMIR expression {DEBUG_PRINT_PAR("Imprimir:\n");$$ = createPrint($2,yylineno);}
while_sentence: MIENTRAS expression HAZ sentences PUNTO {$$ = createWhile($2, $4,yylineno);}
repeat: REPITE expression VECES sentences PUNTO {$$ = createRepeat($2,$4,yylineno);}
varDecl:
IDENTIFIER ASSIGNTOK TYPETOK EQUALS expression {$$ = createDeclAsig($1,$3,$5,yylineno);}
| IDENTIFIER ASSIGNTOK TYPETOK {$$ = createDecl($1,$3,yylineno);}
| IDENTIFIER ASSIGEQUALS expression {$$ = createDeclAsig($1,UNKNOWN,$3,yylineno);}
| IDENTIFIER ASSIGNTOK {$$ = createDecl($1, UNKNOWN,yylineno);}
;
arrayDecl:
IDENTIFIER ASSIGNTOK ENTERO ARRAYTYPETOK EQUALS initList {$$ = createDeclAsigArray($1,$3,$4,$6,yylineno);}
| IDENTIFIER ASSIGNTOK ENTERO ARRAYTYPETOK {$$ = createDeclArray($1,$3,$4,yylineno);}
| IDENTIFIER ASSIGEQUALS initList {$$ = createDeclAsigArray($1,0,UNKNOWN,$3,yylineno);}
;
initList:
OPINITLIST ilist CLOSINITLIST { $$ = $2; testInitiationList($2);}
;
ilist:
expression { $$ = createInitiationList($1, NULL);}
| ilist COMMA { $$ = createInitiationList(NULL, $1);}
| ilist COMMA expression { $$ = createInitiationList($3, $1);}
;
varAssign: IDENTIFIER EQUALS expression { $$ = createAsig($1, $3,yylineno);}
;
arrayAccessorAssign: IDENTIFIER OPARRAY expression CLOSARRAY EQUALS expression { $$ = createArrayAccessorAsig($1,$3,$6,yylineno);}
;
conditional:
IFTOK expression THENTOK sentences PUNTO { $$ = createIf($2,$4,yylineno);}
| IFTOK expression THENTOK sentences ELSETOK sentences PUNTO {$$ = createIfElse($2,$4,$6,yylineno);}
expression: expression ORTOK o {$$ = createBinExpression(BIN_OR, $1, $3);}
| o {$$ = $1;}
;
o: o ANDTOK p {$$ = createBinExpression(BIN_AND, $1, $3);}
| p {$$ = $1;}
;
p: p XORTOK q {$$ = createBinExpression(BIN_XOR, $1, $3);}
| q {$$ = $1;}
;
q: q SITOK r { $$ = createBinExpression(BIN_SI, $1, $3);}
| r {$$ = $1;}
;
r: r SIITOK s {$$ = createBinExpression(BIN_SII, $1, $3);}
| s {$$ = $1;}
;
s: s LESSTOK t {$$ = createBinExpression(BIN_LESS, $1, $3);}
| t {$$ = $1;}
;
t: t LESS_EQTOK u {$$ = createBinExpression(BIN_LESS_EQ, $1, $3);}
| u {$$ = $1;}
;
u: u MORETOK v {$$ = createBinExpression(BIN_MORE, $1, $3);}
| v {$$ = $1;}
;
v: v MORE_EQTOK w {$$ = createBinExpression(BIN_MORE_EQ, $1, $3);}
| w {$$ = $1;}
;
w: w NOT_EQTOK x {$$ = createBinExpression(BIN_NOT_EQ, $1, $3);}
| x {$$ = $1;}
;
x: x DOBLE_EQUALSTOK y {$$ = createBinExpression(BIN_DOBLE_EQUALS, $1, $3);}
| y {$$ = $1;}
;
y: y PLUSTOK z {$$ = createBinExpression(BIN_PLUS, $1, $3);}
| z {$$ = $1;}
;
z: z MINUSTOK f {$$ = createBinExpression(BIN_MINUS, $1, $3);}
| f {$$ = $1;}
;
f: f BYTOK g {$$ = createBinExpression(BIN_BY, $1, $3);}
| g {$$ = $1;}
;
g: g DIVIDETOK h {$$ = createBinExpression(BIN_DIVIDE, $1, $3);}
| h {$$ = $1;}
;
h: MINUSTOK i {$$ = createUnExpression(UN_MINUS, $2);}
| i {$$ = $1;}
;
i: OPPARTH expression CLOSPARTH {$$ = $2;}
| literal {$$ = $1;}
| IDENTIFIER {$$ = createVariableExpression($1);}
| IDENTIFIER OPARRAY expression CLOSARRAY {$$ = createArrayAccessorExpression($1,$3);}
;
literal:
ENTERO | DOBLE | CARACTER | PROPOSICION {$$ = $1;}
//| STRING
;
%%
void yyerror (char const *message) { fprintf (stderr, "%s[%d]\n", message,yylineno);}
/*
int readExitCodeType(EXIT_CODE code, Type e1, Type e2){
int exitCode = 0;
char error[100];
switch(code){
case SUCCESS: DEBUG_PRINT_EXPRESSION(e);break;
case TYPE_NOT_EXISTS:
sprintf(error,"Error linea %d, el tipo %s no existe\n",yylineno ,strType[e1]);
yyerror(error); exitCode = 1; break;
case CONDITION_NOT_BOOL:
sprintf(error, "Error linea %d, operación condicional no definida para el tipo %s\n",yylineno ,strType[e1]);
yyerror(error); exitCode = 1; break;
case TYPE_ERROR:
sprintf(error, "Error linea %d, operación no definida para los tipos %s y %s\n",yylineno ,strType[e1], strType[e2]);
yyerror(error); exitCode = 1; break;
case TYPE_DOESNT_AGREE:
sprintf(error, "Error linea %d, los tipos no coinciden: %s, %s\n",yylineno ,strType[e1], strType[e2]);
yyerror(error); exitCode = 1; break;
default:
yyerror("ERROR DE LA MUERTE");
exitCode = 1;
}
return exitCode;
}
int readExitCodeVariables(EXIT_CODE code, char * var, Type exprType ,Type varType){
char error[100];
int exitCode = 0;
switch(code){
case SUCCESS:
if(DEBUG_MODE){
Expression * e;
valueOf(table, var, &e);
printf("Variable %s Type %s = ", var, strType[getType(e)]);
printExpression(e);
}
break;
case VAR_ALREADY_EXISTS_ERROR:
sprintf(error,"Error linea %d, la variable %s ya existe\n",yylineno,var);
yyerror(error); exitCode = 1; break;
case VAR_NOT_FOUND_ERROR:
sprintf(error, "Error linea %d, la variable %s no existe",yylineno,var);
yyerror(error); exitCode = 1; break;
default:
exitCode = readExitCodeType(code, exprType, varType);
}
return code;
}*/
int main(int argc, char ** argv) {
int ret;
extern FILE *yyin;
if(argc >= 2){
// open a file handle to a particular file:
FILE *myfile = fopen(argv[1], "r");
// make sure it's valid:
if (!myfile) {
fprintf (stderr, "No se pudo abrir el archivo\n");
return -1;
}
// set lex to read from it instead of defaulting to STDIN:
yyin = myfile;
// lex through the input:
table = createTable();
ret = yyparse();
fclose(yyin);
printTable(table);
return ret;
} else {
printf("Usage: cosa filename\n");
return 0;
}
}