-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert.y
299 lines (252 loc) · 9.52 KB
/
convert.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
Copyright 2008-2012 Ostap Cherkashin
Copyright 2008-2011 Julius Chrobak
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
%{
#include "config.h"
#include "string.h"
#include "system.h"
#include "memory.h"
extern void conv_error(const char *, ...);
extern int conv_lex();
extern char *cat(char *fmt, ...);
%}
%union {
char *val;
};
%token <val> TK_NAME
%token <val> TK_INT_VAL TK_LONG_VAL TK_REAL_VAL TK_STRING_VAL
%token <val> TK_REL TK_FN TK_RETURN
%token <val> TK_INT TK_LONG TK_REAL TK_STRING
%token <val> TK_PROJECT TK_RENAME TK_SELECT TK_EXTEND TK_SUMMARY
%token <val> TK_EQ TK_NEQ TK_AND TK_OR TK_LTE TK_GTE
%token <val> '-' '+' '*' '/' '<' '>' '{' '}' '(' ')' ']' '[' ':' ';' ',' '!' '='
%type <val> rel_decl relvar_decl func_decl
%type <val> func_name func_res func_params func_param func_body
%type <val> stmt_return stmt_assigns stmt_assign
%type <val> rel_attr rel_attrs rel_attrs_names
%type <val> project_attr project_attrs
%type <val> rename_attr rename_attrs
%type <val> extend_attr extend_attrs
%type <val> sum_attrs sum_attr
%type <val> rel_post_expr rel_prim_expr rel_mul_expr rel_expr
%type <val> prim_const_expr prim_simple_expr prim_top_expr prim_unary_expr
%type <val> prim_mul_expr prim_add_expr prim_bool_cmp_expr prim_expr
%type <val> sum_func
%type <val> rel_head
%%
program:
program rel_decl { sys_print("%s\n\n", $2); mem_free($2); }
| program relvar_decl { sys_print("%s\n\n", $2); mem_free($2); }
| program func_decl { sys_print("%s\n\n", $2); mem_free($2); }
|
;
rel_decl:
TK_REL TK_NAME rel_head { $$ = cat("type %s %s", $2, $3); }
;
rel_head:
'{' rel_attrs '}' { $$ = cat("%s%s%s", $1, $2, $3); }
| '{' rel_attrs ',' '}' { $$ = cat("%s%s%s", $1, $2, $4); }
;
rel_attrs:
rel_attr { $$ = cat("%s", $1); }
| rel_attrs ',' rel_attr { $$ = cat("%s%s %s", $1, $2, $3); }
;
rel_attrs_names:
TK_NAME { $$ = cat("%s", $1); }
| rel_attrs_names ',' TK_NAME { $$ = cat("%s %s", $1, $3); }
;
rel_attr:
rel_attrs_names ':' TK_INT { $$ = cat("%s %s", $1, $3); }
| rel_attrs_names ':' TK_LONG { $$ = cat("%s %s", $1, $3); }
| rel_attrs_names ':' TK_REAL { $$ = cat("%s %s", $1, $3); }
| rel_attrs_names ':' TK_STRING { $$ = cat("%s %s", $1, $3); }
;
relvar_decl:
TK_NAME ':' TK_NAME ';' { $$ = cat("var %s %s%s", $1, $3, $4); }
| TK_NAME ':' TK_REL rel_head ';' { $$ = cat("var %s %s%s", $1, $4, $5); }
;
func_decl:
TK_FN func_name '(' func_params ')' func_res '{' func_body '}'
{ $$ = cat("%s %s%s%s%s %s\n%s%s%s", $1, $2, $3, $4, $5, $6, $7, $8, $9); }
;
func_name:
TK_NAME { $$ = cat("%s", $1); }
;
func_res:
':' TK_NAME { $$ = cat("%s", $2); }
| ':' TK_REL rel_head { $$ = cat("%s", $3); }
| { $$ = cat("void"); }
;
func_params:
func_param { $$ = cat("%s", $1); }
| func_params ',' func_param { $$ = cat("%s%s %s", $1, $2, $3); }
| { $$ = cat(""); }
;
func_param:
rel_attr { $$ = cat("%s", $1); }
| rel_attrs_names ':' TK_NAME { $$ = cat("%s %s", $1, $3); }
| rel_attrs_names ':' TK_REL rel_head { $$ = cat("%s %s", $1, $4); }
;
func_body:
stmt_return { $$ = cat("\n%s\n", $1); }
| stmt_assigns { $$ = cat("\n%s\n", $1); }
| stmt_assigns stmt_return { $$ = cat("\n%s\n\n%s\n", $1, $2); }
| { $$ = cat(""); }
;
stmt_return:
TK_RETURN rel_expr ';' { $$ = cat("\t%s %s%s", $1, $2, $3); }
;
stmt_assigns:
stmt_assign { $$ = cat("%s", $1); }
| stmt_assigns stmt_assign { $$ = cat("%s\n%s", $1, $2); }
;
stmt_assign:
TK_NAME '=' rel_expr ';'
{ $$ = cat("\t%s %s %s%s", $1, $2, $3, $4); }
| TK_NAME '+' '=' rel_expr ';'
{ $$ = cat("\t%s %s%s %s%s", $1, $2, $3, $4, $5); }
| TK_NAME '-' '=' rel_expr ';'
{ $$ = cat("\t%s %s%s %s%s", $1, $2, $3, $4, $5); }
| TK_NAME '*' '=' rel_expr ';'
{ $$ = cat("\t%s %s%s %s%s", $1, $2, $3, $4, $5); }
| TK_NAME ':' '=' rel_expr ';'
{ $$ = cat("\tvar %s %s %s%s", $1, $3, $4, $5); }
;
rel_prim_expr:
TK_NAME { $$ = cat("%s", $1); }
| '(' rel_expr ')' { $$ = cat("%s", $2); }
;
rel_post_expr:
rel_prim_expr
{ $$ = cat("%s", $1); }
| rel_post_expr TK_PROJECT '(' project_attrs ')'
{ $$ = cat("(%s %s %s)", $2, $4, $1); }
| rel_post_expr TK_RENAME '(' rename_attrs ')'
{ $$ = cat("(%s %s %s)", $2, $4, $1); }
| rel_post_expr TK_SELECT '(' prim_expr ')'
{ $$ = cat("(%s %s %s)", $2, $4, $1); }
| rel_post_expr TK_EXTEND '(' extend_attrs ')'
{ $$ = cat("(%s %s %s)", $2, $4, $1); }
| rel_post_expr TK_SUMMARY '(' sum_attrs ')'
{ $$ = cat("(%s %s %s)", $2, $4, $1); }
| '(' rel_post_expr ',' rel_post_expr ')' TK_SUMMARY '(' sum_attrs ')'
{ $$ = cat("(%s %s %s %s)", $6, $8, $2, $4); }
;
rel_mul_expr:
rel_post_expr { $$ = cat("%s", $1); }
| rel_mul_expr '*' rel_post_expr { $$ = cat("(join %s %s)", $1, $3); }
;
rel_expr:
rel_mul_expr { $$ = cat("%s", $1); }
| rel_expr '+' rel_mul_expr { $$ = cat("(union %s %s)", $1, $3); }
| rel_expr '-' rel_mul_expr { $$ = cat("(minus %s %s)", $1, $3); }
;
project_attrs:
project_attr { $$ = cat("%s", $1); }
| project_attrs ',' project_attr { $$ = cat("%s%s %s", $1, $2, $3); }
;
project_attr:
TK_NAME { $$ = cat("%s", $1); }
;
rename_attrs:
rename_attr { $$ = cat("%s", $1); }
| rename_attrs ',' rename_attr { $$ = cat("%s%s %s", $1, $2, $3); }
;
rename_attr:
TK_NAME '=' TK_NAME { $$ = cat("%s %s %s", $1, $2, $3); }
;
extend_attrs:
extend_attr { $$ = cat("%s", $1); }
| extend_attrs ',' extend_attr { $$ = cat("%s%s %s", $1, $2, $3); }
;
extend_attr:
TK_NAME '=' prim_expr { $$ = cat("%s %s %s", $1, $2, $3); }
;
sum_attrs:
sum_attr { $$ = cat("%s", $1); }
| sum_attrs ',' sum_attr { $$ = cat("%s%s %s", $1, $2, $3); }
;
sum_attr:
TK_NAME '=' sum_func { $$ = cat("%s %s %s", $1, $2, $3); }
;
sum_func:
TK_NAME '(' ')'
{ $$ = cat("%s", $1); }
| TK_NAME '(' TK_NAME ',' prim_expr ')'
{ $$ = cat("%s%s %s %s%s", $2, $1, $3, $5, $6); }
;
prim_const_expr:
TK_INT_VAL { $$ = cat("%s", $1); }
| TK_LONG_VAL { $$ = cat("%s", $1); }
| TK_REAL_VAL { $$ = cat("%s", $1); }
| TK_STRING_VAL { $$ = cat("%s", $1); }
;
prim_simple_expr:
prim_const_expr
{ $$ = cat("%s", $1); }
| '(' prim_expr ')'
{ $$ = cat("%s%s%s", $1, $2, $3); }
| TK_INT '(' prim_expr ')'
{ $$ = cat("%s%s %s%s", $2, $1, $3, $4); }
| TK_REAL '(' prim_expr ')'
{ $$ = cat("%s%s %s%s", $2, $1, $3, $4); }
| TK_LONG '(' prim_expr ')'
{ $$ = cat("%s%s %s%s", $2, $1, $3, $4); }
| TK_NAME '(' prim_expr ')'
{ $$ = cat("%s%s %s%s", $2, $1, $3, $4); }
;
prim_top_expr:
prim_simple_expr { $$ = cat("%s", $1); }
| TK_NAME { $$ = cat("%s", $1); }
;
prim_unary_expr:
prim_top_expr { $$ = cat("%s", $1); }
| '!' prim_unary_expr { $$ = cat("%s%s", $1, $2); }
| '-' prim_simple_expr { $$ = cat("%s%s", $1, $2); }
| '+' prim_simple_expr { $$ = cat("%s%s", $1, $2); }
;
prim_mul_expr:
prim_unary_expr { $$ = cat("%s", $1); }
| prim_mul_expr '*' prim_unary_expr { $$ = cat("%s %s %s", $1, $2, $3); }
| prim_mul_expr '/' prim_unary_expr { $$ = cat("%s %s %s", $1, $2, $3); }
;
prim_add_expr:
prim_mul_expr { $$ = cat("%s", $1); }
| prim_add_expr '+' prim_mul_expr { $$ = cat("%s %s %s", $1, $2, $3); }
| prim_add_expr '-' prim_mul_expr { $$ = cat("%s %s %s", $1, $2, $3); }
;
prim_bool_cmp_expr:
prim_add_expr
{ $$ = cat("%s", $1); }
| prim_bool_cmp_expr TK_EQ prim_add_expr
{ $$ = cat("%s %s %s", $1, $2, $3); }
| prim_bool_cmp_expr TK_NEQ prim_add_expr
{ $$ = cat("%s %s %s", $1, $2, $3); }
| prim_bool_cmp_expr '>' prim_add_expr
{ $$ = cat("%s %s %s", $1, $2, $3); }
| prim_bool_cmp_expr '<' prim_add_expr
{ $$ = cat("%s %s %s", $1, $2, $3); }
| prim_bool_cmp_expr TK_LTE prim_add_expr
{ $$ = cat("%s %s %s", $1, $2, $3); }
| prim_bool_cmp_expr TK_GTE prim_add_expr
{ $$ = cat("%s %s %s", $1, $2, $3); }
;
prim_expr:
prim_bool_cmp_expr
{ $$ = cat("%s", $1); }
| prim_expr TK_AND prim_bool_cmp_expr
{ $$ = cat("%s %s %s", $1, $2, $3); }
| prim_expr TK_OR prim_bool_cmp_expr
{ $$ = cat("%s %s %s", $1, $2, $3); }
;
%%