-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser.yy
1593 lines (1368 loc) · 50.4 KB
/
parser.yy
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*-C++-*- */
/*
* Parser.
*
* Copyright 2003-2005 Carnegie Mellon University and Rutgers University
* Copyright 2007 Håkan Younes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
%{
#include <config.h>
#include "problems.h"
#include "domains.h"
#include "actions.h"
#include "effects.h"
#include "formulas.h"
#include "expressions.h"
#include "functions.h"
#include "predicates.h"
#include "terms.h"
#include "types.h"
#include "rational.h"
#include <iostream>
#include <map>
#include <string>
#include <stdexcept>
#include <typeinfo>
/* Workaround for bug in Bison 1.35 that disables stack growth. */
#define YYLTYPE_IS_TRIVIAL 1
/*
* Context of free variables.
*/
struct Context {
void push_frame() {
frames_.push_back(VariableMap());
}
void pop_frame() {
frames_.pop_back();
}
void insert(const std::string& name, const Variable& v) {
frames_.back().insert(std::make_pair(name, v));
}
const Variable* shallow_find(const std::string& name) const {
VariableMap::const_iterator vi = frames_.back().find(name);
if (vi != frames_.back().end()) {
return &(*vi).second;
} else {
return 0;
}
}
const Variable* find(const std::string& name) const {
for (std::vector<VariableMap>::const_reverse_iterator fi =
frames_.rbegin(); fi != frames_.rend(); fi++) {
VariableMap::const_iterator vi = (*fi).find(name);
if (vi != (*fi).end()) {
return &(*vi).second;
}
}
return 0;
}
private:
struct VariableMap : public std::map<std::string, Variable> {
};
std::vector<VariableMap> frames_;
};
/* The lexer. */
extern int yylex();
/* Current line number. */
extern size_t line_number;
/* Name of current file. */
extern std::string current_file;
/* Level of warnings. */
extern int warning_level;
/* Whether the last parsing attempt succeeded. */
static bool success = true;
/* Current domain. */
static Domain* domain;
/* Domains. */
static std::map<std::string, Domain*> domains;
/* Pointer to problem being parsed, or 0 if no problem is being parsed. */
static Problem* problem;
/* Current requirements. */
static Requirements* requirements;
/* The reward function, if rewards are required. */
static Function reward_function(-1);
/* Predicate being parsed. */
static const Predicate* predicate;
/* Whether predicate declaration is repeated. */
static bool repeated_predicate;
/* Function being parsed. */
static const Function* function;
/* Whether function declaration is repeated. */
static bool repeated_function;
/* Pointer to action being parsed, or 0 if no action is being parsed. */
static ActionSchema* action;
/* Current variable context. */
static Context context;
/* Predicate for atomic state formula being parsed. */
static const Predicate* atom_predicate;
/* Whether the predicate of the currently parsed atom was undeclared. */
static bool undeclared_atom_predicate;
/* Whether parsing effect fluents. */
static bool effect_fluent;
/* Whether parsing metric fluent. */
static bool metric_fluent;
/* Function for fluent being parsed. */
static const Function* fluent_function;
/* Whether the function of the currently parsed fluent was undeclared. */
static bool undeclared_fluent_function;
/* Paramerers for atomic state formula or fluent being parsed. */
static TermList term_parameters;
/* Quantified variables for effect or formula being parsed. */
static TermList quantified;
/* Most recently parsed term for equality formula. */
static Term eq_term(0);
/* Most recently parsed expression for equality formula. */
static const Expression* eq_expr;
/* The first term for equality formula. */
static Term first_eq_term(0);
/* The first expression for equality formula. */
static const Expression* first_eq_expr;
/* Kind of name map being parsed. */
static enum { TYPE_KIND, CONSTANT_KIND, OBJECT_KIND, VOID_KIND } name_kind;
/* Outputs an error message. */
static void yyerror(const std::string& s);
/* Outputs a warning message. */
static void yywarning(const std::string& s);
/* Creates an empty domain with the given name. */
static void make_domain(const std::string* name);
/* Creates an empty problem with the given name. */
static void make_problem(const std::string* name,
const std::string* domain_name);
/* Adds :typing to the requirements. */
static void require_typing();
/* Adds :fluents to the requirements. */
static void require_fluents();
/* Adds :disjunctive-preconditions to the requirements. */
static void require_disjunction();
/* Adds :conditional-effects to the requirements. */
static void require_conditional_effects();
/* Returns a simple type with the given name. */
static const Type& make_type(const std::string* name);
/* Returns the union of the given types. */
static Type make_type(const TypeSet& types);
/* Returns a simple term with the given name. */
static Term make_term(const std::string* name);
/* Creates a predicate with the given name. */
static void make_predicate(const std::string* name);
/* Creates a function with the given name. */
static void make_function(const std::string* name);
/* Creates an action with the given name. */
static void make_action(const std::string* name);
/* Adds the current action to the current domain. */
static void add_action();
/* Prepares for the parsing of a universally quantified effect. */
static void prepare_forall_effect();
/* Creates a universally quantified effect. */
static const Effect* make_forall_effect(const Effect& effect);
/* Adds an outcome to the given probabilistic effect. */
static void add_outcome(std::vector<std::pair<Rational, const Effect*> >& os,
const Rational* p, const Effect& effect);
/* Creates a probabilistic effect. */
static const Effect*
make_prob_effect(const std::vector<std::pair<Rational, const Effect*> >* os);
/* Creates an add effect. */
static const Effect* make_add_effect(const Atom& atom);
/* Creates a delete effect. */
static const Effect* make_delete_effect(const Atom& atom);
/* Creates an assign update effect. */
static const Effect* make_assign_effect(const Fluent& fluent,
const Expression& expr);
/* Creates a scale-up update effect. */
static const Effect* make_scale_up_effect(const Fluent& fluent,
const Expression& expr);
/* Creates a scale-down update effect. */
static const Effect* make_scale_down_effect(const Fluent& fluent,
const Expression& expr);
/* Creates an increase update effect. */
static const Effect* make_increase_effect(const Fluent& fluent,
const Expression& expr);
/* Creates a decrease update effect. */
static const Effect* make_decrease_effect(const Fluent& fluent,
const Expression& expr);
/* Adds types, constants, or objects to the current domain or problem. */
static void add_names(const std::vector<const std::string*>* names,
const Type& type);
/* Adds variables to the current variable list. */
static void add_variables(const std::vector<const std::string*>* names,
const Type& type);
/* Prepares for the parsing of an atomic state formula. */
static void prepare_atom(const std::string* name);
/* Prepares for the parsing of a fluent. */
static void prepare_fluent(const std::string* name);
/* Adds a term with the given name to the current atomic state formula. */
static void add_term(const std::string* name);
/* Creates the atomic formula just parsed. */
static const Atom* make_atom();
/* Creates the fluent just parsed. */
static const Fluent* make_fluent();
/* Creates a subtraction. */
static const Expression* make_subtraction(const Expression& term,
const Expression* opt_term);
/* Creates an atom or fluent for the given name to be used in an
equality formula. */
static void make_eq_name(const std::string* name);
/* Creates an equality formula. */
static const StateFormula* make_equality();
/* Creates a negated formula. */
static const StateFormula* make_negation(const StateFormula& negand);
/* Creates an implication. */
static const StateFormula* make_implication(const StateFormula& f1,
const StateFormula& f2);
/* Prepares for the parsing of an existentially quantified formula. */
static void prepare_exists();
/* Prepares for the parsing of a universally quantified formula. */
static void prepare_forall();
/* Creates an existentially quantified formula. */
static const StateFormula* make_exists(const StateFormula& body);
/* Creates a universally quantified formula. */
static const StateFormula* make_forall(const StateFormula& body);
/* Sets the goal reward for the current problem. */
static void set_goal_reward(const Expression& goal_reward);
/* Sets the default metric for the current problem. */
static void set_default_metric();
%}
%token DEFINE DOMAIN_TOKEN PROBLEM
%token REQUIREMENTS TYPES CONSTANTS PREDICATES FUNCTIONS
%token STRIPS TYPING NEGATIVE_PRECONDITIONS DISJUNCTIVE_PRECONDITIONS EQUALITY
%token EXISTENTIAL_PRECONDITIONS UNIVERSAL_PRECONDITIONS
%token QUANTIFIED_PRECONDITIONS CONDITIONAL_EFFECTS FLUENTS ADL
%token DURATIVE_ACTIONS DURATION_INEQUALITIES CONTINUOUS_EFFECTS
%token PROBABILISTIC_EFFECTS REWARDS MDP
%token ACTION PARAMETERS PRECONDITION EFFECT
%token PDOMAIN OBJECTS INIT GOAL GOAL_REWARD METRIC TOTAL_TIME GOAL_ACHIEVED
%token WHEN NOT AND OR IMPLY EXISTS FORALL PROBABILISTIC
%token ASSIGN SCALE_UP SCALE_DOWN INCREASE DECREASE MINIMIZE MAXIMIZE
%token NUMBER_TOKEN OBJECT_TOKEN EITHER
%token LE GE NAME VARIABLE NUMBER
%token ILLEGAL_TOKEN
%union {
const Effect* effect;
std::vector<std::pair<Rational, const Effect*> >* outcomes;
const StateFormula* formula;
const Atom* atom;
const Expression* expr;
const Fluent* fluent;
const Type* type;
TypeSet* types;
const std::string* str;
std::vector<const std::string*>* strs;
const Rational* num;
}
%type <effect> eff_formula p_effect simple_init one_init eff_formulas one_inits
%type <outcomes> prob_effs prob_inits
%type <formula> formula conjuncts disjuncts
%type <atom> atomic_name_formula atomic_term_formula
%type <expr> value f_exp opt_f_exp ground_f_exp opt_ground_f_exp
%type <fluent> ground_f_head f_head
%type <strs> name_seq variable_seq
%type <type> type_spec type
%type <types> types
%type <str> type_name predicate function name variable
%type <str> DEFINE DOMAIN_TOKEN PROBLEM
%type <str> WHEN NOT AND OR IMPLY EXISTS FORALL PROBABILISTIC
%type <str> ASSIGN SCALE_UP SCALE_DOWN INCREASE DECREASE MINIMIZE MAXIMIZE
%type <str> NUMBER_TOKEN OBJECT_TOKEN EITHER
%type <str> NAME VARIABLE
%type <num> probability NUMBER
%%
file : { success = true; line_number = 1; } domains_and_problems
{ if (!success) YYERROR; }
;
domains_and_problems : /* empty */
| domains_and_problems domain_def
| domains_and_problems problem_def
;
/* ====================================================================== */
/* Domain definitions. */
domain_def : '(' define '(' domain name ')' { make_domain($5); }
domain_body ')'
;
domain_body : /* empty */
| require_def
| require_def domain_body2
| domain_body2
;
domain_body2 : types_def
| types_def domain_body3
| domain_body3
;
domain_body3 : constants_def
| predicates_def
| functions_def
| constants_def domain_body4
| predicates_def domain_body5
| functions_def domain_body6
| structure_defs
;
domain_body4 : predicates_def
| functions_def
| predicates_def domain_body7
| functions_def domain_body8
| structure_defs
;
domain_body5 : constants_def
| functions_def
| constants_def domain_body7
| functions_def domain_body9
| structure_defs
;
domain_body6 : constants_def
| predicates_def
| constants_def domain_body8
| predicates_def domain_body9
| structure_defs
;
domain_body7 : functions_def
| functions_def structure_defs
| structure_defs
;
domain_body8 : predicates_def
| predicates_def structure_defs
| structure_defs
;
domain_body9 : constants_def
| constants_def structure_defs
| structure_defs
;
structure_defs : structure_def
| structure_defs structure_def
;
structure_def : action_def
;
require_def : '(' REQUIREMENTS require_keys ')'
;
require_keys : require_key
| require_keys require_key
;
require_key : STRIPS { requirements->strips = true; }
| TYPING { requirements->typing = true; }
| NEGATIVE_PRECONDITIONS
{ requirements->negative_preconditions = true; }
| DISJUNCTIVE_PRECONDITIONS
{ requirements->disjunctive_preconditions = true; }
| EQUALITY { requirements->equality = true; }
| EXISTENTIAL_PRECONDITIONS
{ requirements->existential_preconditions = true; }
| UNIVERSAL_PRECONDITIONS
{ requirements->universal_preconditions = true; }
| QUANTIFIED_PRECONDITIONS
{ requirements->quantified_preconditions(); }
| CONDITIONAL_EFFECTS { requirements->conditional_effects = true; }
| FLUENTS { requirements->fluents = true; }
| ADL { requirements->adl(); }
| DURATIVE_ACTIONS
{
throw std::runtime_error("`:durative-actions'"
" not supported");
}
| DURATION_INEQUALITIES
{
throw std::runtime_error("`:duration-inequalities'"
" not supported");
}
| CONTINUOUS_EFFECTS
{
throw std::runtime_error("`:continuous-effects'"
" not supported");
}
| PROBABILISTIC_EFFECTS
{
requirements->probabilistic_effects = true;
}
| REWARDS
{
requirements->rewards = true;
reward_function = domain->functions().add_function("reward");
}
| MDP
{
requirements->mdp();
reward_function = domain->functions().add_function("reward");
}
;
types_def : '(' TYPES { require_typing(); name_kind = TYPE_KIND; }
typed_names ')' { name_kind = VOID_KIND; }
;
constants_def : '(' CONSTANTS { name_kind = CONSTANT_KIND; } typed_names ')'
{ name_kind = VOID_KIND; }
;
predicates_def : '(' PREDICATES predicate_decls ')'
;
functions_def : '(' FUNCTIONS { require_fluents(); } function_decls ')'
;
/* ====================================================================== */
/* Predicate and function declarations. */
predicate_decls : /* empty */
| predicate_decls predicate_decl
;
predicate_decl : '(' predicate { make_predicate($2); } variables ')'
{ predicate = 0; }
;
function_decls : /* empty */
| function_decl_seq
| function_decl_seq function_type_spec function_decls
;
function_decl_seq : function_decl
| function_decl_seq function_decl
;
function_type_spec : '-' { require_typing(); } function_type
;
function_decl : '(' function { make_function($2); } variables ')'
{ function = 0; }
;
/* ====================================================================== */
/* Action definitions. */
action_def : '(' ACTION name { make_action($3); }
parameters action_body ')' { add_action(); }
;
parameters : /* empty */
| PARAMETERS '(' variables ')'
;
action_body : precondition action_body2
| action_body2
;
action_body2 : /* empty */
| effect
;
precondition : PRECONDITION formula { action->set_precondition(*$2); }
;
effect : EFFECT eff_formula { action->set_effect(*$2); }
;
/* ====================================================================== */
/* Effect formulas. */
eff_formula : p_effect
| '(' and eff_formulas ')' { $$ = $3; }
| '(' forall { prepare_forall_effect(); } '(' variables ')'
eff_formula ')' { $$ = make_forall_effect(*$7); }
| '(' when { require_conditional_effects(); } formula
eff_formula ')' { $$ = &ConditionalEffect::make(*$4, *$5); }
| '(' probabilistic prob_effs ')'
{ $$ = make_prob_effect($3); }
;
eff_formulas : /* empty */ { $$ = &Effect::EMPTY; }
| eff_formulas eff_formula { $$ = &(*$1 && *$2); }
;
prob_effs : probability eff_formula
{
$$ = new std::vector<std::pair<Rational, const Effect*> >();
add_outcome(*$$, $1, *$2);
}
| prob_effs probability eff_formula
{ $$ = $1; add_outcome(*$$, $2, *$3); }
;
probability : NUMBER
;
p_effect : atomic_term_formula { $$ = make_add_effect(*$1); }
| '(' not atomic_term_formula ')' { $$ = make_delete_effect(*$3); }
| '(' assign { effect_fluent = true; } f_head f_exp ')'
{ $$ = make_assign_effect(*$4, *$5); }
| '(' scale_up { effect_fluent = true; } f_head f_exp ')'
{ $$ = make_scale_up_effect(*$4, *$5); }
| '(' scale_down { effect_fluent = true; } f_head f_exp ')'
{ $$ = make_scale_down_effect(*$4, *$5); }
| '(' increase { effect_fluent = true; } f_head f_exp ')'
{ $$ = make_increase_effect(*$4, *$5); }
| '(' decrease { effect_fluent = true; } f_head f_exp ')'
{ $$ = make_decrease_effect(*$4, *$5); }
;
/* ====================================================================== */
/* Problem definitions. */
problem_def : '(' define '(' problem name ')' '(' PDOMAIN name ')'
{ make_problem($5, $9); } problem_body ')'
{ problem->instantiate(); delete requirements; }
;
problem_body : require_def problem_body2
| problem_body2
;
problem_body2 : object_decl problem_body3
| problem_body3
;
problem_body3 : init goal_spec
| goal_spec
;
object_decl : '(' OBJECTS { name_kind = OBJECT_KIND; } typed_names ')'
{ name_kind = VOID_KIND; }
;
init : '(' INIT init_elements ')'
;
init_elements : /* empty */
| init_elements init_element
;
init_element : atomic_name_formula { problem->add_init_atom(*$1); }
| '(' '=' ground_f_head NUMBER ')'
{ problem->add_init_value(*$3, *$4); delete $4; }
| '(' probabilistic prob_inits ')'
{ problem->add_init_effect(*make_prob_effect($3)); }
;
prob_inits : probability simple_init
{
$$ = new std::vector<std::pair<Rational, const Effect*> >();
add_outcome(*$$, $1, *$2);
}
| prob_inits probability simple_init
{ $$ = $1; add_outcome(*$$, $2, *$3); }
;
simple_init : one_init
| '(' and one_inits ')' { $$ = $3; }
;
one_inits : /* empty */ { $$ = &Effect::EMPTY; }
| one_inits one_init { $$ = &(*$1 && *$2); }
;
one_init : atomic_name_formula { $$ = make_add_effect(*$1); }
| '(' '=' ground_f_head value ')'
{ $$ = make_assign_effect(*$3, *$4); }
;
value : NUMBER { $$ = new Value(*$1); delete $1; }
;
goal_spec : '(' GOAL formula ')' goal_reward { problem->set_goal(*$3); }
| metric_spec
;
goal_reward : metric_spec
| '(' GOAL_REWARD ground_f_exp ')' metric_spec
{ set_goal_reward(*$3); }
;
metric_spec : /* empty */ { set_default_metric(); }
| '(' METRIC maximize { metric_fluent = true; } ground_f_exp ')'
{ problem->set_metric(*$5); metric_fluent = false; }
| '(' METRIC minimize { metric_fluent = true; } ground_f_exp ')'
{ problem->set_metric(*$5, true); metric_fluent = false; }
;
/* ====================================================================== */
/* Formulas. */
formula : atomic_term_formula { $$ = $1; }
| '(' '=' term_or_f_exp
{ first_eq_term = eq_term; first_eq_expr = eq_expr; }
term_or_f_exp ')' { $$ = make_equality(); }
| '(' '<' { require_fluents(); } f_exp f_exp ')'
{ $$ = &LessThan::make(*$4, *$5); }
| '(' LE { require_fluents(); } f_exp f_exp ')'
{ $$ = &LessThanOrEqualTo::make(*$4, *$5); }
| '(' GE { require_fluents(); } f_exp f_exp ')'
{ $$ = &GreaterThanOrEqualTo::make(*$4, *$5); }
| '(' '>' { require_fluents(); } f_exp f_exp ')'
{ $$ = &GreaterThan::make(*$4, *$5); }
| '(' not formula ')' { $$ = make_negation(*$3); }
| '(' and conjuncts ')' { $$ = $3; }
| '(' or { require_disjunction(); } disjuncts ')' { $$ = $4; }
| '(' imply formula formula ')' { $$ = make_implication(*$3, *$4); }
| '(' exists { prepare_exists(); } '(' variables ')' formula ')'
{ $$ = make_exists(*$7); }
| '(' forall { prepare_forall(); } '(' variables ')' formula ')'
{ $$ = make_forall(*$7); }
;
conjuncts : /* empty */ { $$ = &StateFormula::TRUE; }
| conjuncts formula { $$ = &(*$1 && *$2); }
;
disjuncts : /* empty */ { $$ = &StateFormula::FALSE; }
| disjuncts formula { $$ = &(*$1 || *$2); }
;
atomic_term_formula : '(' predicate { prepare_atom($2); } terms ')'
{ $$ = make_atom(); }
| predicate { prepare_atom($1); $$ = make_atom(); }
;
atomic_name_formula : '(' predicate { prepare_atom($2); } names ')'
{ $$ = make_atom(); }
| predicate { prepare_atom($1); $$ = make_atom(); }
;
/* ====================================================================== */
/* Function expressions. */
f_exp : NUMBER { $$ = new Value(*$1); delete $1; }
| '(' '+' f_exp f_exp ')' { $$ = &Addition::make(*$3, *$4); }
| '(' '-' f_exp opt_f_exp ')' { $$ = make_subtraction(*$3, $4); }
| '(' '*' f_exp f_exp ')' { $$ = &Multiplication::make(*$3, *$4); }
| '(' '/' f_exp f_exp ')' { $$ = &Division::make(*$3, *$4); }
| f_head { $$ = $1; }
;
term_or_f_exp : NUMBER
{ require_fluents(); eq_expr = new Value(*$1); delete $1; }
| '(' '+' { require_fluents(); } f_exp f_exp ')'
{ eq_expr = &Addition::make(*$4, *$5); }
| '(' '-' { require_fluents(); } f_exp opt_f_exp ')'
{ eq_expr = make_subtraction(*$4, $5); }
| '(' '*' { require_fluents(); } f_exp f_exp ')'
{ eq_expr = &Multiplication::make(*$4, *$5); }
| '(' '/' { require_fluents(); } f_exp f_exp ')'
{ eq_expr = &Division::make(*$4, *$5); }
| '(' function { require_fluents(); prepare_fluent($2); }
terms ')' { eq_expr = make_fluent(); }
| name { make_eq_name($1); }
| variable { eq_term = make_term($1); eq_expr = 0; }
;
opt_f_exp : /* empty */ { $$ = 0; }
| f_exp
;
f_head : '(' function { prepare_fluent($2); } terms ')' { $$ = make_fluent(); }
| function { prepare_fluent($1); $$ = make_fluent(); }
;
ground_f_exp : NUMBER { $$ = new Value(*$1); delete $1; }
| '(' '+' ground_f_exp ground_f_exp ')'
{ $$ = &Addition::make(*$3, *$4); }
| '(' '-' ground_f_exp opt_ground_f_exp ')'
{ $$ = make_subtraction(*$3, $4); }
| '(' '*' ground_f_exp ground_f_exp ')'
{ $$ = &Multiplication::make(*$3, *$4); }
| '(' '/' ground_f_exp ground_f_exp ')'
{ $$ = &Division::make(*$3, *$4); }
| ground_f_head { $$ = $1; }
| '(' TOTAL_TIME ')'
{ $$ = &Fluent::make(domain->total_time(), TermList()); }
| TOTAL_TIME
{ $$ = &Fluent::make(domain->total_time(), TermList()); }
| '(' GOAL_ACHIEVED ')'
{ $$ = &Fluent::make(domain->goal_achieved(), TermList()); }
| GOAL_ACHIEVED
{ $$ = &Fluent::make(domain->goal_achieved(), TermList()); }
;
opt_ground_f_exp : /* empty */ { $$ = 0; }
| ground_f_exp
;
ground_f_head : '(' function { prepare_fluent($2); } names ')'
{ $$ = make_fluent(); }
| function { prepare_fluent($1); $$ = make_fluent(); }
;
/* ====================================================================== */
/* Terms and types. */
terms : /* empty */
| terms name { add_term($2); }
| terms variable { add_term($2); }
;
names : /* empty */
| names name { add_term($2); }
;
variables : /* empty */
| variable_seq { add_variables($1, TypeTable::OBJECT); }
| variable_seq type_spec { add_variables($1, *$2); delete $2; }
variables
;
variable_seq : variable { $$ = new std::vector<const std::string*>(1, $1); }
| variable_seq variable { $$ = $1; $$->push_back($2); }
;
typed_names : /* empty */
| name_seq { add_names($1, TypeTable::OBJECT); }
| name_seq type_spec { add_names($1, *$2); delete $2; } typed_names
;
name_seq : name { $$ = new std::vector<const std::string*>(1, $1); }
| name_seq name { $$ = $1; $$->push_back($2); }
;
type_spec : '-' { require_typing(); } type { $$ = $3; }
;
type : object { $$ = new Type(TypeTable::OBJECT); }
| type_name { $$ = new Type(make_type($1)); }
| '(' either types ')' { $$ = new Type(make_type(*$3)); delete $3; }
;
types : object { $$ = new TypeSet(); }
| type_name { $$ = new TypeSet(); $$->insert(make_type($1)); }
| types object { $$ = $1; }
| types type_name { $$ = $1; $$->insert(make_type($2)); }
;
function_type : number
;
/* ====================================================================== */
/* Tokens. */
define : DEFINE { delete $1; }
;
domain : DOMAIN_TOKEN { delete $1; }
;
problem : PROBLEM { delete $1; }
;
when : WHEN { delete $1; }
;
not : NOT { delete $1; }
;
and : AND { delete $1; }
;
or : OR { delete $1; }
;
imply : IMPLY { delete $1; }
;
exists : EXISTS { delete $1; }
;
forall : FORALL { delete $1; }
;
probabilistic : PROBABILISTIC { delete $1; }
;
assign : ASSIGN { delete $1; }
;
scale_up : SCALE_UP { delete $1; }
;
scale_down : SCALE_DOWN { delete $1; }
;
increase : INCREASE { delete $1; }
;
decrease : DECREASE { delete $1; }
;
minimize : MINIMIZE { delete $1; }
;
maximize : MAXIMIZE { delete $1; }
;
number : NUMBER_TOKEN { delete $1; }
;
object : OBJECT_TOKEN { delete $1; }
;
either : EITHER { delete $1; }
;
type_name : DEFINE | DOMAIN_TOKEN | PROBLEM
| EITHER
| MINIMIZE | MAXIMIZE
| NAME
;
predicate : type_name
| OBJECT_TOKEN | NUMBER_TOKEN
;
function : name
;
name : DEFINE | DOMAIN_TOKEN | PROBLEM
| NUMBER_TOKEN | OBJECT_TOKEN | EITHER
| WHEN | NOT | AND | OR | IMPLY | EXISTS | FORALL | PROBABILISTIC
| ASSIGN | SCALE_UP | SCALE_DOWN | INCREASE | DECREASE
| MINIMIZE | MAXIMIZE
| NAME
;
variable : VARIABLE
;
%%
/* Outputs an error message. */
static void yyerror(const std::string& s) {
std::cerr << PACKAGE ":" << current_file << ':' << line_number << ": " << s
<< std::endl;
success = false;
}
/* Outputs a warning. */
static void yywarning(const std::string& s) {
if (warning_level > 0) {
std::cerr << PACKAGE ":" << current_file << ':' << line_number << ": " << s
<< std::endl;
if (warning_level > 1) {
success = false;
}
}
}
/* Creates an empty domain with the given name. */
static void make_domain(const std::string* name) {
domain = new Domain(*name);
domains[*name] = domain;
requirements = &domain->requirements;
problem = 0;
delete name;
}
/* Creates an empty problem with the given name. */
static void make_problem(const std::string* name,
const std::string* domain_name) {
std::map<std::string, Domain*>::const_iterator di =
domains.find(*domain_name);
if (di != domains.end()) {
domain = (*di).second;
} else {
domain = new Domain(*domain_name);
domains[*domain_name] = domain;
yyerror("undeclared domain `" + *domain_name + "' used");
}
requirements = new Requirements(domain->requirements);
problem = new Problem(*name, *domain);
const Fluent& total_time_fluent =
Fluent::make(domain->total_time(), TermList());
const Update* total_time_update =
new Assign(total_time_fluent, *new Value(0));
problem->add_init_effect(UpdateEffect::make(*total_time_update));
const Fluent& goal_achieved_fluent =
Fluent::make(domain->goal_achieved(), TermList());
const Update* goal_achieved_update =
new Assign(goal_achieved_fluent, *new Value(0));
problem->add_init_effect(UpdateEffect::make(*goal_achieved_update));
if (requirements->rewards) {
reward_function = *domain->functions().find_function("reward");
const Fluent& reward_fluent = Fluent::make(reward_function, TermList());
const Update* reward_update = new Assign(reward_fluent, *new Value(0));
problem->add_init_effect(UpdateEffect::make(*reward_update));
}
delete name;
delete domain_name;
}
/* Adds :typing to the requirements. */
static void require_typing() {
if (!requirements->typing) {
yywarning("assuming `:typing' requirement");
requirements->typing = true;
}
}
/* Adds :fluents to the requirements. */
static void require_fluents() {
if (!requirements->fluents) {
yywarning("assuming `:fluents' requirement");
requirements->fluents = true;
}
}
/* Adds :disjunctive-preconditions to the requirements. */
static void require_disjunction() {
if (!requirements->disjunctive_preconditions) {
yywarning("assuming `:disjunctive-preconditions' requirement");
requirements->disjunctive_preconditions = true;
}
}
/* Adds :conditional-effects to the requirements. */
static void require_conditional_effects() {
if (!requirements->conditional_effects) {
yywarning("assuming `:conditional-effects' requirement");
requirements->conditional_effects = true;
}
}
/* Returns a simple type with the given name. */
static const Type& make_type(const std::string* name) {
const Type* t = domain->types().find_type(*name);
if (t == 0) {
t = &domain->types().add_type(*name);
if (name_kind != TYPE_KIND) {
yywarning("implicit declaration of type `" + *name + "'");
}
}
delete name;
return *t;
}
/* Returns the union of the given types. */
static Type make_type(const TypeSet& types) {
return TypeTable::union_type(types);
}
/* Returns a simple term with the given name. */
static Term make_term(const std::string* name) {
if ((*name)[0] == '?') {
const Variable* vp = context.find(*name);
if (vp != 0) {