Commit 9883d14cb9d158c95d435e1ffc706e1452461f50

Authored by Salvador Abreu
1 parent d8279a47
Exists in master

continue prolog stack AST build

fzn-parser/README 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +AST nodes for fzn parser
  2 +
  3 +lit(TYPE, VALUE)
  4 +
  5 +TYPE = bool
fzn-parser/flatzinc.l
1 -/* Lexer for FlatZinc. 1 +/* == Prolog AST-stream term Scanner for FlatZinc =============================
  2 + *
  3 + * (C) 2015 Salvador Abreu
  4 + *
  5 + * ----------------------------------------------------------------------------
  6 + * Based on the Lexer for FlatZinc.
2 * Nick Nethercote, started May 2007. 7 * Nick Nethercote, started May 2007.
3 * 8 *
4 * This file is in the public domain, and can be used without copyright 9 * This file is in the public domain, and can be used without copyright
@@ -40,15 +45,15 @@ float_literal -?[0-9]+\.[0-9]+|-?[0-9]+\.[0-9]+[Ee][-+]?[0-9]+|-?[0-9]+[Ee][- @@ -40,15 +45,15 @@ float_literal -?[0-9]+\.[0-9]+|-?[0-9]+\.[0-9]+[Ee][-+]?[0-9]+|-?[0-9]+[Ee][-
40 45
41 /* Attributed tokens */ 46 /* Attributed tokens */
42 {ident} { 47 {ident} {
43 - yylval.string_val = strdup(yytext); 48 + yylval.id = strdup(yytext);
44 return IDENT; 49 return IDENT;
45 } 50 }
46 {underscore_ident} { 51 {underscore_ident} {
47 - yylval.string_val = strdup(yytext); 52 + yylval.id = strdup(yytext);
48 return UNDERSCORE_IDENT; 53 return UNDERSCORE_IDENT;
49 } 54 }
50 {string_literal} { 55 {string_literal} {
51 - yylval.string_val = strdup(yytext); 56 + yylval.lit.sval = strdup(yytext);
52 return STRING_LITERAL; 57 return STRING_LITERAL;
53 } 58 }
54 {int_literal} { 59 {int_literal} {
@@ -77,7 +82,7 @@ float_literal -?[0-9]+\.[0-9]+|-?[0-9]+\.[0-9]+[Ee][-+]?[0-9]+|-?[0-9]+[Ee][- @@ -77,7 +82,7 @@ float_literal -?[0-9]+\.[0-9]+|-?[0-9]+\.[0-9]+[Ee][-+]?[0-9]+|-?[0-9]+[Ee][-
77 } 82 }
78 i++; 83 i++;
79 } 84 }
80 - yylval.int_val = x; 85 + yylval.lit.ival = x;
81 86
82 } else if ('0' == yytext[0] && 'o' == yytext[1]) { 87 } else if ('0' == yytext[0] && 'o' == yytext[1]) {
83 int i = 2, x = 0; 88 int i = 2, x = 0;
@@ -86,15 +91,15 @@ float_literal -?[0-9]+\.[0-9]+|-?[0-9]+\.[0-9]+[Ee][-+]?[0-9]+|-?[0-9]+[Ee][- @@ -86,15 +91,15 @@ float_literal -?[0-9]+\.[0-9]+|-?[0-9]+\.[0-9]+[Ee][-+]?[0-9]+|-?[0-9]+[Ee][-
86 x += (yytext[i] - '0'); 91 x += (yytext[i] - '0');
87 i++; 92 i++;
88 } 93 }
89 - yylval.int_val = x; 94 + yylval.lit.ival = x;
90 95
91 } else { 96 } else {
92 - yylval.int_val = atoi(yytext); 97 + yylval.lit.ival = atoi(yytext);
93 } 98 }
94 return INT_LITERAL; 99 return INT_LITERAL;
95 } 100 }
96 {float_literal} { 101 {float_literal} {
97 - yylval.float_val = atof(yytext); 102 + yylval.lit.rval = atof(yytext);
98 return FLOAT_LITERAL; 103 return FLOAT_LITERAL;
99 } 104 }
100 105
fzn-parser/flatzinc.y
@@ -21,28 +21,36 @@ @@ -21,28 +21,36 @@
21 21
22 #define AST(pop, push) \ 22 #define AST(pop, push) \
23 { \ 23 { \
24 - if (pop) \  
25 - printf ("[%s|_Tail] ==> [", pop); \  
26 - else \  
27 - printf ("_Tail ==> ["); \ 24 + printf ("[%s|_T] -> [", pop); \
28 printf push; \ 25 printf push; \
29 - printf ("|_Tail].\n"); \ 26 + printf ("|_T].\n"); \
  27 +}
  28 +
  29 +#define INIT(x) \
  30 +{ \
  31 + printf ("_ -> %s.\n", x); \
30 } 32 }
31 33
32 #define PUSH(x) \ 34 #define PUSH(x) \
33 { \ 35 { \
34 - printf ("_Tail ==> ["); \ 36 + printf ("_T -> ["); \
35 printf x; \ 37 printf x; \
36 - printf ("|_Tail].\n"); \ 38 + printf ("|_T].\n"); \
37 } 39 }
38 40
39 -#define CONS() AST ("T,H", ("[H|T]"))  
40 -#define CONS_SWAPPED() AST ("H,T", ("[H|T]")) 41 + // pop an item and a list, push new list (CONS)
  42 +#define CONS() AST ("H,T", ("[H|T]"))
