-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonBNF.txt
More file actions
524 lines (378 loc) · 12.4 KB
/
Copy pathPythonBNF.txt
File metadata and controls
524 lines (378 loc) · 12.4 KB
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
keyword ::=
"and" | "del" | "from" | "not" | "while"
| "as" | "elif" | "global" | "or" | "with"
| "assert" | "else" | "if" | "pass" | "yield"
| "break" | "except" | "import" | "print"
| "class" | "exec" | "in" | "raise"
| "continue"| "finally" | "is" | "return"
| "def" | "for" | "lambda" | "try"
identifier ::=
(letter|"_") (letter | digit | "_")*
(where the matched string is not a keyword)
letter ::=
lowercase | uppercase
lowercase ::=
"a"|"b"|...|"z"
uppercase ::=
"A"|"B"|...|"Z"
digit ::=
"0"|"1"|...|"9"
stringliteralpiece ::=
[stringprefix] (shortstring | longstring)
| rawstringprefix (rawshortstring | rawlongstring)
stringprefix ::=
"u" | "U"
rawstringprefix ::=
"r" | "ur" | "R" | "UR" | "Ur" | "uR"
shortstring ::=
"'" shortstringitem1* "'"
| '"' shortstringitem2* '"'
rawshortstring ::=
"'" rawshortstringitem1* "'"
| '"' rawshortstringitem2* '"'
longstring ::=
"'''" <shortest sequence of longstringitems not containing unescaped "'''"> "'''"
| '"""' <shortest sequence of longstringitems not containing unescaped '"""'> '"""'
rawlongstring ::=
"'''" <shortest sequence of rawlongstringitems not containing unescaped "'''"> "'''"
| '"""' <shortest sequence of rawlongstringitems not containing unescaped '"""'> '"""'
shortstringitem1 ::=
<any source character except "'" or newline> | escapeseq
shortstringitem2 ::=
<any source character except '"' or newline> | escapeseq
longstringitem ::=
<any source character except '\'> | escapeseq
escapeseq ::=
'\' <any ASCII character> | '\' <1-3 octal digits>
rawshortstringitem1 ::=
<any source character except "'" or newline> | rawescapeseq
rawshortstringitem2 ::=
<any source character except '"' or newline> | rawescapeseq
rawlongstringitem ::=
<any source character except '\'> | rawescapeseq
rawescapeseq ::=
'\' <any ASCII character>
longinteger ::=
integer ("l" | "L")
integer ::=
decimalinteger | octinteger | hexinteger
decimalinteger ::=
nonzerodigit digit* | "0"
octinteger ::=
"0" octdigit+
hexinteger ::=
"0" ("x" | "X") hexdigit+
nonzerodigit ::=
"1"..."9"
octdigit ::=
"0"..."7"
hexdigit ::=
digit | "a"..."f" | "A"..."F"
floatnumber ::=
pointfloat | exponentfloat
pointfloat ::=
[intpart] fraction | intpart "."
exponentfloat ::=
(intpart | pointfloat)
exponent
intpart ::=
digit+
fraction ::=
"." digit+
exponent ::=
("e" | "E") ["+" | "-"] digit+
imagnumber ::= (floatnumber | intpart) ("j" | "J")
**Grammar**
atom ::=
identifier | literal | enclosure
enclosure ::=
parenth_form | list_display
| generator_expression | dict_display
| string_conversion | yield_atom
literal ::=
stringliteral | integer | longinteger
| floatnumber | imagnumber
stringliteral ::=
stringliteralpiece
| stringliteral stringliteralpiece
parenth_form ::=
"(" [expression_list] ")"
list_display ::=
"[" [expression_list | list_comprehension] "]"
list_comprehension ::=
expression list_for
list_for ::=
"for" target_list "in" old_expression_list
[list_iter]
old_expression_list ::=
old_expression
[("," old_expression)+ [","]]
list_iter ::=
list_for | list_if
list_if ::=
"if" old_expression [list_iter]
generator_expression ::=
"(" expression genexpr_for ")"
genexpr_for ::=
"for" target_list "in" or_test
[genexpr_iter]
genexpr_iter ::=
genexpr_for | genexpr_if
genexpr_if ::=
"if" old_expression [genexpr_iter]
dict_display ::=
"{" [key_datum_list] "}"
key_datum_list ::=
key_datum ("," key_datum)* [","]
key_datum ::=
expression ":" expression
string_conversion ::=
"`" expression_list "`"
yield_atom ::=
"(" yield_expression ")"
yield_expression ::=
"yield" [expression_list]
primary ::=
atom | attributeref
| subscription | slicing | call
attributeref ::=
primary "." identifier
subscription ::=
primary "[" expression_list "]"
slicing ::=
simple_slicing | extended_slicing
simple_slicing ::=
primary "[" short_slice "]"
extended_slicing ::=
primary "[" slice_list "]"
slice_list ::=
slice_item ("," slice_item)* [","]
slice_item ::=
expression | proper_slice | ellipsis
proper_slice ::=
short_slice | long_slice
short_slice ::=
[lower_bound] ":" [upper_bound]
long_slice ::=
short_slice ":" [stride]
lower_bound ::=
expression
upper_bound ::=
expression
stride ::=
expression
ellipsis ::=
"..."
call ::=
primary "(" [argument_list [","]
| expression genexpr_for] ")"
argument_list ::=
positional_arguments ["," keyword_arguments]
["," "*" expression]
["," "**" expression]
| keyword_arguments ["," "*" expression]
["," "**" expression]
| "*" expression ["," "**" expression]
| "**" expression
positional_arguments ::=
expression ("," expression)*
keyword_arguments ::=
keyword_item ("," keyword_item)*
keyword_item ::=
identifier "=" expression
power ::=
primary ["**" u_expr]
u_expr ::=
power | "-" u_expr
| "+" u_expr | "~" u_expr
m_expr ::=
u_expr | m_expr "*" u_expr
| m_expr "//" u_expr
| m_expr "/" u_expr
| m_expr "%" u_expr
a_expr ::=
m_expr | a_expr "+" m_expr
| a_expr "-" m_expr
shift_expr ::=
a_expr
| shift_expr ( "<<" | ">>" ) a_expr
and_expr ::=
shift_expr | and_expr "&" shift_expr
xor_expr ::=
and_expr | xor_expr "^" and_expr
or_expr ::=
xor_expr | or_expr "|" xor_expr
comparison ::=
or_expr ( comp_operator or_expr )*
comp_operator ::=
"<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
| "is" ["not"] | ["not"] "in"
expression ::=
conditional_expression | lambda_form
old_expression ::=
or_test | old_lambda_form
conditional_expression ::=
or_test ["if" or_test "else" expression]
or_test ::=
and_test | or_test "or" and_test
and_test ::=
not_test | and_test "and" not_test
not_test ::=
comparison | "not" not_test
lambda_form ::=
"lambda" [parameter_list] ":" expression
old_lambda_form ::=
"lambda" [parameter_list] ":" old_expression
expression_list ::=
expression ( "," expression )* [","]
simple_stmt ::= expression_stmt
| assert_stmt
| assignment_stmt
| augmented_assignment_stmt
| pass_stmt
| del_stmt
| print_stmt
| return_stmt
| yield_stmt
| raise_stmt
| break_stmt
| continue_stmt
| import_stmt
| global_stmt
| exec_stmt
expression_stmt ::=
expression_list
assert_stmt ::=
"assert" expression ["," expression]
assignment_stmt ::=
(target_list "=")+
(expression_list | yield_expression)
target_list ::=
target ("," target)* [","]
target ::=
identifier
| "(" target_list ")"
| "[" target_list "]"
| attributeref
| subscription
| slicing
augmented_assignment_stmt ::=
target augop
(expression_list | yield_expression)
augop ::=
"+=" | "-=" | "*=" | "/=" | "//=" | "%=" | "**="
| ">>=" | "<<=" | "&=" | "^=" | "|="
pass_stmt ::=
"pass"
del_stmt ::=
"del" target_list
print_stmt ::=
"print" ( [expression ("," expression)* [","]]
| ">>" expression [("," expression)+ [","]] )
return_stmt ::=
"return" [expression_list]
yield_stmt ::=
yield_expression
raise_stmt ::=
"raise" [expression ["," expression
["," expression]]]
break_stmt ::=
"break"
continue_stmt ::=
"continue"
import_stmt ::=
"import" module ["as" name]
( "," module ["as" name] )*
| "from" relative_module "import" identifier
["as" name]
( "," identifier ["as" name] )*
| "from" relative_module "import" "("
identifier ["as" name]
( "," identifier ["as" name] )* [","] ")"
| "from" module "import" "*"
module ::=
(identifier ".")* identifier
relative_module ::=
"."* module | "."+
name ::=
identifier
global_stmt ::=
"global" identifier ("," identifier)*
exec_stmt ::=
"exec" or_expr
["in" expression ["," expression]]
compound_stmt ::=
if_stmt
| while_stmt
| for_stmt
| try_stmt
| with_stmt
| funcdef
| classdef
suite ::=
stmt_list NEWLINE
| NEWLINE INDENT statement+ DEDENT
statement ::=
stmt_list NEWLINE | compound_stmt
stmt_list ::=
simple_stmt (";" simple_stmt)* [";"]
if_stmt ::=
"if" expression ":" suite
( "elif" expression ":" suite )*
["else" ":" suite]
while_stmt ::=
"while" expression ":" suite
["else" ":" suite]
for_stmt ::=
"for" target_list "in" expression_list
":" suite
["else" ":" suite]
try_stmt ::= try1_stmt | try2_stmt
try1_stmt ::=
"try" ":" suite
("except" [expression
["," target]] ":" suite)+
["else" ":" suite]
["finally" ":" suite]
try2_stmt ::=
"try" ":" suite
"finally" ":" suite
with_stmt ::=
"with" expression ["as" target] ":" suite
funcdef ::=
[decorators] "def" funcname "(" [parameter_list] ")"
":" suite
decorators ::=
decorator+
decorator ::=
"@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE
dotted_name ::=
identifier ("." identifier)*
parameter_list ::=
(defparameter ",")*
("*" identifier [, "**" identifier]
| "**" identifier
| defparameter [","] )
defparameter ::=
parameter ["=" expression]
sublist ::=
parameter ("," parameter)* [","]
parameter ::=
identifier | "(" sublist ")"
funcname ::=
identifier
classdef ::=
"class" classname [inheritance] ":"
suite
inheritance ::=
"(" [expression_list] ")"
classname ::=
identifier
file_input ::=
(NEWLINE | statement)*
interactive_input ::=
[stmt_list] NEWLINE | compound_stmt NEWLINE
eval_input ::=
expression_list NEWLINE*
input_input ::=
expression_list NEWLINE