-
Notifications
You must be signed in to change notification settings - Fork 20
/
antlr3-Pddl.g
541 lines (439 loc) · 11.1 KB
/
antlr3-Pddl.g
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
/**
* PDDL grammar for ANTLR v3
* Zeyn Saigol
* School of Computer Science
* University of Birmingham
*
* $Id: Pddl.g 120 2008-10-02 14:59:50Z zas $
*/
grammar Pddl;
options {
output=AST;
backtrack=true;
//k=4;
}
tokens {
DOMAIN;
DOMAIN_NAME;
REQUIREMENTS;
TYPES;
EITHER_TYPE;
CONSTANTS;
FUNCTIONS;
PREDICATES;
ACTION;
DURATIVE_ACTION;
PROBLEM;
PROBLEM_NAME;
PROBLEM_DOMAIN;
OBJECTS;
INIT;
FUNC_HEAD;
PRECONDITION;
EFFECT;
AND_GD;
OR_GD;
NOT_GD;
IMPLY_GD;
EXISTS_GD;
FORALL_GD;
COMPARISON_GD;
AND_EFFECT;
FORALL_EFFECT;
WHEN_EFFECT;
ASSIGN_EFFECT;
NOT_EFFECT;
PRED_HEAD;
GOAL;
BINARY_OP;
UNARY_MINUS;
INIT_EQ;
INIT_AT;
NOT_PRED_INIT;
PRED_INST;
PROBLEM_CONSTRAINT;
PROBLEM_METRIC;
}
@parser::header {package uk.ac.bham.cs.zas.pddl.antlr;}
@parser::members {
private boolean wasError = false;
public void reportError(RecognitionException e) {
wasError = true;
super.reportError(e);
}
public boolean invalidGrammar() {
return wasError;
}
}
// Standard way of disabling the default error handler, and throwing Exceptions instead:
//@rulecatch { }
//@members {
// // raise exception, rather than recovering, on mismatched token within alt
// protected void mismatch(IntStream input, int ttype, BitSet follow)
// throws RecognitionException
// {
// throw new MismatchedTokenException(ttype, input);
// }
//}
@lexer::header {package uk.ac.bham.cs.zas.pddl.antlr;}
/************* Start of grammar *******************/
pddlDoc : domain | problem;
/************* DOMAINS ****************************/
domain
: '(' 'define' domainName
requireDef?
typesDef?
constantsDef?
predicatesDef?
functionsDef?
constraints?
structureDef*
')'
-> ^(DOMAIN domainName requireDef? typesDef?
constantsDef? predicatesDef? functionsDef?
constraints? structureDef*)
;
domainName
: '(' 'domain' NAME ')'
-> ^(DOMAIN_NAME NAME)
;
requireDef
: '(' ':requirements' REQUIRE_KEY+ ')'
-> ^(REQUIREMENTS REQUIRE_KEY+)
;
typesDef
: '(' ':types' typedNameList ')'
-> ^(TYPES typedNameList)
;
// If have any typed names, they must come FIRST!
typedNameList
: (NAME* | singleTypeNameList+ NAME*)
;
singleTypeNameList
: (NAME+ '-' t=type)
-> ^(NAME $t)+
;
type
: ( '(' 'either' primType+ ')' )
-> ^(EITHER_TYPE primType+)
| primType
;
primType : NAME ;
functionsDef
: '(' ':functions' functionList ')'
-> ^(FUNCTIONS functionList)
;
functionList
: (atomicFunctionSkeleton+ ('-' functionType)? )*
;
atomicFunctionSkeleton
: '('! functionSymbol^ typedVariableList ')'!
;
functionSymbol : NAME ;
functionType : 'number' ; // Currently in PDDL only numeric functions are allowed
constantsDef
: '(' ':constants' typedNameList ')'
-> ^(CONSTANTS typedNameList)
;
predicatesDef
: '(' ':predicates' atomicFormulaSkeleton+ ')'
-> ^(PREDICATES atomicFormulaSkeleton+)
;
atomicFormulaSkeleton
: '('! predicate^ typedVariableList ')'!
;
predicate : NAME ;
// If have any typed variables, they must come FIRST!
typedVariableList
: (VARIABLE* | singleTypeVarList+ VARIABLE*)
;
singleTypeVarList
: (VARIABLE+ '-' t=type)
-> ^(VARIABLE $t)+
;
constraints
: '('! ':constraints'^ conGD ')'!
;
structureDef
: actionDef
| durativeActionDef
| derivedDef
;
/************* ACTIONS ****************************/
actionDef
: '(' ':action' actionSymbol
':parameters' '(' typedVariableList ')'
actionDefBody ')'
-> ^(ACTION actionSymbol typedVariableList actionDefBody)
;
actionSymbol : NAME ;
// Should allow preGD instead of goalDesc for preconditions -
// but I can't get the LL(*) parsing to work
// This means 'preference' preconditions cannot be used
actionDefBody
: ( ':precondition' (('(' ')') | goalDesc))?
( ':effect' (('(' ')') | effect))?
-> ^(PRECONDITION goalDesc?) ^(EFFECT effect?)
;
//preGD
// : prefGD
// | '(' 'and' preGD* ')'
// | '(' 'forall' '(' typedVariableList ')' preGD ')'
// ;
//
//prefGD
// : '(' 'preference' NAME? goalDesc ')'
// | goalDesc
// ;
goalDesc
: atomicTermFormula
| '(' 'and' goalDesc* ')'
-> ^(AND_GD goalDesc*)
| '(' 'or' goalDesc* ')'
-> ^(OR_GD goalDesc*)
| '(' 'not' goalDesc ')'
-> ^(NOT_GD goalDesc)
| '(' 'imply' goalDesc goalDesc ')'
-> ^(IMPLY_GD goalDesc goalDesc)
| '(' 'exists' '(' typedVariableList ')' goalDesc ')'
-> ^(EXISTS_GD typedVariableList goalDesc)
| '(' 'forall' '(' typedVariableList ')' goalDesc ')'
-> ^(FORALL_GD typedVariableList goalDesc)
| fComp
-> ^(COMPARISON_GD fComp)
;
fComp
: '('! binaryComp fExp fExp ')'!
;
atomicTermFormula
: '(' predicate term* ')' -> ^(PRED_HEAD predicate term*)
;
term : NAME | VARIABLE ;
/************* DURATIVE ACTIONS ****************************/
durativeActionDef
: '(' ':durative-action' actionSymbol
':parameters' '(' typedVariableList ')'
daDefBody ')'
-> ^(DURATIVE_ACTION actionSymbol typedVariableList daDefBody)
;
daDefBody
: ':duration' durationConstraint
| ':condition' (('(' ')') | daGD)
| ':effect' (('(' ')') | daEffect)
;
daGD
: prefTimedGD
| '(' 'and' daGD* ')'
| '(' 'forall' '(' typedVariableList ')' daGD ')'
;
prefTimedGD
: timedGD
| '(' 'preference' NAME? timedGD ')'
;
timedGD
: '(' 'at' timeSpecifier goalDesc ')'
| '(' 'over' interval goalDesc ')'
;
timeSpecifier : 'start' | 'end' ;
interval : 'all' ;
/************* DERIVED DEFINITIONS ****************************/
derivedDef
: '('! ':derived'^ typedVariableList goalDesc ')'!
;
/************* EXPRESSIONS ****************************/
fExp
: NUMBER
| '(' binaryOp fExp fExp2 ')' -> ^(BINARY_OP binaryOp fExp fExp2)
| '(' '-' fExp ')' -> ^(UNARY_MINUS fExp)
| fHead
;
// This is purely a workaround for an ANTLR bug in tree construction
// http://www.antlr.org/wiki/display/ANTLR3/multiple+occurences+of+a+token+mix+up+the+list+management+in+tree+rewrites
fExp2 : fExp ;
fHead
: '(' functionSymbol term* ')' -> ^(FUNC_HEAD functionSymbol term*)
| functionSymbol -> ^(FUNC_HEAD functionSymbol)
;
effect
: '(' 'and' cEffect* ')' -> ^(AND_EFFECT cEffect*)
| cEffect
;
cEffect
: '(' 'forall' '(' typedVariableList ')' effect ')'
-> ^(FORALL_EFFECT typedVariableList effect)
| '(' 'when' goalDesc condEffect ')'
-> ^(WHEN_EFFECT goalDesc condEffect)
| pEffect
;
pEffect
: '(' assignOp fHead fExp ')'
-> ^(ASSIGN_EFFECT assignOp fHead fExp)
| '(' 'not' atomicTermFormula ')'
-> ^(NOT_EFFECT atomicTermFormula)
| atomicTermFormula
;
// TODO: why is this different from the "and cEffect" above? Does it matter?
condEffect
: '(' 'and' pEffect* ')' -> ^(AND_EFFECT pEffect*)
| pEffect
;
// TODO: should these be uppercase & lexer section?
binaryOp : '*' | '+' | '-' | '/' ;
binaryComp : '>' | '<' | '=' | '>=' | '<=' ;
assignOp : 'assign' | 'scale-up' | 'scale-down' | 'increase' | 'decrease' ;
/************* DURATIONS ****************************/
durationConstraint
: '(' 'and' simpleDurationConstraint+ ')'
| '(' ')'
| simpleDurationConstraint
;
simpleDurationConstraint
: '(' durOp '?duration' durValue ')'
| '(' 'at' timeSpecifier simpleDurationConstraint ')'
;
durOp : '<=' | '>=' | '=' ;
durValue : NUMBER | fExp ;
daEffect
: '(' 'and' daEffect* ')'
| timedEffect
| '(' 'forall' '(' typedVariableList ')' daEffect ')'
| '(' 'when' daGD timedEffect ')'
| '(' assignOp fHead fExpDA ')'
;
timedEffect
: '(' 'at' timeSpecifier daEffect ')' // BNF has a-effect here, but not defined anywhere
| '(' 'at' timeSpecifier fAssignDA ')'
| '(' assignOp fHead fExp ')' // BNF has assign-op-t and f-exp-t here, but not defined anywhere
;
fAssignDA
: '(' assignOp fHead fExpDA ')'
;
fExpDA
: '(' ((binaryOp fExpDA fExpDA) | ('-' fExpDA)) ')'
| '?duration'
| fExp
;
/************* PROBLEMS ****************************/
problem
: '(' 'define' problemDecl
problemDomain
requireDef?
objectDecl?
init
goal
probConstraints?
metricSpec?
// lengthSpec? This is not defined anywhere in the BNF spec
')'
-> ^(PROBLEM problemDecl problemDomain requireDef? objectDecl?
init goal probConstraints? metricSpec?)
;
problemDecl
: '(' 'problem' NAME ')'
-> ^(PROBLEM_NAME NAME)
;
problemDomain
: '(' ':domain' NAME ')'
-> ^(PROBLEM_DOMAIN NAME)
;
objectDecl
: '(' ':objects' typedNameList ')'
-> ^(OBJECTS typedNameList)
;
init
: '(' ':init' initEl* ')'
-> ^(INIT initEl*)
;
initEl
: nameLiteral
| '(' '=' fHead NUMBER ')' -> ^(INIT_EQ fHead NUMBER)
| '(' 'at' NUMBER nameLiteral ')' -> ^(INIT_AT NUMBER nameLiteral)
;
nameLiteral
: atomicNameFormula
| '(' 'not' atomicNameFormula ')' -> ^(NOT_PRED_INIT atomicNameFormula)
;
atomicNameFormula
: '(' predicate NAME* ')' -> ^(PRED_INST predicate NAME*)
;
// Should allow preGD instead of goalDesc -
// but I can't get the LL(*) parsing to work
// This means 'preference' preconditions cannot be used
//goal : '(' ':goal' preGD ')' -> ^(GOAL preGD);
goal : '(' ':goal' goalDesc ')' -> ^(GOAL goalDesc) ;
probConstraints
: '(' ':constraints' prefConGD ')'
-> ^(PROBLEM_CONSTRAINT prefConGD)
;
prefConGD
: '(' 'and' prefConGD* ')'
| '(' 'forall' '(' typedVariableList ')' prefConGD ')'
| '(' 'preference' NAME? conGD ')'
| conGD
;
metricSpec
: '(' ':metric' optimization metricFExp ')'
-> ^(PROBLEM_METRIC optimization metricFExp)
;
optimization : 'minimize' | 'maximize' ;
metricFExp
: '(' binaryOp metricFExp metricFExp ')'
| '(' ('*'|'/') metricFExp metricFExp+ ')'
| '(' '-' metricFExp ')'
| NUMBER
| '(' functionSymbol NAME* ')'
| functionSymbol
| 'total-time'
| '(' 'is-violated' NAME ')'
;
/************* CONSTRAINTS ****************************/
conGD
: '(' 'and' conGD* ')'
| '(' 'forall' '(' typedVariableList ')' conGD ')'
| '(' 'at' 'end' goalDesc ')'
| '(' 'always' goalDesc ')'
| '(' 'sometime' goalDesc ')'
| '(' 'within' NUMBER goalDesc ')'
| '(' 'at-most-once' goalDesc ')'
| '(' 'sometime-after' goalDesc goalDesc ')'
| '(' 'sometime-before' goalDesc goalDesc ')'
| '(' 'always-within' NUMBER goalDesc goalDesc ')'
| '(' 'hold-during' NUMBER NUMBER goalDesc ')'
| '(' 'hold-after' NUMBER goalDesc ')'
;
/************* LEXER ****************************/
REQUIRE_KEY
: ':strips'
| ':typing'
| ':negative-preconditions'
| ':disjunctive-preconditions'
| ':equality'
| ':existential-preconditions'
| ':universal-preconditions'
| ':quantified-preconditions'
| ':conditional-effects'
| ':fluents'
| ':adl'
| ':durative-actions'
| ':derived-predicates'
| ':timed-initial-literals'
| ':preferences'
| ':constraints'
;
NAME: LETTER ANY_CHAR* ;
fragment LETTER: 'a'..'z' | 'A'..'Z';
fragment ANY_CHAR: LETTER | '0'..'9' | '-' | '_';
VARIABLE : '?' LETTER ANY_CHAR* ;
NUMBER : DIGIT+ ('.' DIGIT+)? ;
fragment DIGIT: '0'..'9';
LINE_COMMENT
: ';' ~('\n'|'\r')* '\r'? '\n' { $channel = HIDDEN; }
;
WHITESPACE
: ( ' '
| '\t'
| '\r'
| '\n'
)+
{ $channel = HIDDEN; }
;