41 43
  44 + // pop one thing, push it as a 1-element list
42 #define TAIL() AST ("T", ("[T]")) 45 #define TAIL() AST ("T", ("[T]"))
  46 +
  47 + // push the empty list
43 #define NIL() PUSH (("[]")) 48 #define NIL() PUSH (("[]"))
44 49
  50 + // pop one thing, push an open-ended list headed with that thing
45 #define TAIL_OPEN() AST ("T", ("[T|_]")) 51 #define TAIL_OPEN() AST ("T", ("[T|_]"))
  52 +
  53 + // push an unbound variable
46 #define NIL_OPEN() PUSH (("_")) 54 #define NIL_OPEN() PUSH (("_"))
47 55
48 #define BINARY(op) AST ("B,A", ("op(A,B)")) 56 #define BINARY(op) AST ("B,A", ("op(A,B)"))
@@ -73,17 +81,21 @@ @@ -73,17 +81,21 @@
73 // caused stack overflows on large models. The error recovery isn't great, 81 // caused stack overflows on large models. The error recovery isn't great,
74 // but it's better than none. 82 // but it's better than none.
75 83
76 -model : pred_decl_items var_decl_items constraint_items model_end 84 +model : { INIT ("[]"); }
  85 + pred_decl_items { AST ("X", ("preds(X)")); }
  86 + var_decl_items { AST ("X", ("vars(X)")); }
  87 + constraint_items { AST ("X", ("constrs(X)")); }
  88 + model_end
77 89
78 -pred_decl_items : pred_decl_items pred_decl_item ';'  
79 - | pred_decl_items error ';' { yyerrok; }  
80 - | /* empty */ 90 +pred_decl_items : pred_decl_items pred_decl_item ';' { CONS (); }
  91 + | pred_decl_items error ';' { yyerrok; }
  92 + | /* empty */ { NIL (); }
81 93
82 -var_decl_items : var_decl_items var_decl_item ';'  
83 - | /* empty */ 94 +var_decl_items : var_decl_items var_decl_item ';' { CONS (); }
  95 + | /* empty */ { NIL (); }
84 96
85 -constraint_items: constraint_items constraint_item ';'  
86 - | /* empty */ 97 +constraint_items: constraint_items constraint_item ';' { CONS (); }
  98 + | /* empty */ { NIL (); }
87 99
88 model_end : solve_item ';' 100 model_end : solve_item ';'
89 101
@@ -118,6 +130,7 @@ ident_anns: @@ -118,6 +130,7 @@ ident_anns:
118 130
119 constraint_item: 131 constraint_item:
120 CONSTRAINT constraint_elem annotations 132 CONSTRAINT constraint_elem annotations
  133 + { AST ("A, C", ("constraint(C, A)")); }
