-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsl.ebnf
302 lines (249 loc) · 12.9 KB
/
dsl.ebnf
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
300
301
302
# An EBNF of the DSL for Goals as Reward-Producing Programs
@@grammar :: GameDSL
@@parseinfo :: True
@@eol_comments :: /;([^\n]*?)$/
start = '(define' game_def domain_def setup_def constraints_def terminal_def scoring_def ')' $;
game_def = '(game' game_name:id ')';
domain_def = '(:domain' domain_name:id ')';
setup_def = '(:setup' setup ')' | ();
constraints_def = '(:constraints' preferences ')';
terminal_def = '(:terminal' terminal ')' | ();
scoring_def = '(:scoring' scoring ')';
# Generic definitions useful across sections
id = /[a-z0-9][a-z0-9\-]+/;
name = /[A-Za-z][A-Za-z0-9_]*/;
number_pattern = /-?\d*\.?\d+/;
variable = /\?[a-w][a-z0-9]*/;
color_variable = /\?x[0-9]*/;
orientation_variable = /\?y[0-9]*/;
side_variable = /\?z[0-9]*/;
variable_list = '(' {variables+:(variable_type_def | color_variable_type_def | orientation_variable_type_def | side_variable_type_def)}+ ')';
variable_type_def = {var_names+:variable}+ '-' var_type:type_definition;
color_variable_type_def = {var_names+:color_variable}+ '-' var_type:color_type_definition;
orientation_variable_type_def = {var_names+:orientation_variable}+ '-' var_type:orientation_type_definition;
side_variable_type_def = {var_names+:side_variable}+ '-' var_type:side_type_definition;
type_definition = type:(object_type | either_types);
color_type_definition = type:(color_type | either_color_types);
orientation_type_definition = type:(orientation_type | either_orientation_types);
side_type_definition = type:(side_type | either_side_types);
either_types = '(either' {type_names+:object_type}+ ')';
either_color_types = '(either' {type_names+:color}+ ')';
either_orientation_types = '(either' {type_names+:orientation}+ ')';
either_side_types = '(either' {type_names+:side}+ ')';
object_type = terminal:name; # we don't hardcode all the types, but do set a prior over them in the sampler
object_name = terminal:('agent' | 'bed' | 'desk' | 'door' | 'floor' | 'main_light_switch' | 'mirror' | 'room_center' | 'rug' | 'side_table' | 'bottom_drawer' | 'bottom_shelf' | 'east_sliding_door' | 'east_wall' | 'north_wall' | 'south_wall' | 'top_drawer' | 'top_shelf' | 'west_sliding_door' | 'west_wall');
color_type = terminal:'color';
color = terminal:('blue' | 'brown' | 'gray' | 'green' | 'orange' | 'pink' | 'purple' | 'red' | 'tan' | 'white' | 'yellow');
orientation_type = terminal:'orientation';
orientation = terminal:('diagonal' | 'sideways' | 'upright' | 'upside_down');
side_type = terminal:'side';
side = terminal:('back' | 'front' | 'left' | 'right');
comparison_arg_number_value = terminal:number_pattern;
time_number_value = terminal:number_pattern;
score_number_value = terminal:number_pattern;
pref_count_number_value = terminal:number_pattern;
scoring_number_value = terminal:number_pattern;
function_comparison = comp:(two_arg_comparison | multiple_args_equal_comparison);
two_arg_comparison = '(' comp_op:binary_comp_no_equal arg_1:comparison_arg arg_2:comparison_arg ')';
multiple_args_equal_comparison = '(=' {equal_comp_args+:comparison_arg}+ ')';
predicate_or_function_term = term:(object_name | variable);
predicate_or_function_color_term = term:(color | color_variable);
# predicate_or_function_location_term = term:(location | variable);
predicate_or_function_orientation_term = term:(orientation | orientation_variable);
predicate_or_function_side_term = term:(side | side_variable);
predicate_or_function_type_term = term:(object_type | variable);
comparison_arg = arg:(function_eval | comparison_arg_number_value);
binary_comp = '<=' | '<' | '=' | '>=' | '>';
binary_comp_no_equal = '<=' | '<' | '>=' | '>';
function_eval = func:(function_building_size |
function_distance |
function_distance_side_3 |
function_distance_side_4 |
function_x_position
)
;
function_building_size = '(building_size' arg_1:predicate_or_function_term ')';
function_distance = '(distance' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
function_distance_side_3 = '(distance_side' arg_1:predicate_or_function_term arg_2:predicate_or_function_side_term arg_3:predicate_or_function_term ')';
function_distance_side_4 = '(distance_side' arg_1:predicate_or_function_term arg_2:predicate_or_function_side_term arg_3:predicate_or_function_term arg_4:predicate_or_function_side_term ')';
function_x_position = '(x_position' arg_1:predicate_or_function_term ')';
predicate = pred:(predicate_above |
predicate_adjacent |
predicate_adjacent_side_3 |
predicate_adjacent_side_4 |
predicate_agent_crouches |
predicate_agent_holds |
predicate_between |
predicate_broken |
predicate_equal_x_position |
predicate_equal_z_position |
predicate_faces |
predicate_game_over |
predicate_game_start |
predicate_in |
predicate_in_motion |
predicate_is_setup_object |
predicate_near |
predicate_object_orientation |
predicate_on |
predicate_open |
predicate_opposite |
predicate_rug_color_under |
predicate_same_color |
predicate_same_object |
predicate_same_type |
predicate_toggled_on |
predicate_touch
)
;
predicate_above = '(above' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
predicate_adjacent = '(adjacent' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
predicate_adjacent_side_3 = '(adjacent_side' arg_1:predicate_or_function_term arg_2:predicate_or_function_side_term arg_3:predicate_or_function_term ')';
predicate_adjacent_side_4 = '(adjacent_side' arg_1:predicate_or_function_term arg_2:predicate_or_function_side_term arg_3:predicate_or_function_term arg_4:predicate_or_function_side_term ')';
predicate_agent_crouches = '(agent_crouches' no_args:{} ')'; # hack to make sure it parses an expression
predicate_agent_holds = '(agent_holds' arg_1:predicate_or_function_term ')';
predicate_between = '(between' arg_1:predicate_or_function_term arg_2:predicate_or_function_term arg_3:predicate_or_function_term ')';
predicate_broken = '(broken' arg_1:predicate_or_function_term ')';
predicate_equal_x_position = '(equal_x_position' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
predicate_equal_z_position = '(equal_z_position' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
predicate_faces = '(faces' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
predicate_game_over = '(game_over' no_args:{} ')'; # hack to make sure it parses an expression
predicate_game_start = '(game_start' no_args:{} ')'; # hack to make sure it parses an expression
predicate_in = '(in' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
predicate_in_motion = '(in_motion' arg_1:predicate_or_function_term ')';
predicate_is_setup_object = '(is_setup_object' arg_1:predicate_or_function_term ')';
predicate_near = '(near' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
predicate_object_orientation = '(object_orientation' arg_1:predicate_or_function_term arg_2:predicate_or_function_orientation_term ')';
predicate_on = '(on' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
predicate_open = '(open' arg_1:predicate_or_function_term ')';
predicate_opposite = '(opposite' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
predicate_rug_color_under = '(rug_color_under' arg_1:predicate_or_function_term arg_2:predicate_or_function_color_term ')';
predicate_same_color = '(same_color' arg_1:predicate_or_function_term arg_2:(predicate_or_function_color_term | predicate_or_function_term) ')';
predicate_same_object = '(same_object' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
predicate_same_type = '(same_type' arg_1:predicate_or_function_term arg_2:predicate_or_function_type_term ')';
predicate_toggled_on = '(toggled_on' arg_1:predicate_or_function_term ')';
predicate_touch = '(touch' arg_1:predicate_or_function_term arg_2:predicate_or_function_term ')';
# Game Setup
setup = setup:(setup_and |
setup_or |
setup_not |
setup_exists |
setup_forall |
setup_statement
)
;
setup_and = '(and' {and_args+:setup}+ ')';
setup_or = '(or' {or_args+:setup}+ ')';
setup_not = '(not' not_args:setup ')';
setup_exists = '(exists' exists_vars:variable_list exists_args:setup ')';
setup_forall = '(forall' forall_vars:variable_list forall_args:setup ')';
setup_statement = statement:(setup_game_conserved | setup_game_optional);
setup_game_conserved = '(game-conserved' conserved_pred:super_predicate ')';
setup_game_optional = '(game-optional' optional_pred:super_predicate ')';
super_predicate = pred:(
super_predicate_and |
super_predicate_or |
super_predicate_not |
super_predicate_exists |
super_predicate_forall |
function_comparison |
predicate
)
;
super_predicate_and = '(and' {and_args+:super_predicate}+ ')';
super_predicate_or = '(or' {or_args+:super_predicate}+ ')';
super_predicate_not = '(not' not_args:super_predicate ')';
super_predicate_exists = '(exists' exists_vars:variable_list exists_args:super_predicate ')';
super_predicate_forall = '(forall' forall_vars:variable_list forall_args:super_predicate ')';
# Constraints/Preferences
preferences =
'(and' {preferences+:pref_def}+ ')' |
preferences:pref_def
;
pref_def = definition:(preference | pref_forall);
preference = '(preference' pref_name:preference_name pref_body:pref_body ')';
preference_name = name;
pref_forall = '(forall' forall_vars:variable_list forall_pref:pref_forall_prefs ')';
pref_forall_prefs = '(and' {preferences+:preference}+ ')' |
{preferences:preference};
pref_body = body:(
pref_body_exists |
then |
at_end
);
# pref_body_forall |
pref_body_exists = '(exists' exists_vars:variable_list exists_args:(then | at_end) ')';
at_end = '(at-end' at_end_pred:super_predicate')';
always = '(always' always_pred:super_predicate')';
then = '(then' {then_funcs+:seq_func}+ ')';
seq_func = seq_func:(
once |
once_measure |
hold |
while_hold # |
);
# any |
# any = '(any)';
once = '(once ' once_pred:super_predicate ')';
once_measure = '(once-measure' once_measure_pred:super_predicate measurement:function_eval ')';
hold = '(hold' hold_pred:super_predicate ')';
while_hold = '(hold-while' hold_pred:super_predicate {while_preds+:super_predicate}+ ')';
terminal = terminal:(
terminal_and |
terminal_or |
terminal_not |
terminal_comp
);
terminal_and = '(and' {and_args+:terminal}+ ')';
terminal_or = '(or' {or_args+:terminal}+ ')';
terminal_not = '(not' not_args:terminal ')';
terminal_comp = comp:(terminal_time_comp | terminal_score_comp | terminal_pref_count_comp);
terminal_time_comp = '(' op:binary_comp expr_1:total_time expr_2:time_number_value ')';
terminal_score_comp = '(' op:binary_comp expr_1:total_score expr_2:score_number_value ')';
terminal_pref_count_comp = '(' op:binary_comp expr_1:scoring_expr_or_number expr_2:pref_count_number_value ')';
total_time = '(total-time)';
total_score = '(total-score)' ;
scoring_expr = expr:(
scoring_external_maximize |
scoring_external_minimize |
scoring_multi_expr |
scoring_binary_expr |
scoring_neg_expr |
preference_eval |
scoring_comparison
);
scoring_expr_or_number = expr:(scoring_expr | scoring_number_value);
scoring_external_maximize = '(external-forall-maximize' scoring_expr:scoring_expr ')';
scoring_external_minimize = '(external-forall-minimize' scoring_expr:scoring_expr ')';
scoring_multi_expr = '(' op:multi_op {expr+:scoring_expr_or_number}+ ')';
scoring_binary_expr = '(' op:binary_op expr_1:scoring_expr_or_number expr_2:scoring_expr_or_number ')';
scoring_neg_expr = '(-' expr:scoring_expr_or_number ')';
multi_op = '+' | '*';
binary_op = '-' | '/';
scoring_comparison = comp:(
scoring_comp |
scoring_equals_comp);
scoring_comp = '(' op:binary_comp_no_equal expr_1:scoring_expr_or_number expr_2:scoring_expr_or_number ')';
scoring_equals_comp = '(=' {expr+:scoring_expr_or_number}+ ')';
preference_eval = count_method:(
count |
count_overlapping |
count_once |
count_once_per_objects |
count_unique_positions |
count_same_positions |
count_once_per_external_objects |
count_measure
);
count = '(count' name_and_types:pref_name_and_types ')';
count_overlapping = '(count-overlapping' name_and_types:pref_name_and_types ')';
count_once = '(count-once' name_and_types:pref_name_and_types ')';
count_once_per_objects = '(count-once-per-objects' name_and_types:pref_name_and_types ')';
count_unique_positions = '(count-unique-positions' name_and_types:pref_name_and_types ')';
count_same_positions = '(count-same-positions' name_and_types:pref_name_and_types ')';
count_once_per_external_objects = '(count-once-per-external-objects' name_and_types:pref_name_and_types ')';
count_measure = '(count-measure' name_and_types:pref_name_and_types ')';
pref_name_and_types = pref_name:preference_name {object_types+:pref_object_type}*;
pref_object_type = ':' type_name:(color | object_name | object_type);
# Scoring
scoring = scoring_expr;