]>
Commit | Line | Data |
---|---|---|
1 | # Grammar for Python | |
2 | ||
3 | # NOTE WELL: You should also follow all the steps listed at | |
4 | # https://devguide.python.org/grammar/ | |
5 | ||
6 | # Start symbols for the grammar: | |
7 | # single_input is a single interactive statement; | |
8 | # file_input is a module or sequence of commands read from an input file; | |
9 | # eval_input is the input for the eval() functions. | |
10 | # NB: compound_stmt in single_input is followed by extra NEWLINE! | |
11 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE | |
12 | file_input: stmt* ENDMARKER | |
13 | eval_input: testlist NEWLINE* ENDMARKER | |
14 | ||
15 | decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE | |
16 | decorators: decorator+ | |
17 | decorated: decorators (classdef | funcdef | async_funcdef) | |
18 | ||
19 | async_funcdef: 'async' funcdef | |
20 | funcdef: 'def' NAME parameters ['->' test] ':' suite | |
21 | ||
22 | parameters: '(' [typedargslist] ')' | |
23 | typedargslist: ( | |
24 | (tfpdef ['=' test] (',' tfpdef ['=' test])* ',' '/' [',' [ tfpdef ['=' test] ( | |
25 | ',' tfpdef ['=' test])* ([',' [ | |
26 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] | |
27 | | '**' tfpdef [',']]]) | |
28 | | '*' [tfpdef] (',' tfpdef ['=' test])* ([',' ['**' tfpdef [',']]]) | |
29 | | '**' tfpdef [',']]] ) | |
30 | | (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ | |
31 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] | |
32 | | '**' tfpdef [',']]] | |
33 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] | |
34 | | '**' tfpdef [',']) | |
35 | ) | |
36 | tfpdef: NAME [':' test] | |
37 | varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ | |
38 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] | |
39 | | '**' vfpdef [',']]] | |
40 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] | |
41 | | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ | |
42 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] | |
43 | | '**' vfpdef [',']]] | |
44 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] | |
45 | | '**' vfpdef [','] | |
46 | ) | |
47 | vfpdef: NAME | |
48 | ||
49 | stmt: simple_stmt | compound_stmt | NEWLINE | |
50 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE | |
51 | small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt | | |
52 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt) | |
53 | expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | | |
54 | ('=' (yield_expr|testlist_star_expr))*) | |
55 | annassign: ':' test ['=' (yield_expr|testlist_star_expr)] | |
56 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] | |
57 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | | |
58 | '<<=' | '>>=' | '**=' | '//=') | |
59 | # For normal and annotated assignments, additional restrictions enforced by the interpreter | |
60 | del_stmt: 'del' exprlist | |
61 | pass_stmt: 'pass' | |
62 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt | |
63 | break_stmt: 'break' | |
64 | continue_stmt: 'continue' | |
65 | return_stmt: 'return' [testlist_star_expr] | |
66 | yield_stmt: yield_expr | |
67 | raise_stmt: 'raise' [test ['from' test]] | |
68 | import_stmt: import_name | import_from | |
69 | import_name: 'import' dotted_as_names | |
70 | # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS | |
71 | import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+) | |
72 | 'import' ('*' | '(' import_as_names ')' | import_as_names)) | |
73 | import_as_name: NAME ['as' NAME] | |
74 | dotted_as_name: dotted_name ['as' NAME] | |
75 | import_as_names: import_as_name (',' import_as_name)* [','] | |
76 | dotted_as_names: dotted_as_name (',' dotted_as_name)* | |
77 | dotted_name: NAME ('.' NAME)* | |
78 | global_stmt: 'global' NAME (',' NAME)* | |
79 | nonlocal_stmt: 'nonlocal' NAME (',' NAME)* | |
80 | assert_stmt: 'assert' test [',' test] | |
81 | ||
82 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt | |
83 | async_stmt: 'async' (funcdef | with_stmt | for_stmt) | |
84 | if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite] | |
85 | while_stmt: 'while' namedexpr_test ':' suite ['else' ':' suite] | |
86 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] | |
87 | try_stmt: ('try' ':' suite | |
88 | ((except_clause ':' suite)+ | |
89 | ['else' ':' suite] | |
90 | ['finally' ':' suite] | | |
91 | 'finally' ':' suite)) | |
92 | with_stmt: 'with' with_item (',' with_item)* ':' suite | |
93 | with_item: test ['as' expr] | |
94 | # NB compile.c makes sure that the default except clause is last | |
95 | except_clause: 'except' [test ['as' NAME]] | |
96 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT | |
97 | ||
98 | namedexpr_test: test [':=' test] | |
99 | test: or_test ['if' or_test 'else' test] | lambdef | |
100 | test_nocond: or_test | lambdef_nocond | |
101 | lambdef: 'lambda' [varargslist] ':' test | |
102 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond | |
103 | or_test: and_test ('or' and_test)* | |
104 | and_test: not_test ('and' not_test)* | |
105 | not_test: 'not' not_test | comparison | |
106 | comparison: expr (comp_op expr)* | |
107 | # <> isn't actually a valid comparison operator in Python. It's here for the | |
108 | # sake of a __future__ import described in PEP 401 (which really works :-) | |
109 | comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' | |
110 | star_expr: '*' expr | |
111 | expr: xor_expr ('|' xor_expr)* | |
112 | xor_expr: and_expr ('^' and_expr)* | |
113 | and_expr: shift_expr ('&' shift_expr)* | |
114 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* | |
115 | arith_expr: term (('+'|'-') term)* | |
116 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* | |
117 | factor: ('+'|'-'|'~') factor | power | |
118 | power: atom_expr ['**' factor] | |
119 | atom_expr: ['await'] atom trailer* | |
120 | atom: ('(' [yield_expr|testlist_comp] ')' | | |
121 | '[' [testlist_comp] ']' | | |
122 | '{' [dictorsetmaker] '}' | | |
123 | NAME | NUMBER | strings | '...' | 'None' | 'True' | 'False') | |
124 | testlist_comp: (namedexpr_test|star_expr) ( comp_for | (',' (namedexpr_test|star_expr))* [','] ) | |
125 | trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME | |
126 | subscriptlist: subscript (',' subscript)* [','] | |
127 | subscript: test | [test] ':' [test] [sliceop] | |
128 | sliceop: ':' [test] | |
129 | exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] | |
130 | testlist: test (',' test)* [','] | |
131 | dictorsetmaker: ( ((test ':' test | '**' expr) | |
132 | (comp_for | (',' (test ':' test | '**' expr))* [','])) | | |
133 | ((test | star_expr) | |
134 | (comp_for | (',' (test | star_expr))* [','])) ) | |
135 | ||
136 | classdef: 'class' NAME ['(' [arglist] ')'] ':' suite | |
137 | ||
138 | arglist: argument (',' argument)* [','] | |
139 | ||
140 | # The reason that keywords are test nodes instead of NAME is that using NAME | |
141 | # results in an ambiguity. ast.c makes sure it's a NAME. | |
142 | # "test '=' test" is really "keyword '=' test", but we have no such token. | |
143 | # These need to be in a single rule to avoid grammar that is ambiguous | |
144 | # to our LL(1) parser. Even though 'test' includes '*expr' in star_expr, | |
145 | # we explicitly match '*' here, too, to give it proper precedence. | |
146 | # Illegal combinations and orderings are blocked in ast.c: | |
147 | # multiple (test comp_for) arguments are blocked; keyword unpackings | |
148 | # that precede iterable unpackings are blocked; etc. | |
149 | argument: ( test [comp_for] | | |
150 | test ':=' test | | |
151 | test '=' test | | |
152 | '**' test | | |
153 | '*' test ) | |
154 | ||
155 | comp_iter: comp_for | comp_if | |
156 | sync_comp_for: 'for' exprlist 'in' or_test [comp_iter] | |
157 | comp_for: ['async'] sync_comp_for | |
158 | comp_if: 'if' test_nocond [comp_iter] | |
159 | ||
160 | # not used in grammar, but may appear in "node" passed from Parser to Compiler | |
161 | encoding_decl: NAME | |
162 | ||
163 | yield_expr: 'yield' [yield_arg] | |
164 | yield_arg: 'from' test | testlist_star_expr | |
165 | ||
166 | strings: (STRING | fstring)+ | |
167 | fstring: FSTRING_START fstring_content* FSTRING_END | |
168 | fstring_content: FSTRING_STRING | fstring_expr | |
169 | fstring_conversion: '!' NAME | |
170 | fstring_expr: '{' (testlist_comp | yield_expr) ['='] [ fstring_conversion ] [ fstring_format_spec ] '}' | |
171 | fstring_format_spec: ':' fstring_content* |