121 134
122 constraint_elem: 135 constraint_elem:
123 IDENT '(' exprs ')' 136 IDENT '(' exprs ')'
@@ -165,20 +178,20 @@ scalar_ti_expr_tail: @@ -165,20 +178,20 @@ scalar_ti_expr_tail:
165 | float_ti_expr_tail 178 | float_ti_expr_tail
166 179
167 bool_ti_expr_tail: 180 bool_ti_expr_tail:
168 - BOOL 181 + BOOL { PUSH (("bool")); }
169 182
170 int_ti_expr_tail: 183 int_ti_expr_tail:
171 - INT  
172 - | INT_LITERAL DOTDOT INT_LITERAL  
173 - | '{' int_literals '}' 184 + INT { PUSH (("int")); }
  185 + | INT_LITERAL DOTDOT INT_LITERAL { PUSH (("int(%d,%d)", $1, $3)); }
  186 + | '{' int_literals '}' { AST ("Ls", ("int(Ls)")); }
174 187
175 int_literals: 188 int_literals:
176 - INT_LITERAL ',' int_literals  
177 - | INT_LITERAL 189 + INT_LITERAL ',' int_literals { AST ("Ls, L", ("[L|Ls]")); }
  190 + | INT_LITERAL { PUSH (("[%d]", $1)); }
178 191
179 float_ti_expr_tail: 192 float_ti_expr_tail:
180 - FLOAT  
181 - | FLOAT_LITERAL DOTDOT FLOAT_LITERAL 193 + FLOAT { PUSH (("float")); }
  194 + | FLOAT_LITERAL DOTDOT FLOAT_LITERAL { PUSH (("float(%g,%g)", $1, $3)); }
182 195
183 set_ti_expr_tail: 196 set_ti_expr_tail:
184 SET OF int_ti_expr_tail 197 SET OF int_ti_expr_tail
@@ -193,26 +206,28 @@ exprs: @@ -193,26 +206,28 @@ exprs:
193 206
194 expr: 207 expr:
195 bool_literal 208 bool_literal
196 - | INT_LITERAL  
197 - | FLOAT_LITERAL  
198 - | STRING_LITERAL 209 + | INT_LITERAL { PUSH (("lit(int, %d)", $1)); }
  210 + | FLOAT_LITERAL { PUSH (("lit(float, %g)", $1)); }
  211 + | STRING_LITERAL { PUSH (("lit(string, \"%s\")", $1)); }
199 | set_literal 212 | set_literal
200 | array_literal 213 | array_literal
201 | array_access_expr 214 | array_access_expr
202 - | IDENT 215 + | IDENT { PUSH (("lit(float, %g)", $1)); }
203 | UNDERSCORE_IDENT 216 | UNDERSCORE_IDENT
204 | IDENT '(' exprs ')' /* An annotation value with > 0 arguments. */ 217 | IDENT '(' exprs ')' /* An annotation value with > 0 arguments. */
205 218
206 -bool_literal: FALSE | TRUE 219 +bool_literal:
  220 + FALSE { PUSH (("lit(bool, false)")); }
  221 + | TRUE { PUSH (("lit(bool, true)")); }
207 222
208 set_literal: 223 set_literal:
209 - '{' exprs '}'  
210 - | '{' '}'  
211 - | INT_LITERAL DOTDOT INT_LITERAL 224 + '{' exprs '}' { AST ("Es", ("lit(set(_), Es)")); }
  225 + | '{' '}' { PUSH (("lit(set(_), [])")); }
  226 + | INT_LITERAL DOTDOT INT_LITERAL { PUSH (("lit(set(%d, %d)", $1, $3)); }
212 227
213 array_literal: 228 array_literal:
214 - '[' exprs ']'  
215 - | '[' ']' 229 + '[' exprs ']' { AST ("Es", ("alit(Es)")); }
  230 + | '[' ']' { PUSH (("alit([])")); }
216 231
217 array_access_expr: IDENT '[' INT_LITERAL ']' 232 array_access_expr: IDENT '[' INT_LITERAL ']'
218 | UNDERSCORE_IDENT '[' INT_LITERAL ']' 233 | UNDERSCORE_IDENT '[' INT_LITERAL ']'
@@ -222,8 +237,8 @@ array_access_expr: IDENT '[' INT_LITERAL ']' @@ -222,8 +237,8 @@ array_access_expr: IDENT '[' INT_LITERAL ']'
222 //--------------------------------------------------------------------------- 237 //---------------------------------------------------------------------------
223 238
224 annotations: 239 annotations:
225 - COLONCOLON expr annotations  
226 - | /* empty */ 240 + COLONCOLON expr annotations { CONS (); }
  241 + | /* empty */ { NIL (); }
227 242
228 %% 243 %%
229 244