/
Souffle.g4
734 lines (657 loc) · 13.4 KB
/
Souffle.g4
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
//Adopted from original souffle lexer and grammar (bison) at:
//https://github.com/souffle-lang/souffle/tree/master/src/parser
grammar Souffle;
tokens { PREPROCESSOR_ID }
@lexer::header{
import java.util.*;
}
@lexer::members {
Set<String> defines;
public SouffleLexer(CharStream input, Set<String> defines){
this(input);
this.defines = defines;
}
}
WS : [ \t\r\n]+ -> skip; // skip spaces, tabs, newlines
//Directives
DECL: '.decl'WS;
FUNCTOR: '.functor'WS;
INPUT_DECL: '.input'WS;
OUTPUT_DECL: '.output'WS;
PRINTSIZE_DECL:'.printsize'WS;
LIMITSIZE_DECL:'.limitsize'WS;
TYPE:'.type'WS;
COMPONENT:'.comp'WS;
INSTANTIATE:'.init'WS;
NUMBER_TYPE:'.number_type'WS;
SYMBOL_TYPE:'.symbol_type'WS;
OVERRIDE:'.override'WS;
PRAGMA:'.pragma'WS;
PLAN:'.plan'WS;
INCLUDE: '.include'WS;
ONCE:'.once'WS;
FOLD: 'fold'WS;
//Keywords
AUTOINC:'autoinc';
BW_AND:'band';
BW_OR:'bor';
BW_XOR:'bxor';
BW_NOT:'bnot';
BW_SHIFT_L:'bshl';
BW_SHIFT_R:'bshr';
BW_SHIFT_R_UNSIGNED:'bshru';
L_AND:'land';
L_OR:'lor';
L_XOR:'lxor';
L_NOT:'lnot';
TMATCH:'match';
MEAN:'mean';
CAT:'cat';
ORD:'ord';
RANGE:'range';
STRLEN:'strlen';
SUBSTR:'substr';
STATEFUL:'stateful';
TCONTAINS:'contains';
OUTPUT_QUALIFIER:'output';
INPUT_QUALIFIER:'input';
OVERRIDABLE_QUALIFIER:'overridable';
PRINTSIZE_QUALIFIER:'printsize';
EQREL_QUALIFIER:'eqrel';
INLINE_QUALIFIER:'inline';
NO_INLINE_QUALIFIER:'no_inline';
MAGIC_QUALIFIER:'magic';
NO_MAGIC_QUALIFIER:'no_magic';
BRIE_QUALIFIER:'brie';
BTREE_DELETE_QUALIFIER:'btree_delete';
BTREE_QUALIFIER:'btree';
MIN:'min';
MAX:'max';
AS:'as';
NIL:'nil';
UNDERSCORE:'_';
COUNT:'count';
SUM:'sum';
TRUELIT:'true';
FALSELIT:'false';
TOFLOAT:'to_float';
TONUMBER:'to_number';
TOSTRING:'to_string';
TOUNSIGNED:'to_unsigned';
CHOICEDOMAIN:'choice-domain';
//Symbols
PIPE:'|';
LBRACKET:'[';
RBRACKET:']';
DOLLAR:'$';
PLUS:'+';
MINUS:'-';
LPAREN:'(';
RPAREN:')';
COMMA:',';
COLON:':';
SEMICOLON:';';
SUBTYPE:'<:';
LE:'<=';
GE:'>=';
NE:'!=';
EQUALS:'=';
EXCLAMATION:'!';
STAR:'*';
AT:'@';
SLASH:'/';
CARET:'^';
PERCENT:'%';
LBRACE:'{';
RBRACE:'}';
LT:'<';
GT:'>';
IF:':-';
DOT:'.';
NUMBER:[0-9]+'.'[0-9]+'.'[0-9]+'.'[0-9]+
| [0-9]+
| '0b'[0-1]+
| '0x'HEX+
;
FLOAT:[0-9]+[.][0-9]+;
UNSIGNED: [0-9]+'u'
| '0b'[0-1]+'u'
| '0x'[a-fA-F0-9]+'u'
;
IDENT: [?a-zA-Z]|[_?a-zA-Z][_?a-zA-Z0-9]+ {if(defines.contains(getText())) setType(SouffleParser.PREPROCESSOR_ID);} ;
STRING: '"' (ESC | ~["\\])* '"';
fragment ESC :
'\\' (["\\/bfnrt] | UNICODE) ;
fragment UNICODE : 'u' HEX HEX HEX HEX ;
fragment HEX : [0-9a-fA-F] ;
COMMENT: '/*' .*? '*/' -> channel(HIDDEN);
LINE_COMMENT: '//' ~[\r\n]* -> channel(HIDDEN)
;
INCLUDE_PRE: '#include' STRING [\r\n]*-> skip;
PREPROCESSOR_PRAGMA: '#pragma' IDENT [\r\n]*-> skip;
PREPROCESSOR_DEFINE: '#define' .+? [\n\r]-> skip;
PREPROCESSOR_MULTILINE: '#define' .+? (.*? '\\')* [\n\r]-> skip;
PREPROCESSOR_MULTILINE_CONT: PREPROCESSOR_MULTILINE .*? [\n\r]+-> skip;
IF_PRE: '#if' | '#ifndef' | '#ifdef';
ENDIF : '#endif' ->skip;
ELSE_PRE: '#else'->skip;
PREPROCESSOR_IF: IF_PRE .*? [\r\n]->skip;
PREPROCESSOR_ELSE: ELSE_PRE .*? ENDIF->skip;
PREPROCESSOR: '#' .+? [\r\n]+-> skip;
program
: unit EOF
;
/**
* Top-level Program Elements
*/
unit
:
| unit directive_head
| unit souffle_rule
| unit fact
| unit component_decl
| unit component_init
| unit pragma
| unit type_decl
| unit functor_decl
| unit relation_decl
;
/**
* A Qualified Name
*/
qualified_name
: IDENT
| qualified_name DOT IDENT
| preprocessor_macro
;
/**
* Type Declarations
*/
type_decl
: TYPE IDENT SUBTYPE qualified_name
| TYPE IDENT EQUALS union_type_list
| TYPE IDENT EQUALS record_type_list
| TYPE IDENT EQUALS adt_branch_list
/* Deprecated Type Declarations */
| NUMBER_TYPE IDENT
| SYMBOL_TYPE IDENT
| TYPE IDENT
;
/* Attribute definition of a relation */
/* specific wrapper to ensure the err msg says "expected ',' or ')'" */
record_type_list
: LBRACKET RBRACKET
| LBRACKET non_empty_attributes RBRACKET
;
/* Union type argument declarations */
union_type_list
: qualified_name
| union_type_list PIPE qualified_name
;
adt_branch_list
: adt_branch
| adt_branch_list PIPE adt_branch
;
adt_branch
: IDENT LBRACE RBRACE
| IDENT LBRACE non_empty_attributes RBRACE
;
/**
* Relations
*/
/**
* Relation Declaration
*/
relation_decl
: DECL relation_names attributes_list relation_tags dependency_list
;
/**
* Relation Names
*/
relation_names
: IDENT
| relation_names COMMA IDENT
;
/**
* Attributes
*/
attributes_list
: LPAREN RPAREN
| LPAREN non_empty_attributes RPAREN
;
non_empty_attributes
: attribute
| non_empty_attributes COMMA attribute
;
attribute
: IDENT COLON qualified_name
;
/**
* Relation Tags
*/
relation_tags
:
| relation_tags OVERRIDABLE_QUALIFIER
| relation_tags INLINE_QUALIFIER
| relation_tags NO_INLINE_QUALIFIER
| relation_tags MAGIC_QUALIFIER
| relation_tags NO_MAGIC_QUALIFIER
| relation_tags BRIE_QUALIFIER
| relation_tags BTREE_QUALIFIER
| relation_tags BTREE_DELETE_QUALIFIER
| relation_tags EQREL_QUALIFIER
/* Deprecated Qualifiers */
| relation_tags OUTPUT_QUALIFIER
| relation_tags INPUT_QUALIFIER
| relation_tags PRINTSIZE_QUALIFIER
;
/**
* Attribute Name List
*/
non_empty_attribute_names
: IDENT
| non_empty_attribute_names COMMA IDENT
| preprocessor_macro
;
/**
* Functional Dependency Constraint
*/
dependency
: IDENT
| LPAREN non_empty_attribute_names RPAREN
;
dependency_list_aux
: dependency
| dependency_list_aux COMMA dependency
;
dependency_list
:
| CHOICEDOMAIN dependency_list_aux
;
/**
* Datalog Rule Structure
*/
/**
* Fact
*/
fact
: atom DOT
| preprocessor_macro DOT?
;
/**
* Rule
*/
souffle_rule
: rule_def
| rule_def query_plan
| atom LE atom IF body DOT
| atom LE atom IF body DOT query_plan
;
/**
* Rule Definition
*/
rule_def
: head IF body DOT
;
/**
* Rule Head
*/
head
: atom
| head COMMA atom
;
/**
* Rule Body
*/
body
: disjunction
;
disjunction
: conjunction
| disjunction SEMICOLON conjunction
;
conjunction
: term
| conjunction COMMA term
;
/**
* Terms in Rule Bodies
*/
term
: atom
| constraint
| LPAREN disjunction RPAREN
| EXCLAMATION term
| preprocessor_macro
;
/**
* Rule body atom
*/
atom
: qualified_name LPAREN arg_list RPAREN
;
/**
* Literal Constraints
*/
constraint
/* binary infix constraints */
: arg LT arg
| arg GT arg
| arg LE arg
| arg GE arg
| arg EQUALS arg
| arg NE arg
/* binary prefix constraints */
| TMATCH LPAREN arg COMMA arg RPAREN
| TCONTAINS LPAREN arg COMMA arg RPAREN
/* zero-arity constraints */
| TRUELIT
| FALSELIT
| preprocessor_macro
;
/**
* Argument List
*/
arg_list
:
| non_empty_arg_list
;
non_empty_arg_list
: arg
| non_empty_arg_list COMMA arg
;
/**
* Atom argument
*/
arg
: STRING
| FLOAT
| UNSIGNED
| NUMBER
| UNDERSCORE
| DOLLAR
| AUTOINC LPAREN RPAREN
| IDENT
| NIL
| LBRACKET arg_list RBRACKET
| DOLLAR qualified_name LPAREN arg_list RPAREN
| LPAREN arg RPAREN
| AS LPAREN arg COMMA qualified_name RPAREN
| AT IDENT LPAREN arg_list RPAREN
| functor_built_in LPAREN arg_list RPAREN
/* some aggregates have the same name as functors */
| aggregate_func LPAREN arg COMMA non_empty_arg_list RPAREN
/* -- intrinsic functor -- */
/* unary functors */
| MINUS arg
| BW_NOT arg
| L_NOT arg
/* binary infix functors */
| arg PLUS arg
| arg MINUS arg
| arg STAR arg
| arg SLASH arg
| arg PERCENT arg
| arg CARET arg
| arg L_AND arg
| arg L_OR arg
| arg L_XOR arg
| arg BW_AND arg
| arg BW_OR arg
| arg BW_XOR arg
| arg BW_SHIFT_L arg
| arg BW_SHIFT_R arg
| arg BW_SHIFT_R_UNSIGNED arg
/* -- User-defined aggregators -- */
| AT AT IDENT arg_list COLON arg COMMA aggregate_body
/* -- aggregators -- */
| aggregate_func arg_list COLON aggregate_body
| preprocessor_macro
;
functor_built_in
: CAT
| ORD
| RANGE
| STRLEN
| SUBSTR
| TOFLOAT
| TONUMBER
| TOSTRING
| TOUNSIGNED
;
aggregate_func
: COUNT
| MAX
| MEAN
| MIN
| SUM
;
aggregate_body
: LBRACE body RBRACE
| atom
;
/**
* Query Plan
*/
query_plan
: PLAN query_plan_list
;
query_plan_list
: NUMBER COLON plan_order
| query_plan_list COMMA NUMBER COLON plan_order
;
plan_order
: LPAREN RPAREN
| LPAREN non_empty_plan_order_list RPAREN
;
non_empty_plan_order_list
: NUMBER
| non_empty_plan_order_list COMMA NUMBER
;
/**
* Components
*/
/**
* Component Declaration
*/
component_decl
: component_head LBRACE component_body RBRACE
;
/**
* Component Head
*/
component_head
: COMPONENT component_type
| component_head COLON component_type
| component_head COMMA component_type
;
/**
* Component Type
*/
component_type
: IDENT component_type_params
| PREPROCESSOR_ID component_type_params
;
/**
* Component Parameters
*/
component_type_params
:
| LT component_param_list GT
;
/**
* Component Parameter List
*/
component_param_list
: IDENT
| component_param_list COMMA IDENT
| preprocessor_macro
;
/**
* Component body
*/
component_body
:
| component_body directive_head
| component_body souffle_rule
| component_body fact
| component_body OVERRIDE IDENT
| component_body component_init
| component_body component_decl
| component_body type_decl
| component_body relation_decl
;
/**
* Component Initialisation
*/
component_init
: INSTANTIATE IDENT EQUALS component_type
;
/**
* User-Defined Functors
*/
/**
* Functor declaration
*/
functor_decl
: FUNCTOR IDENT LPAREN functor_arg_type_list RPAREN COLON qualified_name
| FUNCTOR IDENT LPAREN functor_arg_type_list RPAREN COLON qualified_name STATEFUL
;
/**
* Functor argument list type
*/
functor_arg_type_list
:
| non_empty_functor_arg_type_list
;
non_empty_functor_arg_type_list
: functor_attribute
| non_empty_functor_arg_type_list COMMA functor_attribute
;
functor_attribute
: qualified_name
| IDENT COLON qualified_name
;
/**
* Other Directives
*/
/**
* Pragma Directives
*/
pragma
: PRAGMA STRING STRING
| PRAGMA STRING
;
/**
* Directives
*/
directive_head
: directive_head_decl directive_list
;
directive_head_decl
: INPUT_DECL
| OUTPUT_DECL
| PRINTSIZE_DECL
| LIMITSIZE_DECL
;
/**
* Directive List
*/
directive_list
: relation_directive_list
| relation_directive_list LPAREN RPAREN
| relation_directive_list LPAREN non_empty_key_value_pairs RPAREN
;
/**
* Directive List
*/
relation_directive_list
: qualified_name
| relation_directive_list COMMA qualified_name
;
/**
* Key-value Pairs
*/
non_empty_key_value_pairs
: IDENT EQUALS kvp_value
| non_empty_key_value_pairs COMMA IDENT EQUALS kvp_value
;
kvp_value
: STRING
| IDENT
| NUMBER
| TRUELIT
| FALSELIT
| preprocessor_macro
;
preprocessor_macro: PREPROCESSOR_ID
| PREPROCESSOR_ID LPAREN macro_args RPAREN
| PREPROCESSOR_IF;
macro_args:
| non_empty_macro_args;
non_empty_macro_args: macro_arg
| non_empty_macro_args COMMA macro_arg;
macro_arg: STRING
| FLOAT
| UNSIGNED
| NUMBER
| UNDERSCORE
| DOLLAR
| AUTOINC LPAREN RPAREN
| IDENT
| NIL
| LBRACKET macro_args RBRACKET
| DOLLAR qualified_name LPAREN macro_args RPAREN
| LPAREN macro_args RPAREN
| AS LPAREN macro_arg COMMA qualified_name RPAREN
| AT IDENT LPAREN macro_args RPAREN
| functor_built_in LPAREN macro_args RPAREN
/* some aggregates have the same name as functors */
| aggregate_func LPAREN macro_arg COMMA non_empty_arg_list RPAREN
/* -- intrinsic functor -- */
/* unary functors */
| MINUS macro_arg
| BW_NOT macro_arg
| L_NOT macro_arg
| EXCLAMATION macro_arg
| IDENT DOT macro_arg
| AT IDENT
| macro_arg SEMICOLON macro_arg
| macro_arg LT macro_arg
| macro_arg GT macro_arg
| macro_arg LE macro_arg
| macro_arg GE macro_arg
| macro_arg EQUALS macro_arg
| macro_arg NE macro_arg
/* binary prefix constraints */
| TMATCH LPAREN macro_arg COMMA macro_arg RPAREN
| TCONTAINS LPAREN macro_arg COMMA macro_arg RPAREN
/* zero-arity constraints */
| TRUELIT
| FALSELIT
| EQUALS
| LE
| GE
| LT
| GT
| NE
/* binary infix functors */
| macro_arg PLUS macro_arg
| macro_arg MINUS macro_arg
| macro_arg STAR macro_arg
| macro_arg SLASH macro_arg
| macro_arg PERCENT macro_arg
| macro_arg CARET macro_arg
| macro_arg L_AND macro_arg
| macro_arg L_OR macro_arg
| macro_arg L_XOR macro_arg
| macro_arg BW_AND macro_arg
| macro_arg BW_OR macro_arg
| macro_arg BW_XOR macro_arg
| macro_arg BW_SHIFT_L macro_arg
| macro_arg BW_SHIFT_R macro_arg
| macro_arg BW_SHIFT_R_UNSIGNED macro_arg
/* -- aggregators -- */
| aggregate_func macro_args COLON aggregate_body
| IDENT LPAREN macro_args RPAREN
| preprocessor_macro;