47d42f0d
Salvador Abreu
start moving towa...
|
1
2
3
4
5
6
7
|
// == Prolog AST-stream term Parser for FlatZinc ==============================
//
// (C) 2015 Salvador Abreu
//
// ----------------------------------------------------------------------------
// Based on the Parser for FlatZinc 1.1
// by Nick Nethercote and Julien Fischer
|
25820b21
Salvador Abreu
added flatzinc pa...
|
8
9
10
11
12
13
14
15
16
17
18
|
//
// NOTE: the parser produced by the following grammar does not ensure
// that expressions are type correct. Further type-checking after parsing
// is required for this.
//
// This file is in the public domain, and can be used without copyright
// restrictions.
%{
#include <stdio.h>
#include <stdlib.h>
|
47d42f0d
Salvador Abreu
start moving towa...
|
19
20
21
|
#include "types.h"
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
22
23
24
25
26
27
28
29
|
// -- AST stack macros: -------------------------------------------------------
// AST (POP, (PUSH))
// ASTT (POP, (PUSH), TRANS)
// INIT (STACK)
// PUSH (ITEM)
// CONS ()
// NIL ()
|
47d42f0d
Salvador Abreu
start moving towa...
|
30
31
|
#define AST(pop, push) \
{ \
|
1db63da8
Salvador Abreu
Prolog operator i...
|
32
|
printf ("[%s|_T] > [", pop); \
|
47d42f0d
Salvador Abreu
start moving towa...
|
33
|
printf push; \
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
34
35
36
|
printf ("|_T].\n"); \
}
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
37
38
39
40
41
42
43
44
45
|
#define ASTT(pop, push, trans) \
{ \
printf ("[%s|_T] > [", pop); \
printf push; \
printf ("|_T] :- "); \
printf trans; \
printf (".\n"); \
}
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
46
47
|
#define INIT(x) \
{ \
|
1db63da8
Salvador Abreu
Prolog operator i...
|
48
|
printf ("_ > %s.\n", x); \
|
47d42f0d
Salvador Abreu
start moving towa...
|
49
50
|
}
|
e1521486
Salvador Abreu
continue to devel...
|
51
52
|
#define PRE() \
{ \
|
1db63da8
Salvador Abreu
Prolog operator i...
|
53
|
printf ("_T > ["); \
|
e1521486
Salvador Abreu
continue to devel...
|
54
55
56
57
58
59
60
|
}
#define POST() \
{ \
printf ("|_T].\n"); \
}
|
47d42f0d
Salvador Abreu
start moving towa...
|
61
62
|
#define PUSH(x) \
{ \
|
1db63da8
Salvador Abreu
Prolog operator i...
|
63
|
printf ("_T > ["); \
|
47d42f0d
Salvador Abreu
start moving towa...
|
64
|
printf x; \
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
65
|
printf ("|_T].\n"); \
|
47d42f0d
Salvador Abreu
start moving towa...
|
66
67
|
}
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
68
69
|
// pop an item and a list, push new list (CONS)
#define CONS() AST ("H,T", ("[H|T]"))
|
47d42f0d
Salvador Abreu
start moving towa...
|
70
|
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
71
72
73
|
// pop a list and an item, push new list (RCONS)
#define RCONS() AST ("T,H", ("[H|T]"))
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
74
|
// push the empty list
|
47d42f0d
Salvador Abreu
start moving towa...
|
75
76
|
#define NIL() PUSH (("[]"))
|
25820b21
Salvador Abreu
added flatzinc pa...
|
77
78
79
80
81
|
%}
// Possible values for attributed tokens.
%union {
|
47d42f0d
Salvador Abreu
start moving towa...
|
82
|
UNION_DEFS ();
|
25820b21
Salvador Abreu
added flatzinc pa...
|
83
84
85
|
};
// Token kinds
|
47d42f0d
Salvador Abreu
start moving towa...
|
86
87
|
%token <lit> INT_LITERAL STRING_LITERAL FLOAT_LITERAL
<id> IDENT UNDERSCORE_IDENT
|
25820b21
Salvador Abreu
added flatzinc pa...
|
88
89
|
ARRAY BOOL CONSTRAINT FALSE FLOAT INT MAXIMIZE MINIMIZE OF
PREDICATE SATISFY SET SOLVE TRUE VAR DOTDOT COLONCOLON
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
90
91
92
|
%right ','
|
25820b21
Salvador Abreu
added flatzinc pa...
|
93
94
95
96
97
98
99
100
101
102
103
|
%%
//---------------------------------------------------------------------------
// Model top-level
//---------------------------------------------------------------------------
// Nb: these rules are left-recursive, which is good for Yacc as they run in
// constant stack space. Earlier versions were right-recursive, and this
// caused stack overflows on large models. The error recovery isn't great,
// but it's better than none.
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
104
105
106
107
108
|
model : { INIT ("[]"); }
pred_decl_items { AST ("X", ("preds(X)")); }
var_decl_items { AST ("X", ("vars(X)")); }
constraint_items { AST ("X", ("constrs(X)")); }
model_end
|
39c71c9f
Salvador Abreu
stack state comments
|
109
|
{ AST ("S,C,V,P", ("fzn(P, V, C, S)")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
110
|
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
111
112
113
|
pred_decl_items : pred_decl_items pred_decl_item ';' { CONS (); }
| pred_decl_items error ';' { yyerrok; }
| /* empty */ { NIL (); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
114
|
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
115
116
|
var_decl_items : var_decl_items var_decl_item ';' { CONS (); }
| /* empty */ { NIL (); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
117
|
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
118
119
|
constraint_items: constraint_items constraint_item ';' { CONS (); }
| /* empty */ { NIL (); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
120
121
122
123
124
125
126
127
128
129
130
|
model_end : solve_item ';'
//---------------------------------------------------------------------------
// Items
//---------------------------------------------------------------------------
pred_decl_item:
PREDICATE IDENT '(' pred_decl_args ')'
|
f7de4bd3
Salvador Abreu
var decl fix
|
131
|
var_decl_item: /* -> [ var(ID,TYPE,INIT,ANNOT) | _ ] */
|
25820b21
Salvador Abreu
added flatzinc pa...
|
132
|
VAR non_array_ti_expr_tail ':' ident_anns var_decl_item2
|
f7de4bd3
Salvador Abreu
var decl fix
|
133
|
{ AST ("VAL,AN,ID,T", ("var(ID, T, VAL, AN)")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
134
|
| non_array_ti_expr_tail ':' ident_anns '=' expr
|
f7de4bd3
Salvador Abreu
var decl fix
|
135
|
{ AST ("VAL,AN,ID,T", ("var(ID, T, VAL, AN)")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
136
|
| ARRAY '[' INT_LITERAL DOTDOT INT_LITERAL ']' OF array_decl_tail
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
137
|
{ AST ("VAL,AN,ID,T", ("var(ID, array(T,%d,%d), VAL, AN)", $3, $5)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
138
|
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
139
|
var_decl_item2: /* -> [ VAL | _ ] */
|
e2445a00
Salvador Abreu
progress on AST
|
140
141
|
'=' expr { }
| /*empty*/ { NIL (); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
142
|
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
143
|
array_decl_tail: /* -> [ TYPE,VAL,ANNOT,ID | _ ] */
|
25820b21
Salvador Abreu
added flatzinc pa...
|
144
145
146
|
non_array_ti_expr_tail ':' ident_anns '=' array_literal
| VAR non_array_ti_expr_tail ':' ident_anns array_decl_tail2
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
147
|
array_decl_tail2: /* -> [ VAL | _ ] */
|
e2445a00
Salvador Abreu
progress on AST
|
148
149
|
'=' array_literal { }
| /*empty*/ { NIL (); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
150
|
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
151
|
ident_anns: /* -> [ ANNOT,ID | _ ] */
|
e2445a00
Salvador Abreu
progress on AST
|
152
153
|
IDENT { PUSH (("'%s'", $1)); } annotations
| UNDERSCORE_IDENT { PUSH (("'%s'", $1)); } annotations
|
25820b21
Salvador Abreu
added flatzinc pa...
|
154
|
|
e2445a00
Salvador Abreu
progress on AST
|
155
|
constraint_item: /* -> [ constraint(...) | ] */
|
25820b21
Salvador Abreu
added flatzinc pa...
|
156
|
CONSTRAINT constraint_elem annotations
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
157
|
{ AST ("A,C", ("constraint(C,A)")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
158
|
|
e2445a00
Salvador Abreu
progress on AST
|
159
|
constraint_elem: /* -> [ CONSTR_ID, EXPRLIST | _ ] */
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
160
|
IDENT '(' exprs ')' { ASTT ("AL", ("C"), ("C =.. ['%s'|AL]", $1)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
161
|
|
39c71c9f
Salvador Abreu
stack state comments
|
162
163
|
solve_item: /* -> [ solve(S,A) | _ ] */
SOLVE annotations solve_kind { AST ("S,A", ("solve(S, A)")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
164
165
|
solve_kind:
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
166
167
168
|
SATISFY { PUSH (("satisfy")); }
| MINIMIZE expr { AST ("E", ("minimize(E)")); }
| MAXIMIZE expr { AST ("E", ("maximize(E)")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
169
170
171
172
173
174
175
176
177
178
179
|
//---------------------------------------------------------------------------
// Predicate parameters
//---------------------------------------------------------------------------
pred_decl_args:
pred_decl_arg "," pred_decl_args
| pred_decl_arg
pred_decl_arg:
non_array_ti_expr_tail ':' IDENT
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
180
|
{ AST ("T", ("T:'%s'", $3)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
181
|
| VAR non_array_ti_expr_tail ':' IDENT
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
182
|
{ AST ("T", ("var(T):'%s'", $4)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
183
|
| ARRAY '[' pred_arg_array_index ']' OF pred_arg_array_tail ':' IDENT
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
184
|
{ AST ("T,U,L", ("var(array(T,L,U)):'%s'", $8)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
185
|
|
39c71c9f
Salvador Abreu
stack state comments
|
186
|
pred_arg_array_index: /* -> [ UB, LB | _ ] */
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
187
188
|
INT { PUSH (("_,1")); }
| INT_LITERAL DOTDOT INT_LITERAL { PUSH (("%d,%d", $3, $1)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
189
|
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
190
191
192
|
pred_arg_array_tail: /* -> [ TYPE | _ ] */
non_array_ti_expr_tail { }
| VAR non_array_ti_expr_tail { AST ("T", ("var(T)")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
//---------------------------------------------------------------------------
// Type-Inst Expression Tails
//---------------------------------------------------------------------------
non_array_ti_expr_tail:
scalar_ti_expr_tail
| set_ti_expr_tail
scalar_ti_expr_tail:
bool_ti_expr_tail
| int_ti_expr_tail
| float_ti_expr_tail
bool_ti_expr_tail:
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
208
|
BOOL { PUSH (("bool")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
209
210
|
int_ti_expr_tail:
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
211
212
213
|
INT { PUSH (("int")); }
| INT_LITERAL DOTDOT INT_LITERAL { PUSH (("int(%d,%d)", $1, $3)); }
| '{' int_literals '}' { AST ("Ls", ("int(Ls)")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
214
215
|
int_literals:
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
216
|
INT_LITERAL ',' int_literals { AST ("Ls", ("[%d|Ls]", $1)); }
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
217
|
| INT_LITERAL { PUSH (("[%d]", $1)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
218
219
|
float_ti_expr_tail:
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
220
221
|
FLOAT { PUSH (("float")); }
| FLOAT_LITERAL DOTDOT FLOAT_LITERAL { PUSH (("float(%g,%g)", $1, $3)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
222
223
|
set_ti_expr_tail:
|
420a353a
Salvador Abreu
continue AST build
|
224
|
SET OF int_ti_expr_tail { AST ("T", ("set(T)")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
225
226
227
228
229
230
|
//---------------------------------------------------------------------------
// Expressions
//---------------------------------------------------------------------------
exprs:
|
6512088f
Salvador Abreu
expression lists ...
|
231
|
expr ',' exprs { RCONS (); }
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
232
|
| expr { AST ("E", ("[E]")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
233
234
235
|
expr:
bool_literal
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
236
237
238
|
| INT_LITERAL { PUSH (("lit(%d,int)", $1.ival)); }
| FLOAT_LITERAL { PUSH (("lit(%g,float)", $1.rval)); }
| STRING_LITERAL { PUSH (("lit(\"%s\",string)", $1.sval)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
239
240
241
|
| set_literal
| array_literal
| array_access_expr
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
242
243
|
| IDENT { PUSH (("id('%s')", $1)); }
| UNDERSCORE_IDENT { PUSH (("id('%s')", $1)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
244
245
|
| IDENT '(' exprs ')' /* An annotation value with > 0 arguments. */
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
246
|
bool_literal:
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
247
248
|
FALSE { PUSH (("lit(false,bool)")); }
| TRUE { PUSH (("lit(true,bool)")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
249
250
|
set_literal:
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
251
252
|
'{' exprs '}' { AST ("Es", ("lit(Es,set(_))")); }
| '{' '}' { PUSH (("lit([],set(_))")); }
|
e1521486
Salvador Abreu
continue to devel...
|
253
254
|
| INT_LITERAL DOTDOT INT_LITERAL { int i;
PRE();
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
255
|
printf ("lit([");
|
e1521486
Salvador Abreu
continue to devel...
|
256
257
|
for (i=$1.ival; i<$3.ival; ++i)
printf ("%d, ", i);
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
258
|
printf ("%d], set(int))", $3.ival);
|
e1521486
Salvador Abreu
continue to devel...
|
259
|
POST(); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
260
261
|
array_literal:
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
262
263
|
'[' exprs ']' { AST ("Es", ("lit(Es,array(_))")); }
| '[' ']' { PUSH (("lit([],array(_))")); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
264
|
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
265
266
|
array_access_expr:
IDENT '[' INT_LITERAL ']' { PUSH (("aref('%s',lit(%d,int),_)",$1,$3)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
267
|
| UNDERSCORE_IDENT '[' INT_LITERAL ']'
|
0b83a7f1
Salvador Abreu
First stab at sem...
|
268
|
{ PUSH (("aref('%s',lit(%d,int),_)",$1,$3)); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
269
270
271
272
273
274
|
//---------------------------------------------------------------------------
// Annotations
//---------------------------------------------------------------------------
annotations:
|
6e2f2ea4
Salvador Abreu
continue AST comp...
|
275
|
COLONCOLON expr annotations { AST ("As, E", ("[E|As]")); }
|
6dd9dd96
Salvador Abreu
continue prolog s...
|
276
|
| /* empty */ { NIL (); }
|
25820b21
Salvador Abreu
added flatzinc pa...
|
277
278
279
280
281
282
283
284
285
|
%%
#include "lex.yy.c"
char* filename;
int main(int argc, char *argv[])
{
|
39c71c9f
Salvador Abreu
stack state comments
|
286
287
288
289
|
if (argc == 1) {
yyin = stdin;
}
else if (argc == 2) {
|
25820b21
Salvador Abreu
added flatzinc pa...
|
290
291
292
|
filename = argv[1];
yyin = fopen(filename, "r");
if (yyin == NULL) {
|
39c71c9f
Salvador Abreu
stack state comments
|
293
294
|
fprintf(stderr, "cannot open file: '%s'\n", filename);
exit(1);
|
25820b21
Salvador Abreu
added flatzinc pa...
|
295
|
}
|
39c71c9f
Salvador Abreu
stack state comments
|
296
297
298
299
300
301
302
303
|
}
else {
fprintf(stderr, "Usage: %s [FILE.fzn]\n", argv[0]);
exit(1);
}
yyparse();
return 0;
|
25820b21
Salvador Abreu
added flatzinc pa...
|
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
}
int yyerror(char *s)
{
if (0 == strcmp(yytext, "")) {
fprintf(stderr,
"%s:%d: %s before end of file\n", filename, yylineno, s);
} else {
fprintf(stderr,
"%s:%d: %s before '%s'\n", filename, yylineno, s, yytext);
}
return 0;
}
/*
** This is only defined so the Flex library isn't needed.
*/
int yywrap()
{
return 1;
}
|