-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtree-sitter-stan.ebnf
369 lines (276 loc) · 8.21 KB
/
tree-sitter-stan.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
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
//
// From tree-sitter-stan/src/grammar.json
//
//
// EBNF to generate railroad diagram at
// (IPV6) https://www.bottlecaps.de/rr/ui
// (IPV4) https://rr.red-dove.com/ui
//
program ::=
functions? data? transformed_data? parameters? transformed_parameters? model? generated_quantities?
functions ::=
'functions' '{' function_definition* '}'
data ::=
'data' _variable_decls
transformed_data ::=
'transformed' 'data' _variable_decl_statements
parameters ::=
'parameters' _variable_decls
transformed_parameters ::=
'transformed' 'parameters' _variable_decl_statements
model ::=
'model' _variable_decl_statements
generated_quantities ::=
'generated' 'quantities' _variable_decl_statements
_variable_decl_statements ::=
'{' variable_declaration* _statement* '}'
_variable_decls ::=
'{' variable_declaration* '}'
function_definition ::=
return_type function_declarator _statement
function_declarator ::=
identifier parameter_list
parameter_list ::=
'(' ( parameter_declaration ( ',' parameter_declaration )* )? ')'
parameter_declaration ::=
'data'? unsized_type identifier
return_type ::=
'void'
| unsized_type
unsized_type ::=
basic_type ( '[' ','* ']' )?
basic_type ::=
'int' | 'real' | 'vector' | 'row_vector' | 'matrix'
variable_declaration ::=
_var_type identifier dims? ( '=' _expression )? ';'
_var_type ::=
int_type
| real_type
| vector_type
| ordered_type
| positive_ordered_type
| simplex_type
| unit_vector_type
| row_vector_type
| matrix_type
| cholesky_factor_cov_type
| cholesky_factor_corr_type
| cov_matrix_type
| corr_matrix_type
int_type ::=
'int' range_constraint?
real_type ::=
'real' range_constraint?
vector_type ::=
'vector' range_constraint? '[' _expression ']'
ordered_type ::=
'ordered' '[' _expression ']'
positive_ordered_type ::=
'positive_ordered' '[' _expression ']'
simplex_type ::=
'simplex' '[' _expression ']'
unit_vector_type ::=
'unit_vector' '[' _expression ']'
row_vector_type ::=
'row_vector' range_constraint? '[' _expression ']'
matrix_type ::=
'matrix' range_constraint? '[' _expression ',' _expression ']'
cholesky_factor_corr_type ::=
'cholesky_factor_corr' '[' _expression ']'
cholesky_factor_cov_type ::=
'cholesky_factor_cov' '[' _expression ( ',' _expression )? ']'
corr_matrix_type ::=
'corr_matrix' '[' _expression ']'
cov_matrix_type ::=
'cov_matrix' '[' _expression ']'
range_constraint ::=
range_empty | range_lower | range_upper | range_lower_upper
range_empty ::=
'<' '>'
range_lower_upper ::=
'<' 'lower' '=' _range_expression ',' 'upper' '=' _range_expression '>'
range_lower ::=
'<' 'lower' '=' _range_expression '>'
range_upper ::=
'<' 'upper' '=' _range_expression '>'
dims ::=
'[' ( _expression ( ',' _expression )* )? ']'
identifier ::=
[A-Za-z][A-Za-z0-9_]*
_expression ::=
_range_expression
| conditional_expression
| infix_op_expression
| prefix_op_expression
| postfix_op_expression
| indexed_expression
_range_expression ::=
_common_expression
| infix_op_range_expression
| prefix_op_range_expression
| postfix_op_range_expression
| indexed_range_expression
_common_expression ::=
integer_literal
| real_literal
| identifier
| array_expression
| vector_expression
| function_expression
| distr_expression
| parenthized_expression
conditional_expression ::=
_expression '?' _expression ':' _expression
array_expression ::=
'{' ( _expression ( ',' _expression )* )? '}'
vector_expression ::=
'[' ( _expression ( ',' _expression )* )? ']'
infix_op_expression ::=
_expression '^' _expression
| _expression '.*' _expression
| _expression './' _expression
| _expression '\\' _expression
| _expression '*' _expression
| _expression '%' _expression
| _expression '/' _expression
| _expression '+' _expression
| _expression '-' _expression
| _expression '>=' _expression
| _expression '>' _expression
| _expression '<=' _expression
| _expression '<' _expression
| _expression '!=' _expression
| _expression '==' _expression
| _expression '&&' _expression
| _expression '||' _expression
prefix_op_expression ::=
'+' _expression
| '-' _expression
| '!' _expression
postfix_op_expression ::=
_expression "'"
indexed_expression ::=
_expression '[' index? ( ',' index? )* ']'
infix_op_range_expression ::=
_range_expression '^' _range_expression
| _range_expression '.*' _range_expression
| _range_expression './' _range_expression
| _range_expression '\\' _range_expression
| _range_expression '*' _range_expression
| _range_expression '%' _range_expression
| _range_expression '/' _range_expression
| _range_expression '+' _range_expression
| _range_expression '-' _range_expression
prefix_op_range_expression ::=
'+' _range_expression
| '-' _range_expression
| '!' _range_expression
postfix_op_range_expression ::=
_range_expression "'"
indexed_range_expression ::=
_range_expression '[' index? ( ',' index? )* ']'
function_expression ::=
identifier argument_list
argument_list ::=
'(' ( _expression ( ',' _expression )* )? ')'
distr_expression ::=
identifier distr_argument_list
distr_argument_list ::=
'(' _expression '|' ( _expression ( ',' _expression )* )? ')'
integrate_ode ::=
'integrate_ode' '(' identifier _expression _expression _expression _expression _expression _expression ')'
integrate_ode_r45 ::=
'integrate_ode_rk45' '(' identifier ( _expression _expression _expression _expression _expression _expression | _expression _expression _expression _expression _expression _expression _expression _expression _expression ) ')'
algebra_solver ::=
'algebra_solver' '(' identifier ( _expression _expression _expression _expression | _expression _expression _expression _expression _expression _expression _expression ) ')'
integrate_ode_bdf ::=
'integrate_ode_bdf' '(' identifier ( _expression _expression _expression _expression _expression _expression | _expression _expression _expression _expression _expression _expression _expression _expression _expression ) ')'
parenthized_expression ::=
'(' _expression ')'
index ::=
_expression
| colon_expression
| left_colon_expression
| right_colon_expression
colon_expression ::=
_expression ':' _expression
left_colon_expression ::=
':' _expression
right_colon_expression ::=
_expression ':'
integer_literal ::=
[0-9]+
real_literal ::=
( [0-9]*'.'[0-9]+([eE][+-]?[0-9]+)? | [0-9]+'.'([eE][+-]?[0-9]+)? | [0-9]+[eE][+-]?[0-9]+ )
_statement ::=
empty_statement
| assignment_statement
| sampling_statement
| function_expression
| log_prob_statement
| target_statement
| break_statement
| continue_statement
| print_statement
| reject_statement
| return_statement
| if_statement
| while_statement
| for_statement
| block_statement
empty_statement ::=
';'
assignment_statement ::=
lhs assignment_op _expression ';'
lhs ::=
identifier ( '[' index ( ',' index )* ']' )*
assignment_op ::=
'<-'
| '='
| '+='
| '-='
| '*='
| '/='
| '.*='
| './='
sampling_statement ::=
_expression '~' identifier '(' ( _expression ( ',' _expression )* )? ')' _truncation? ';'
_truncation ::=
lower_upper_truncation
| lower_truncation
| upper_truncation
| empty_truncation
lower_upper_truncation ::=
'T' '[' _expression ',' _expression ']'
lower_truncation ::=
'T' '[' _expression ',' ']'
upper_truncation ::=
'T' '[' ',' _expression ']'
empty_truncation ::=
'T' '[' ',' ']'
log_prob_statement ::=
'increment_log_prob' '(' _expression ')' ';'
target_statement ::=
'target' '+=' _expression ';'
break_statement ::=
'break' ';'
continue_statement ::=
'continue' ';'
print_statement ::=
'print' '(' ( ( _expression | string_literal ) ( ',' ( _expression | string_literal ) )* )? ')' ';'
string_literal ::=
'"'[A-Za-z0-9 ~@#x23$%^&*_'`+{}#x5B#x5D()<>|/!?.,;:=-]*"""
reject_statement ::=
'reject' '(' ( _expression | string_literal )? ')' ';'
return_statement ::=
'return' _expression ';'
if_statement ::=
'if' '(' _expression ')' _statement ( 'else' _statement )?
while_statement ::=
'while' '(' _expression ')' _statement
for_statement ::=
'for' '(' identifier 'in' _expression ( ':' _expression )? ')' _statement
block_statement ::=
'{' variable_declaration* _statement* '}'
comment ::=
( '//' '.'* | '#' '.'* | '/*' [^*]*'\'*+([^/*][^*]*'\'*+)* '/' )