| @@ -0,0 +1,238 @@ |
| @@ -0,0 +1,238 @@ |
| |
1
| +// Parser for FlatZinc 1.1. |
| |
2
| +// Authors: Nick Nethercote |
| |
3
| +// Julien Fischer |
| |
4
| +// |
| |
5
| +// NOTE: the parser produced by the following grammar does not ensure |
| |
6
| +// that expressions are type correct. Further type-checking after parsing |
| |
7
| +// is required for this. |
| |
8
| +// |
| |
9
| +// This file is in the public domain, and can be used without copyright |
| |
10
| +// restrictions. |
| |
11
| + |
| |
12
| +%{ |
| |
13
| +#include <stdio.h> |
| |
14
| +#include <stdlib.h> |
| |
15
| +%} |
| |
16
| + |
| |
17
| + |
| |
18
| +// Possible values for attributed tokens. |
| |
19
| +%union { |
| |
20
| + char* string_val; |
| |
21
| + int int_val; |
| |
22
| + double float_val; |
| |
23
| +}; |
| |
24
| + |
| |
25
| +// Token kinds |
| |
26
| +%token <int_val> INT_LITERAL |
| |
27
| + <string_val> STRING_LITERAL IDENT UNDERSCORE_IDENT |
| |
28
| + <float_val> FLOAT_LITERAL |
| |
29
| + ARRAY BOOL CONSTRAINT FALSE FLOAT INT MAXIMIZE MINIMIZE OF |
| |
30
| + PREDICATE SATISFY SET SOLVE TRUE VAR DOTDOT COLONCOLON |
| |
31
| + |
| |
32
| +%% |
| |
33
| + |
| |
34
| +//--------------------------------------------------------------------------- |
| |
35
| +// Model top-level |
| |
36
| +//--------------------------------------------------------------------------- |
| |
37
| + |
| |
38
| +// Nb: these rules are left-recursive, which is good for Yacc as they run in |
| |
39
| +// constant stack space. Earlier versions were right-recursive, and this |
| |
40
| +// caused stack overflows on large models. The error recovery isn't great, |
| |
41
| +// but it's better than none. |
| |
42
| + |
| |
43
| +model : pred_decl_items var_decl_items constraint_items model_end |
| |
44
| + |
| |
45
| +pred_decl_items : pred_decl_items pred_decl_item ';' |
| |
46
| + | pred_decl_items error ';' { yyerrok; } |
| |
47
| + | /* empty */ |
| |
48
| + |
| |
49
| +var_decl_items : var_decl_items var_decl_item ';' |
| |
50
| + | /* empty */ |
| |
51
| + |
| |
52
| +constraint_items: constraint_items constraint_item ';' |
| |
53
| + | /* empty */ |
| |
54
| + |
| |
55
| +model_end : solve_item ';' |
| |
56
| + |
| |
57
| + |
| |
58
| +//--------------------------------------------------------------------------- |
| |
59
| +// Items |
| |
60
| +//--------------------------------------------------------------------------- |
| |
61
| + |
| |
62
| +pred_decl_item: |
| |
63
| + PREDICATE IDENT '(' pred_decl_args ')' |
| |
64
| + |
| |
65
| +var_decl_item: |
| |
66
| + VAR non_array_ti_expr_tail ':' ident_anns var_decl_item2 |
| |
67
| + | non_array_ti_expr_tail ':' ident_anns '=' expr |
| |
68
| + | ARRAY '[' INT_LITERAL DOTDOT INT_LITERAL ']' OF array_decl_tail |
| |
69
| + |
| |
70
| +var_decl_item2: |
| |
71
| + '=' expr |
| |
72
| + | /*empty*/ |
| |
73
| + |
| |
74
| +array_decl_tail: |
| |
75
| + non_array_ti_expr_tail ':' ident_anns '=' array_literal |
| |
76
| + | VAR non_array_ti_expr_tail ':' ident_anns array_decl_tail2 |
| |
77
| + |
| |
78
| +array_decl_tail2: |
| |
79
| + '=' array_literal |
| |
80
| + | /*empty*/ |
| |
81
| + |
| |
82
| +ident_anns: |
| |
83
| + IDENT annotations |
| |
84
| + | UNDERSCORE_IDENT annotations |
| |
85
| + |
| |
86
| +constraint_item: |
| |
87
| + CONSTRAINT constraint_elem annotations |
| |
88
| + |
| |
89
| +constraint_elem: |
| |
90
| + IDENT '(' exprs ')' |
| |
91
| + |
| |
92
| +solve_item: |
| |
93
| + SOLVE annotations solve_kind |
| |
94
| + |
| |
95
| +solve_kind: |
| |
96
| + SATISFY |
| |
97
| + | MINIMIZE expr |
| |
98
| + | MAXIMIZE expr |
| |
99
| + |
| |
100
| +//--------------------------------------------------------------------------- |
| |
101
| +// Predicate parameters |
| |
102
| +//--------------------------------------------------------------------------- |
| |
103
| + |
| |
104
| +pred_decl_args: |
| |
105
| + pred_decl_arg "," pred_decl_args |
| |
106
| + | pred_decl_arg |
| |
107
| + |
| |
108
| +pred_decl_arg: |
| |
109
| + non_array_ti_expr_tail ':' IDENT |
| |
110
| + | VAR non_array_ti_expr_tail ':' IDENT |
| |
111
| + | ARRAY '[' pred_arg_array_index ']' OF pred_arg_array_tail ':' IDENT |
| |
112
| + |
| |
113
| +pred_arg_array_index: |
| |
114
| + INT |
| |
115
| + | INT_LITERAL DOTDOT INT_LITERAL |
| |
116
| + |
| |
117
| +pred_arg_array_tail: |
| |
118
| + non_array_ti_expr_tail |
| |
119
| + | VAR non_array_ti_expr_tail |
| |
120
| + |
| |
121
| +//--------------------------------------------------------------------------- |
| |
122
| +// Type-Inst Expression Tails |
| |
123
| +//--------------------------------------------------------------------------- |
| |
124
| + |
| |
125
| +non_array_ti_expr_tail: |
| |
126
| + scalar_ti_expr_tail |
| |
127
| + | set_ti_expr_tail |
| |
128
| + |
| |
129
| +scalar_ti_expr_tail: |
| |
130
| + bool_ti_expr_tail |
| |
131
| + | int_ti_expr_tail |
| |
132
| + | float_ti_expr_tail |
| |
133
| + |
| |
134
| +bool_ti_expr_tail: |
| |
135
| + BOOL |
| |
136
| + |
| |
137
| +int_ti_expr_tail: |
| |
138
| + INT |
| |
139
| + | INT_LITERAL DOTDOT INT_LITERAL |
| |
140
| + | '{' int_literals '}' |
| |
141
| + |
| |
142
| +int_literals: |
| |
143
| + INT_LITERAL ',' int_literals |
| |
144
| + | INT_LITERAL |
| |
145
| + |
| |
146
| +float_ti_expr_tail: |
| |
147
| + FLOAT |
| |
148
| + | FLOAT_LITERAL DOTDOT FLOAT_LITERAL |
| |
149
| + |
| |
150
| +set_ti_expr_tail: |
| |
151
| + SET OF int_ti_expr_tail |
| |
152
| + |
| |
153
| +//--------------------------------------------------------------------------- |
| |
154
| +// Expressions |
| |
155
| +//--------------------------------------------------------------------------- |
| |
156
| + |
| |
157
| +exprs: |
| |
158
| + expr ',' exprs |
| |
159
| + | expr |
| |
160
| + |
| |
161
| +expr: |
| |
162
| + bool_literal |
| |
163
| + | INT_LITERAL |
| |
164
| + | FLOAT_LITERAL |
| |
165
| + | STRING_LITERAL |
| |
166
| + | set_literal |
| |
167
| + | array_literal |
| |
168
| + | array_access_expr |
| |
169
| + | IDENT |
| |
170
| + | UNDERSCORE_IDENT |
| |
171
| + | IDENT '(' exprs ')' /* An annotation value with > 0 arguments. */ |
| |
172
| + |
| |
173
| +bool_literal: FALSE | TRUE |
| |
174
| + |
| |
175
| +set_literal: |
| |
176
| + '{' exprs '}' |
| |
177
| + | '{' '}' |
| |
178
| + | INT_LITERAL DOTDOT INT_LITERAL |
| |
179
| + |
| |
180
| +array_literal: |
| |
181
| + '[' exprs ']' |
| |
182
| + | '[' ']' |
| |
183
| + |
| |
184
| +array_access_expr: IDENT '[' INT_LITERAL ']' |
| |
185
| + | UNDERSCORE_IDENT '[' INT_LITERAL ']' |
| |
186
| + |
| |
187
| +//--------------------------------------------------------------------------- |
| |
188
| +// Annotations |
| |
189
| +//--------------------------------------------------------------------------- |
| |
190
| + |
| |
191
| +annotations: |
| |
192
| + COLONCOLON expr annotations |
| |
193
| + | /* empty */ |
| |
194
| + |
| |
195
| +%% |
| |
196
| + |
| |
197
| +#include "lex.yy.c" |
| |
198
| + |
| |
199
| +char* filename; |
| |
200
| + |
| |
201
| +int main(int argc, char *argv[]) |
| |
202
| +{ |
| |
203
| + if (argc != 2) { |
| |
204
| + fprintf(stderr, "Usage: %s <file.fzn>\n", argv[0]); |
| |
205
| + exit(1); |
| |
206
| + } |
| |
207
| + |
| |
208
| + filename = argv[1]; |
| |
209
| + yyin = fopen(filename, "r"); |
| |
210
| + if (yyin == NULL) { |
| |
211
| + fprintf(stderr, "cannot open file: '%s'\n", filename); |
| |
212
| + exit(1); |
| |
213
| + } |
| |
214
| + |
| |
215
| + yyparse(); |
| |
216
| + return 0; |
| |
217
| +} |
| |
218
| + |
| |
219
| +int yyerror(char *s) |
| |
220
| +{ |
| |
221
| + if (0 == strcmp(yytext, "")) { |
| |
222
| + fprintf(stderr, |
| |
223
| + "%s:%d: %s before end of file\n", filename, yylineno, s); |
| |
224
| + } else { |
| |
225
| + fprintf(stderr, |
| |
226
| + "%s:%d: %s before '%s'\n", filename, yylineno, s, yytext); |
| |
227
| + } |
| |
228
| + return 0; |
| |
229
| +} |
| |
230
| + |
| |
231
| +/* |
| |
232
| +** This is only defined so the Flex library isn't needed. |
| |
233
| +*/ |
| |
234
| +int yywrap() |
| |
235
| +{ |
| |
236
| + return 1; |
| |
237
| +} |
| |
238
| + |