forked from stan-dev/stanc3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.mly
772 lines (704 loc) · 26.1 KB
/
parser.mly
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
(** The parser for Stan. A Menhir file. *)
%{
open Core_kernel
open Middle
open Ast
open Debugging
open Errors
(* Takes a sized_basic_type and a list of sizes and repeatedly applies then
SArray constructor, taking sizes off the list *)
let reducearray (sbt, l) =
List.fold_right l ~f:(fun z y -> SizedType.SArray (y, z)) ~init:sbt
let build_id id loc =
grammar_logger ("identifier " ^ id);
{name=id; id_loc=Location_span.of_positions_exn loc}
let rec iterate_n f x = function
| 0 -> x
| n -> iterate_n f (f x) (n - 1)
let nest_unsized_array basic_type n =
iterate_n (fun t -> UnsizedType.UArray t) basic_type n
%}
%token FUNCTIONBLOCK DATABLOCK TRANSFORMEDDATABLOCK PARAMETERSBLOCK
TRANSFORMEDPARAMETERSBLOCK MODELBLOCK GENERATEDQUANTITIESBLOCK
%token LBRACE RBRACE LPAREN RPAREN LBRACK RBRACK LABRACK RABRACK COMMA SEMICOLON
BAR
%token RETURN IF ELSE WHILE FOR IN BREAK CONTINUE
%token VOID INT REAL VECTOR ROWVECTOR ARRAY MATRIX ORDERED POSITIVEORDERED SIMPLEX
UNITVECTOR CHOLESKYFACTORCORR CHOLESKYFACTORCOV CORRMATRIX COVMATRIX
%token LOWER UPPER OFFSET MULTIPLIER
%token <string> INTNUMERAL
%token <string> REALNUMERAL
%token <string> STRINGLITERAL
%token <string> IDENTIFIER
%token TARGET
%token QMARK COLON BANG MINUS PLUS HAT ELTPOW TRANSPOSE TIMES DIVIDE MODULO IDIVIDE
LDIVIDE ELTTIMES ELTDIVIDE OR AND EQUALS NEQUALS LEQ GEQ TILDE
%token ASSIGN PLUSASSIGN MINUSASSIGN TIMESASSIGN DIVIDEASSIGN
ELTDIVIDEASSIGN ELTTIMESASSIGN
%token ARROWASSIGN INCREMENTLOGPROB GETLP (* all of these are deprecated *)
%token PRINT REJECT
%token TRUNCATE
%token EOF
(* UNREACHABLE tokens will never be produced by the lexer, so we can use them as
"a thing that will never parse". This is useful in a few places. For example,
when we the parser to differentiate between different failing states for
error message purposes, we can partially accept one of them and then fail by
requiring an UNREACHABLE token. That's the approach taken in decl_identifier.
*)
%token UNREACHABLE
%right COMMA
%right QMARK COLON
%left OR
%left AND
%left EQUALS NEQUALS
%left LEQ LABRACK GEQ RABRACK
%left PLUS MINUS
%left TIMES DIVIDE MODULO ELTTIMES ELTDIVIDE
%left IDIVIDE LDIVIDE
%nonassoc unary_over_binary
%right HAT ELTPOW
%left TRANSPOSE
%left LBRACK
%nonassoc below_ELSE
%nonassoc ELSE
(* Top level rule *)
%start <Ast.untyped_program> program
%%
(* Grammar *)
(* program *)
program:
| ofb=option(function_block)
odb=option(data_block)
otdb=option(transformed_data_block)
opb=option(parameters_block)
otpb=option(transformed_parameters_block)
omb=option(model_block)
ogb=option(generated_quantities_block)
EOF
{
grammar_logger "program" ;
{ functionblock= ofb
; datablock= odb
; transformeddatablock= otdb
; parametersblock= opb
; transformedparametersblock= otpb
; modelblock= omb
; generatedquantitiesblock= ogb }
}
(* blocks *)
function_block:
| FUNCTIONBLOCK LBRACE fd=list(function_def) RBRACE
{ grammar_logger "function_block" ; fd}
data_block:
| DATABLOCK LBRACE tvd=list(top_var_decl_no_assign) RBRACE
{ grammar_logger "data_block" ; tvd }
transformed_data_block:
| TRANSFORMEDDATABLOCK LBRACE tvds=list(top_vardecl_or_statement) RBRACE
{ grammar_logger "transformed_data_block" ; tvds }
(* NOTE: this allows mixing of statements and top_var_decls *)
parameters_block:
| PARAMETERSBLOCK LBRACE tvd=list(top_var_decl_no_assign) RBRACE
{ grammar_logger "parameters_block" ; tvd }
transformed_parameters_block:
| TRANSFORMEDPARAMETERSBLOCK LBRACE tvds=list(top_vardecl_or_statement) RBRACE
{ grammar_logger "transformed_parameters_block" ; tvds }
model_block:
| MODELBLOCK LBRACE vds=list(vardecl_or_statement) RBRACE
{ grammar_logger "model_block" ; vds }
generated_quantities_block:
| GENERATEDQUANTITIESBLOCK LBRACE tvds=list(top_vardecl_or_statement) RBRACE
{ grammar_logger "generated_quantities_block" ; tvds }
(* function definitions *)
identifier:
| id=IDENTIFIER { build_id id $loc }
| TRUNCATE { build_id "T" $loc}
| OFFSET { build_id "offset" $loc}
| MULTIPLIER { build_id "multiplier" $loc}
| LOWER { build_id "lower" $loc}
| UPPER { build_id "upper" $loc}
| ARRAY { build_id "array" $loc}
decl_identifier:
| id=identifier { id }
(* The only purpose of the UNREACHABLE rules is to improve the syntax
error messages when a user tries to use a keyword as a variable name.
The rule can never actually be built, but it provides a parser state
that's distinct from the use of other non-identifiers, so we can assign
it a different message in the .messages file.
*)
| FUNCTIONBLOCK UNREACHABLE
| DATABLOCK UNREACHABLE
| PARAMETERSBLOCK UNREACHABLE
| MODELBLOCK UNREACHABLE
| RETURN UNREACHABLE
| IF UNREACHABLE
| ELSE UNREACHABLE
| WHILE UNREACHABLE
| FOR UNREACHABLE
| IN UNREACHABLE
| BREAK UNREACHABLE
| CONTINUE UNREACHABLE
| VOID UNREACHABLE
| INT UNREACHABLE
| REAL UNREACHABLE
| VECTOR UNREACHABLE
| ROWVECTOR UNREACHABLE
| MATRIX UNREACHABLE
| ORDERED UNREACHABLE
| POSITIVEORDERED UNREACHABLE
| SIMPLEX UNREACHABLE
| UNITVECTOR UNREACHABLE
| CHOLESKYFACTORCORR UNREACHABLE
| CHOLESKYFACTORCOV UNREACHABLE
| CORRMATRIX UNREACHABLE
| COVMATRIX UNREACHABLE
| PRINT UNREACHABLE
| REJECT UNREACHABLE
| TARGET UNREACHABLE
| GETLP UNREACHABLE
{
raise (Failure "This should be unreachable; the UNREACHABLE token should \
never be produced")
}
function_def:
| rt=return_type name=decl_identifier LPAREN args=separated_list(COMMA, arg_decl)
RPAREN b=statement
{
grammar_logger "function_def" ;
{stmt=FunDef {returntype = rt; funname = name;
arguments = args; body=b;};
smeta={loc=Location_span.of_positions_exn $loc}
}
}
return_type:
| VOID
{ grammar_logger "return_type VOID" ; Void }
| ut=unsized_type
{ grammar_logger "return_type unsized_type" ; ReturnType ut }
arg_decl:
| od=option(DATABLOCK) ut=unsized_type id=decl_identifier
{ grammar_logger "arg_decl" ;
match od with None -> (UnsizedType.AutoDiffable, ut, id) | _ -> (DataOnly, ut, id) }
always(x):
| x=x
{ Some(x) }
unsized_type:
| ARRAY n_opt=always(unsized_dims) bt=basic_type
| bt=basic_type n_opt=option(unsized_dims)
{ grammar_logger "unsized_type";
nest_unsized_array bt (Option.value n_opt ~default:0)
}
basic_type:
| INT
{ grammar_logger "basic_type INT" ; UnsizedType.UInt }
| REAL
{ grammar_logger "basic_type REAL" ; UnsizedType.UReal }
| VECTOR
{ grammar_logger "basic_type VECTOR" ; UnsizedType.UVector }
| ROWVECTOR
{ grammar_logger "basic_type ROWVECTOR" ; UnsizedType.URowVector }
| MATRIX
{ grammar_logger "basic_type MATRIX" ; UnsizedType.UMatrix }
unsized_dims:
| LBRACK cs=list(COMMA) RBRACK
{ grammar_logger "unsized_dims" ; List.length(cs) + 1 }
(* Never accept this rule, but return the same type as expression *)
no_assign:
| UNREACHABLE
{ (* This code will never be reached *)
raise (Failure "This should be unreachable; the UNREACHABLE token should \
never be produced")
}
optional_assignment(rhs):
| rhs_opt=option(pair(ASSIGN, rhs))
{ Option.map ~f:snd rhs_opt }
id_and_optional_assignment(rhs):
| id=decl_identifier rhs_opt=optional_assignment(rhs)
{ (id, rhs_opt) }
(*
* All rules for declaration statements.
* The first argument matches the type and should return a (type, constraint) pair.
* The second argument matches the RHS expression and should return an expression
* (or use no_assign to never allow a RHS).
*
* The value returned is a function from a bool (is_global, which controls
* whether the declarations should be global variables) to a list of statements
* (which will always be declarations).
*
* The rules match declarations with/without assignments, with/without array
* dimensions, single/multiple identifiers, and dimensions before/after the
* identifier.
*)
decl(type_rule, rhs):
(* This rule matches the old array syntax, e.g:
int x[1,2] = ..;
We need to match it separately because we won't support multiple inline
declarations using this form.
This form is likely TO BE DEPRECIATED in Stan 3
*)
| ty=type_rule id=decl_identifier dims=dims rhs_opt=optional_assignment(rhs)
SEMICOLON
{ (fun ~is_global ->
{ stmt=
VarDecl {
decl_type= Sized (reducearray (fst ty, dims))
; transformation= snd ty
; identifier= id
; initial_value= rhs_opt
; is_global
}
; smeta= {
loc= Location_span.of_positions_exn $loc
}
})
}
(* This rule matches non-array declarations and also the new array syntax, e.g:
array[1,2] int x = ..;
*)
(* Note that the array dimensions option must be inlined with ioption, else
it will conflict with first rule. *)
(* It's a bit of a hack that "array[x,y,z]" is matched with a lhs rule and
then narrowed down by throwing errors. This is done to avoid reserving
"array" as a keyword, while also avoiding the reduce-reduce conflict that
would occur if "array[x,y,z]" were its own rule without reserving the
keyword. *)
| dims_opt=ioption(lhs) ty=type_rule
id_rhs=id_and_optional_assignment(rhs) SEMICOLON
{ (fun ~is_global ->
let int_ix ix = match ix with
| Single e -> Some e
| _ -> None
in
let int_ixs ixs =
List.fold_left
~init:(Some [])
~f:(Option.map2 ~f:(fun ixs ix -> ix::ixs))
(List.map ~f:int_ix
(List.rev ixs))
in
let error message =
pp_syntax_error
Fmt.stderr
(Parsing (message, Location_span.of_positions_exn $loc(dims_opt) ));
exit 1
in
let dims = match dims_opt with
| Some ({expr= Indexed ({expr= Variable {name="array"; _}; _}, ixs); _}) ->
(match int_ixs ixs with
| Some sizes -> sizes
| None -> error "Dimensions should be expressions, not multiple or range indexing.")
| None -> []
| _ -> error "Found a declaration following an expression."
in
let (id, rhs_opt) = id_rhs in
{ stmt=
VarDecl {
decl_type= Sized (reducearray (fst ty, dims))
; transformation= snd ty
; identifier= id
; initial_value= rhs_opt
; is_global
}
; smeta= {
loc=
(* From the docs:
We remark that, if the current production has an empty right-hand side,
then $startpos and $endpos are equal, and (by convention) are the end
position of the most recently parsed symbol (that is, the symbol that
happens to be on top of the automaton’s stack when this production is
reduced). If the current production has a nonempty right-hand side,
then $startpos is the same as $startpos($1) and $endpos is the same
as $endpos($n), where n is the length of the right-hand side.
So when dims_opt is empty, it uses the preview token as its startpos,
but that makes the whole declaration think it starts at the previous
token. Sadly, $sloc and $symbolstartpos generates code using !=, which
Core_kernel considers to be an error.
*)
let startpos = match dims_opt with
| None -> $startpos(ty)
| Some _ -> $startpos
in
Location_span.of_positions_exn (startpos, $endpos)
}
}
)}
var_decl:
| d_fn=decl(sized_basic_type, expression)
{ grammar_logger "var_decl" ;
d_fn ~is_global:false
}
top_var_decl:
| d_fn=decl(top_var_type, expression)
{ grammar_logger "top_var_decl" ;
d_fn ~is_global:true
}
top_var_decl_no_assign:
| d_fn=decl(top_var_type, no_assign)
{ grammar_logger "top_var_decl_no_assign" ;
d_fn ~is_global:true
}
sized_basic_type:
| INT
{ grammar_logger "INT_var_type" ; (SizedType.SInt, Identity) }
| REAL
{ grammar_logger "REAL_var_type" ; (SizedType.SReal, Identity) }
| VECTOR LBRACK e=expression RBRACK
{ grammar_logger "VECTOR_var_type" ; (SizedType.SVector e, Identity) }
| ROWVECTOR LBRACK e=expression RBRACK
{ grammar_logger "ROWVECTOR_var_type" ; (SizedType.SRowVector e , Identity) }
| MATRIX LBRACK e1=expression COMMA e2=expression RBRACK
{ grammar_logger "MATRIX_var_type" ; (SizedType.SMatrix (e1, e2), Identity) }
top_var_type:
| INT r=range_constraint
{ grammar_logger "INT_top_var_type" ; (SInt, r) }
| REAL c=type_constraint
{ grammar_logger "REAL_top_var_type" ; (SReal, c) }
| VECTOR c=type_constraint LBRACK e=expression RBRACK
{ grammar_logger "VECTOR_top_var_type" ; (SVector e, c) }
| ROWVECTOR c=type_constraint LBRACK e=expression RBRACK
{ grammar_logger "ROWVECTOR_top_var_type" ; (SRowVector e, c) }
| MATRIX c=type_constraint LBRACK e1=expression COMMA e2=expression RBRACK
{ grammar_logger "MATRIX_top_var_type" ; (SMatrix (e1, e2), c) }
| ORDERED LBRACK e=expression RBRACK
{ grammar_logger "ORDERED_top_var_type" ; (SVector e, Ordered) }
| POSITIVEORDERED LBRACK e=expression RBRACK
{
grammar_logger "POSITIVEORDERED_top_var_type" ;
(SVector e, PositiveOrdered)
}
| SIMPLEX LBRACK e=expression RBRACK
{ grammar_logger "SIMPLEX_top_var_type" ; (SVector e, Simplex) }
| UNITVECTOR LBRACK e=expression RBRACK
{ grammar_logger "UNITVECTOR_top_var_type" ; (SVector e, UnitVector) }
| CHOLESKYFACTORCORR LBRACK e=expression RBRACK
{
grammar_logger "CHOLESKYFACTORCORR_top_var_type" ;
(SMatrix (e, e), CholeskyCorr)
}
| CHOLESKYFACTORCOV LBRACK e1=expression oe2=option(pair(COMMA, expression))
RBRACK
{
grammar_logger "CHOLESKYFACTORCOV_top_var_type" ;
match oe2 with Some (_,e2) -> ( SMatrix (e1, e2), CholeskyCov)
| _ -> (SMatrix (e1, e1), CholeskyCov)
}
| CORRMATRIX LBRACK e=expression RBRACK
{ grammar_logger "CORRMATRIX_top_var_type" ; (SMatrix (e, e), Correlation) }
| COVMATRIX LBRACK e=expression RBRACK
{ grammar_logger "COVMATRIX_top_var_type" ; (SMatrix (e, e), Covariance) }
type_constraint:
| r=range_constraint
{ grammar_logger "type_constraint_range" ; r }
| LABRACK l=offset_mult RABRACK
{ grammar_logger "type_constraint_offset_mult" ; l }
range_constraint:
| (* nothing *)
{ grammar_logger "empty_constraint" ; Program.Identity }
| LABRACK r=range RABRACK
{ grammar_logger "range_constraint" ; r }
range:
| LOWER ASSIGN e1=constr_expression COMMA UPPER ASSIGN e2=constr_expression
| UPPER ASSIGN e2=constr_expression COMMA LOWER ASSIGN e1=constr_expression
{ grammar_logger "lower_upper_range" ; Program.LowerUpper (e1, e2) }
| LOWER ASSIGN e=constr_expression
{ grammar_logger "lower_range" ; Lower e }
| UPPER ASSIGN e=constr_expression
{ grammar_logger "upper_range" ; Upper e }
offset_mult:
| OFFSET ASSIGN e1=constr_expression COMMA MULTIPLIER ASSIGN e2=constr_expression
| MULTIPLIER ASSIGN e2=constr_expression COMMA OFFSET ASSIGN e1=constr_expression
{ grammar_logger "offset_mult" ; Program.OffsetMultiplier (e1, e2) }
| OFFSET ASSIGN e=constr_expression
{ grammar_logger "offset" ; Offset e }
| MULTIPLIER ASSIGN e=constr_expression
{ grammar_logger "multiplier" ; Multiplier e }
(* arr_dims:
* | ARRAY LBRACK l=separated_nonempty_list(COMMA, expression) RBRACK
* { grammar_logger "array dims" ; l } *)
dims:
| LBRACK l=separated_nonempty_list(COMMA, expression) RBRACK
{ grammar_logger "dims" ; l }
(* expressions *)
%inline expression:
| l=lhs
{
grammar_logger "lhs_expression" ;
l
}
| e=non_lhs
{ grammar_logger "non_lhs_expression" ;
{expr=e;
emeta={loc= Location_span.of_positions_exn $loc}}}
non_lhs:
| e1=expression QMARK e2=expression COLON e3=expression
{ grammar_logger "ifthenelse_expr" ; TernaryIf (e1, e2, e3) }
| e1=expression op=infixOp e2=expression
{ grammar_logger "infix_expr" ; BinOp (e1, op, e2) }
| op=prefixOp e=expression %prec unary_over_binary
{ grammar_logger "prefix_expr" ; PrefixOp (op, e) }
| e=expression op=postfixOp
{ grammar_logger "postfix_expr" ; PostfixOp (e, op)}
| ue=non_lhs LBRACK i=indexes RBRACK
{ grammar_logger "expression_indexed" ;
Indexed ({expr=ue;
emeta={loc= Location_span.of_positions_exn $loc(ue)}}, i)}
| e=common_expression
{ grammar_logger "common_expr" ; e }
(* TODO: why do we not simply disallow greater than in constraints? No need to disallow all logical operations, right? *)
constr_expression:
| e1=constr_expression op=arithmeticBinOp e2=constr_expression
{
grammar_logger "constr_expression_arithmetic" ;
{expr=BinOp (e1, op, e2);
emeta={loc=Location_span.of_positions_exn $loc}
}
}
| op=prefixOp e=constr_expression %prec unary_over_binary
{
grammar_logger "constr_expression_prefixOp" ;
{expr=PrefixOp (op, e);
emeta={loc=Location_span.of_positions_exn $loc}}
}
| e=constr_expression op=postfixOp
{
grammar_logger "constr_expression_postfix" ;
{expr=PostfixOp (e, op);
emeta={loc=Location_span.of_positions_exn $loc}}
}
| e=constr_expression LBRACK i=indexes RBRACK
{
grammar_logger "constr_expression_indexed" ;
{expr=Indexed (e, i);
emeta={loc=Location_span.of_positions_exn $loc}}
}
| e=common_expression
{
grammar_logger "constr_expression_common_expr" ;
{expr=e;
emeta={loc= Location_span.of_positions_exn $loc}}
}
| id=identifier
{
grammar_logger "constr_expression_identifier" ;
{expr=Variable id;
emeta={loc=Location_span.of_positions_exn $loc}}
}
common_expression:
| i=INTNUMERAL
{ grammar_logger ("intnumeral " ^ i) ; IntNumeral i }
| r=REALNUMERAL
{ grammar_logger ("realnumeral " ^ r) ; RealNumeral r }
| LBRACE xs=separated_nonempty_list(COMMA, expression) RBRACE
{ grammar_logger "array_expression" ; ArrayExpr xs }
| LBRACK xs=separated_list(COMMA, expression) RBRACK
{ grammar_logger "row_vector_expression" ; RowVectorExpr xs }
| id=identifier LPAREN args=separated_list(COMMA, expression) RPAREN
{ grammar_logger "fun_app" ;
if
List.length args = 1
&& ( String.is_suffix ~suffix:"_lpdf" id.name
|| String.is_suffix ~suffix:"_lupdf" id.name
|| String.is_suffix ~suffix:"_lpmf" id.name
|| String.is_suffix ~suffix:"_lupmf" id.name )
then CondDistApp ((), id, args)
else FunApp ((), id, args) }
| TARGET LPAREN RPAREN
{ grammar_logger "target_read" ; GetTarget }
| GETLP LPAREN RPAREN
{ grammar_logger "get_lp" ; GetLP } (* deprecated *)
| id=identifier LPAREN e=expression BAR args=separated_list(COMMA, expression)
RPAREN
{ grammar_logger "conditional_dist_app" ; CondDistApp ((), id, e :: args) }
| LPAREN e=expression RPAREN
{ grammar_logger "extra_paren" ; Paren e }
%inline prefixOp:
| BANG
{ grammar_logger "prefix_bang" ; Operator.PNot }
| MINUS
{ grammar_logger "prefix_minus" ; Operator.PMinus }
| PLUS
{ grammar_logger "prefix_plus" ; Operator.PPlus }
%inline postfixOp:
| TRANSPOSE
{ grammar_logger "postfix_transpose" ; Operator.Transpose }
%inline infixOp:
| a=arithmeticBinOp
{ grammar_logger "infix_arithmetic" ; a }
| l=logicalBinOp
{ grammar_logger "infix_logical" ; l }
%inline arithmeticBinOp:
| PLUS
{ grammar_logger "infix_plus" ; Operator.Plus }
| MINUS
{ grammar_logger "infix_minus" ;Operator.Minus }
| TIMES
{ grammar_logger "infix_times" ; Operator.Times }
| DIVIDE
{ grammar_logger "infix_divide" ; Operator.Divide }
| IDIVIDE
{ grammar_logger "infix_intdivide" ; Operator.IntDivide }
| MODULO
{ grammar_logger "infix_modulo" ; Operator.Modulo }
| LDIVIDE
{ grammar_logger "infix_ldivide" ; Operator.LDivide }
| ELTTIMES
{ grammar_logger "infix_elttimes" ; Operator.EltTimes }
| ELTDIVIDE
{ grammar_logger "infix_eltdivide" ; Operator.EltDivide }
| HAT
{ grammar_logger "infix_hat" ; Operator.Pow }
| ELTPOW
{ grammar_logger "infix_eltpow" ; Operator.EltPow }
%inline logicalBinOp:
| OR
{ grammar_logger "infix_or" ; Operator.Or }
| AND
{ grammar_logger "infix_and" ; Operator.And }
| EQUALS
{ grammar_logger "infix_equals" ; Operator.Equals }
| NEQUALS
{ grammar_logger "infix_nequals" ; Operator.NEquals}
| LABRACK
{ grammar_logger "infix_less" ; Operator.Less }
| LEQ
{ grammar_logger "infix_leq" ; Operator.Leq }
| RABRACK
{ grammar_logger "infix_greater" ; Operator.Greater }
| GEQ
{ grammar_logger "infix_geq" ; Operator.Geq }
indexes:
| (* nothing *)
{ grammar_logger "index_nothing" ; [All] }
| COLON
{ grammar_logger "index_all" ; [All] }
| e=expression
{ grammar_logger "index_single" ; [Single e] }
| e=expression COLON
{ grammar_logger "index_upper" ; [Upfrom e] }
| COLON e=expression
{ grammar_logger "index_lower" ; [Downfrom e] }
| e1=expression COLON e2=expression
{ grammar_logger "index_twosided" ; [Between (e1, e2)] }
| i1=indexes COMMA i2=indexes
{ grammar_logger "indexes" ; i1 @ i2 }
printables:
| e=expression
{ grammar_logger "printable expression" ; [PExpr e] }
| s=string_literal
{ grammar_logger "printable string" ; [PString s] }
| p1=printables COMMA p2=printables
{ grammar_logger "printables" ; p1 @ p2 }
(* L-values *)
lhs:
| id=identifier
{ grammar_logger "lhs_identifier" ;
{expr=Variable id
;emeta = { loc=Location_span.of_positions_exn $loc}}
}
| l=lhs LBRACK indices=indexes RBRACK
{ grammar_logger "lhs_index" ;
{expr=Indexed (l, indices)
;emeta = { loc=Location_span.of_positions_exn $loc}}}
(* statements *)
statement:
| s=atomic_statement
{ grammar_logger "atomic_statement" ;
{stmt= s;
smeta= { loc=Location_span.of_positions_exn $loc} }
}
| s=nested_statement
{ grammar_logger "nested_statement" ;
{stmt= s;
smeta={loc = Location_span.of_positions_exn $loc} }
}
atomic_statement:
| l=lhs op=assignment_op e=expression SEMICOLON
{ grammar_logger "assignment_statement" ;
Assignment {assign_lhs=lvalue_of_expr l;
assign_op=op;
assign_rhs=e} }
| id=identifier LPAREN args=separated_list(COMMA, expression) RPAREN SEMICOLON
{ grammar_logger "funapp_statement" ; NRFunApp ((),id, args) }
| INCREMENTLOGPROB LPAREN e=expression RPAREN SEMICOLON
{ grammar_logger "incrementlogprob_statement" ; IncrementLogProb e } (* deprecated *)
| e=expression TILDE id=identifier LPAREN es=separated_list(COMMA, expression)
RPAREN ot=option(truncation) SEMICOLON
{ grammar_logger "tilde_statement" ;
let t = match ot with Some tt -> tt | None -> NoTruncate in
Tilde {arg= e; distribution= id; args= es; truncation= t }
}
| TARGET PLUSASSIGN e=expression SEMICOLON
{ grammar_logger "targetpe_statement" ; TargetPE e }
| BREAK SEMICOLON
{ grammar_logger "break_statement" ; Break }
| CONTINUE SEMICOLON
{ grammar_logger "continue_statement" ; Continue }
| PRINT LPAREN l=printables RPAREN SEMICOLON
{ grammar_logger "print_statement" ; Print l }
| REJECT LPAREN l=printables RPAREN SEMICOLON
{ grammar_logger "reject_statement" ; Reject l }
| RETURN e=expression SEMICOLON
{ grammar_logger "return_statement" ; Return e }
| RETURN SEMICOLON
{ grammar_logger "return_nothing_statement" ; ReturnVoid }
| SEMICOLON
{ grammar_logger "skip" ; Skip }
%inline assignment_op:
| ASSIGN
{ grammar_logger "assign_plain" ; Assign }
| ARROWASSIGN
{ grammar_logger "assign_arrow" ; ArrowAssign } (* deprecated *)
| PLUSASSIGN
{ grammar_logger "assign_plus" ; OperatorAssign Plus }
| MINUSASSIGN
{ grammar_logger "assign_minus" ; OperatorAssign Minus }
| TIMESASSIGN
{ grammar_logger "assign_times" ; OperatorAssign Times }
| DIVIDEASSIGN
{ grammar_logger "assign_divide" ; OperatorAssign Divide }
| ELTTIMESASSIGN
{ grammar_logger "assign_elttimes" ; OperatorAssign EltTimes }
| ELTDIVIDEASSIGN
{ grammar_logger "assign_eltdivide" ; OperatorAssign EltDivide }
string_literal:
| s=STRINGLITERAL
{ grammar_logger ("string_literal " ^ s) ; s }
truncation:
| TRUNCATE LBRACK e1=option(expression) COMMA e2=option(expression)
RBRACK
{ grammar_logger "truncation" ;
match (e1, e2) with
| Some tt1, Some tt2 -> TruncateBetween (tt1, tt2)
| Some tt1, None -> TruncateUpFrom tt1
| None, Some tt2 -> TruncateDownFrom tt2
| None, None -> NoTruncate }
nested_statement:
| IF LPAREN e=expression RPAREN s1=statement ELSE s2=statement
{ grammar_logger "ifelse_statement" ; IfThenElse (e, s1, Some s2) }
| IF LPAREN e=expression RPAREN s=statement %prec below_ELSE
{ grammar_logger "if_statement" ; IfThenElse (e, s, None) }
| WHILE LPAREN e=expression RPAREN s=statement
{ grammar_logger "while_statement" ; While (e, s) }
| FOR LPAREN id=identifier IN e1=expression COLON e2=expression RPAREN
s=statement
{
grammar_logger "for_statement" ;
For {loop_variable= id;
lower_bound= e1;
upper_bound= e2;
loop_body= s;}
}
| FOR LPAREN id=identifier IN e=expression RPAREN s=statement
{ grammar_logger "foreach_statement" ; ForEach (id, e, s) }
| LBRACE l=list(vardecl_or_statement) RBRACE
{ grammar_logger "block_statement" ; Block l } (* NOTE: I am choosing to allow mixing of statements and var_decls *)
(* statement or var decls *)
vardecl_or_statement:
| s=statement
{ grammar_logger "vardecl_or_statement_statement" ; s }
| v=var_decl
{ grammar_logger "vardecl_or_statement_vardecl" ; v }
top_vardecl_or_statement:
| s=statement
{ grammar_logger "top_vardecl_or_statement_statement" ; s }
| v=top_var_decl
{ grammar_logger "top_vardecl_or_statement_top_vardecl" ; v }