[r1813]: trunk / Toss / Solver / StructureParser.mly Maximize Restore History

Download this file

StructureParser.mly    197 lines (173 with data), 6.5 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
/* Tokens taken from Lexer.mll */
%{
open Lexer
open Formula
open Structure
%}
%start parse_rel_defs parse_rel_const_defs parse_structure
%type <string array> elem_tuple
%type <(string * int option * string array list,
string * string) Aux.choice> rel_or_const_expr
%type <(string * string list * Formula.formula) list> parse_rel_defs
%type <(string * string list * Formula.formula) list *
(string * (int, string) Aux.choice) list> parse_rel_const_defs
%type <string * (string * float) list> fun_expr
%type <Structure.structure -> Structure.structure> ext_struct_expr board_expr
%type <Structure.structure -> Structure.structure> extend_struct
%type <Structure.structure> struct_expr parse_structure
%%
float_int:
| i=FLOAT { i }
| i=INT { float_of_int i }
elem_tuple:
| tup = delimited (OPEN, separated_list (COMMA, ID), CLOSE)
{ Array.of_list tup }
| elem = ID
{ [|elem|] }
/* While we otherwise prefer to use commas to separate set elements,
we only allow semicolons below to avoid confusion with
constructing tuples. */
rel_or_const_expr:
| rel = ID
tup = delimited (OPEN, separated_list (COMMA, ID), CLOSE)
{ Aux.Left (rel, None, [Array.of_list tup]) }
| rel = ID
arity = option (preceded (COLON, INT))
tups = delimited (OPENCUR, separated_list (SEMICOLON, elem_tuple),
CLOSECUR)
{ Aux.Left (rel, arity, tups) }
| DOT c = ID EQ r = ID { Aux.Right ("." ^ c, r) }
| DOT c = ID EQ r = INT { Aux.Right ("." ^ c, string_of_int r) }
| rel = ID error
{ Lexer.report_parsing_error $startpos $endpos
("Syntax error in graph of relation " ^ rel) }
fun_expr:
| fn = ID
assgns = delimited (OPENCUR, separated_list (
COMMA, separated_pair (ID, RARR, float_int)), CLOSECUR)
{ fn, assgns }
| fn = ID error
{ Lexer.report_parsing_error $startpos $endpos
("Syntax error in graph of function " ^ fn) }
ext_struct_expr:
| OPENSQ
elems = separated_list (COMMA, ID)
MID
rels = separated_list (SEMICOLON, rel_or_const_expr)
MID
funs = separated_list (SEMICOLON, fun_expr)
CLOSESQ
{ fun struc ->
create_from_lists ~struc elems rels funs }
| OPENSQ
elems = separated_list (COMMA, ID)
MID
rels = separated_list (SEMICOLON, rel_or_const_expr)
MID MINUS CLOSESQ
{ fun struc -> create_from_lists_position ~struc elems rels }
| OPENSQ n = INT MINUS k = INT
MID
rels = separated_list (SEMICOLON, rel_or_const_expr)
MID
funs = separated_list (SEMICOLON, fun_expr)
CLOSESQ
{ let elems =
List.map (fun i -> "e" ^ (string_of_int i)) (Aux.range ~from:n (k+1)) in
fun struc -> create_from_lists ~struc elems rels funs }
| OPENSQ n = INT MINUS k = INT
MID
rels = separated_list (SEMICOLON, rel_or_const_expr)
MID MINUS CLOSESQ
{ let elems =
List.map (fun i -> "e" ^ (string_of_int i)) (Aux.range ~from:n (k+1)) in
fun struc ->
create_from_lists_range (float n) ~struc elems rels }
| OPENSQ
separated_list (COMMA, ID)
MID
separated_list (SEMICOLON, rel_or_const_expr)
MID err = error
{ ignore err;
Lexer.report_parsing_error $startpos(err) $endpos(err)
("Syntax error in functions part of model.") }
| OPENSQ
separated_list (COMMA, ID)
MID err = error
{ ignore err;
Lexer.report_parsing_error $startpos(err) $endpos(err)
("Syntax error in relations part of model.") }
| OPENSQ error
{ Lexer.report_parsing_error $startpos $endpos
("Syntax error in model definition.") }
board_expr:
| row_col_rels = option (pair (ID, ID))
pos_initial = delimited (
OPEN, separated_pair (FLOAT, COMMA, FLOAT), CLOSE)
pos_increment = delimited (
OPEN, separated_pair (FLOAT, COMMA, FLOAT), CLOSE)
board = BOARD_STRING
{ fun struc ->
try parse_board ?row_col_rels ~pos_initial
~pos_increment ~struc board
with Structure.Board_parse_error s ->
Lexer.report_parsing_error $startpos $endpos
("Syntax error in board:\n" ^ s)}
| row_col_rels = option (pair (ID, ID))
pos_increment = option (delimited (
OPEN, separated_pair (FLOAT, COMMA, FLOAT), CLOSE))
board = BOARD_STRING
{ fun struc ->
try parse_board ?row_col_rels
?pos_increment ~struc board
with Structure.Board_parse_error s ->
Lexer.report_parsing_error $startpos $endpos
("Syntax error in board:\n" ^ s)}
basic_struct_expr:
| ext_struct = ext_struct_expr
{ ext_struct (empty_structure ()) }
| ext_struct = ext_struct_expr
board = board_expr
{ board (ext_struct (empty_structure ())) }
rel_def_simple:
| rel = ID args = delimited (OPEN, separated_list (COMMA, ID), CLOSE)
EQ body = formula_expr { (rel, args, body) }
fun_def_simple:
| COLON f = ID OPEN v = ID CLOSE EQ body = real_expr
{ (f, v, body) }
%public struct_expr:
| model = basic_struct_expr
{ model }
| model = basic_struct_expr WITH
defs = separated_list (SEMICOLON, rel_def_simple)
{ Solver.add_def_rels model defs }
| model = basic_struct_expr WITH
defs = separated_list (SEMICOLON, rel_def_simple) WITH
funs = separated_list (SEMICOLON, fun_def_simple)
{ Solver.add_def_funs (Solver.add_def_rels model defs) funs }
| error
{ Lexer.report_parsing_error $startpos $endpos
"Syntax error in structure definition." }
%public extend_struct:
| ext_struct = ext_struct_expr { ext_struct }
| board = board_expr { board }
| ext_struct = ext_struct_expr
board = board_expr
{ fun struc -> board (ext_struct struc) }
parse_structure:
| struct_expr EOF { $1 }
parse_rel_defs:
| rdefs = separated_list (SEMICOLON, rel_def_simple) EOF { rdefs }
| error
{ Lexer.report_parsing_error $startpos $endpos
"Syntax error parsing relations definitions." }
const_def_simple:
| DOT cons = ID EQ AMP i = INT { ("." ^ cons, Aux.Left i) }
| DOT cons = ID EQ DOT c = ID { ("." ^ cons, Aux.Right ("." ^ c)) }
parse_rel_const_defs:
| rdefs = separated_list (SEMICOLON, rel_def_simple) EOF { (rdefs, []) }
| rdefs = separated_list (SEMICOLON, rel_def_simple) WITH
cdefs = separated_list (SEMICOLON, const_def_simple) EOF
{ rdefs, cdefs }
| error
{ Lexer.report_parsing_error $startpos $endpos
"Syntax error parsing relations and constants definitions." }