Benjamin Peterson | a846d0a | 2012-01-15 21:28:00 -0500 | [diff] [blame] | 1 | # Grammar for 2to3. This grammar supports Python 2.x and 3.x. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 2 | |
Łukasz Langa | b51f5de | 2018-03-13 00:44:49 -0700 | [diff] [blame] | 3 | # NOTE WELL: You should also follow all the steps listed at |
| 4 | # https://devguide.python.org/grammar/ |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 5 | |
| 6 | # Start symbols for the grammar: |
| 7 | # file_input is a module or sequence of commands read from an input file; |
| 8 | # single_input is a single interactive statement; |
| 9 | # eval_input is the input for the eval() and input() functions. |
| 10 | # NB: compound_stmt in single_input is followed by extra NEWLINE! |
| 11 | file_input: (NEWLINE | stmt)* ENDMARKER |
| 12 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 13 | eval_input: testlist NEWLINE* ENDMARKER |
| 14 | |
| 15 | decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 16 | decorators: decorator+ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 17 | decorated: decorators (classdef | funcdef | async_funcdef) |
Jelle Zijlstra | f64aae4 | 2018-03-18 09:54:33 -0700 | [diff] [blame] | 18 | async_funcdef: ASYNC funcdef |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 19 | funcdef: 'def' NAME parameters ['->' test] ':' suite |
| 20 | parameters: '(' [typedargslist] ')' |
Gregory P. Smith | 42c9f0f | 2020-12-14 09:10:10 -0800 | [diff] [blame] | 21 | |
| 22 | # The following definition for typedarglist is equivalent to this set of rules: |
| 23 | # |
| 24 | # arguments = argument (',' argument)* |
| 25 | # argument = tfpdef ['=' test] |
| 26 | # kwargs = '**' tname [','] |
| 27 | # args = '*' [tname] |
| 28 | # kwonly_kwargs = (',' argument)* [',' [kwargs]] |
| 29 | # args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 30 | # poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]] |
| 31 | # typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 32 | # typedarglist = arguments ',' '/' [',' [typedargslist_no_posonly]])|(typedargslist_no_posonly)" |
| 33 | # |
| 34 | # It needs to be fully expanded to allow our LL(1) parser to work on it. |
| 35 | |
| 36 | typedargslist: tfpdef ['=' test] (',' tfpdef ['=' test])* ',' '/' [ |
| 37 | ',' [((tfpdef ['=' test] ',')* ('*' [tname] (',' tname ['=' test])* |
| 38 | [',' ['**' tname [',']]] | '**' tname [',']) |
| 39 | | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])] |
| 40 | ] | ((tfpdef ['=' test] ',')* ('*' [tname] (',' tname ['=' test])* |
| 41 | [',' ['**' tname [',']]] | '**' tname [',']) |
| 42 | | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
| 43 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 44 | tname: NAME [':' test] |
| 45 | tfpdef: tname | '(' tfplist ')' |
| 46 | tfplist: tfpdef (',' tfpdef)* [','] |
Gregory P. Smith | 42c9f0f | 2020-12-14 09:10:10 -0800 | [diff] [blame] | 47 | |
| 48 | # The following definition for varargslist is equivalent to this set of rules: |
| 49 | # |
| 50 | # arguments = argument (',' argument )* |
| 51 | # argument = vfpdef ['=' test] |
| 52 | # kwargs = '**' vname [','] |
| 53 | # args = '*' [vname] |
| 54 | # kwonly_kwargs = (',' argument )* [',' [kwargs]] |
| 55 | # args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 56 | # poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]] |
| 57 | # vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 58 | # varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | (vararglist_no_posonly) |
| 59 | # |
| 60 | # It needs to be fully expanded to allow our LL(1) parser to work on it. |
| 61 | |
| 62 | varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ |
| 63 | ((vfpdef ['=' test] ',')* ('*' [vname] (',' vname ['=' test])* |
| 64 | [',' ['**' vname [',']]] | '**' vname [',']) |
| 65 | | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
| 66 | ]] | ((vfpdef ['=' test] ',')* |
| 67 | ('*' [vname] (',' vname ['=' test])* [',' ['**' vname [',']]]| '**' vname [',']) |
| 68 | | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
| 69 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 70 | vname: NAME |
| 71 | vfpdef: vname | '(' vfplist ')' |
| 72 | vfplist: vfpdef (',' vfpdef)* [','] |
| 73 | |
| 74 | stmt: simple_stmt | compound_stmt |
| 75 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 76 | small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | |
| 77 | import_stmt | global_stmt | exec_stmt | assert_stmt) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 78 | expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 79 | ('=' (yield_expr|testlist_star_expr))*) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 80 | annassign: ':' test ['=' test] |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 81 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
Benjamin Peterson | 4ab92c8 | 2014-04-10 00:12:47 -0400 | [diff] [blame] | 82 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 83 | '<<=' | '>>=' | '**=' | '//=') |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 84 | # For normal and annotated assignments, additional restrictions enforced by the interpreter |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 85 | print_stmt: 'print' ( [ test (',' test)* [','] ] | |
| 86 | '>>' test [ (',' test)+ [','] ] ) |
| 87 | del_stmt: 'del' exprlist |
| 88 | pass_stmt: 'pass' |
| 89 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt |
| 90 | break_stmt: 'break' |
| 91 | continue_stmt: 'continue' |
Vlad Emelianov | 768d739 | 2020-03-01 20:59:26 +0100 | [diff] [blame] | 92 | return_stmt: 'return' [testlist_star_expr] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 93 | yield_stmt: yield_expr |
| 94 | raise_stmt: 'raise' [test ['from' test | ',' test [',' test]]] |
| 95 | import_stmt: import_name | import_from |
| 96 | import_name: 'import' dotted_as_names |
| 97 | import_from: ('from' ('.'* dotted_name | '.'+) |
| 98 | 'import' ('*' | '(' import_as_names ')' | import_as_names)) |
| 99 | import_as_name: NAME ['as' NAME] |
| 100 | dotted_as_name: dotted_name ['as' NAME] |
| 101 | import_as_names: import_as_name (',' import_as_name)* [','] |
| 102 | dotted_as_names: dotted_as_name (',' dotted_as_name)* |
| 103 | dotted_name: NAME ('.' NAME)* |
| 104 | global_stmt: ('global' | 'nonlocal') NAME (',' NAME)* |
| 105 | exec_stmt: 'exec' expr ['in' test [',' test]] |
| 106 | assert_stmt: 'assert' test [',' test] |
| 107 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 108 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt |
Jelle Zijlstra | f64aae4 | 2018-03-18 09:54:33 -0700 | [diff] [blame] | 109 | async_stmt: ASYNC (funcdef | with_stmt | for_stmt) |
Tim Hatch | 3c3aa45 | 2020-04-02 15:34:54 -0700 | [diff] [blame] | 110 | if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite] |
| 111 | while_stmt: 'while' namedexpr_test ':' suite ['else' ':' suite] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 112 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] |
| 113 | try_stmt: ('try' ':' suite |
| 114 | ((except_clause ':' suite)+ |
| 115 | ['else' ':' suite] |
| 116 | ['finally' ':' suite] | |
| 117 | 'finally' ':' suite)) |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 118 | with_stmt: 'with' with_item (',' with_item)* ':' suite |
| 119 | with_item: test ['as' expr] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 120 | with_var: 'as' expr |
| 121 | # NB compile.c makes sure that the default except clause is last |
| 122 | except_clause: 'except' [test [(',' | 'as') test]] |
| 123 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT |
| 124 | |
| 125 | # Backward compatibility cruft to support: |
| 126 | # [ x for x in lambda: True, lambda: False if x() ] |
| 127 | # even while also allowing: |
| 128 | # lambda x: 5 if x else 2 |
| 129 | # (But not a mix of the two) |
| 130 | testlist_safe: old_test [(',' old_test)+ [',']] |
| 131 | old_test: or_test | old_lambdef |
| 132 | old_lambdef: 'lambda' [varargslist] ':' old_test |
| 133 | |
Tim Hatch | 3c3aa45 | 2020-04-02 15:34:54 -0700 | [diff] [blame] | 134 | namedexpr_test: test [':=' test] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 135 | test: or_test ['if' or_test 'else' test] | lambdef |
| 136 | or_test: and_test ('or' and_test)* |
| 137 | and_test: not_test ('and' not_test)* |
| 138 | not_test: 'not' not_test | comparison |
| 139 | comparison: expr (comp_op expr)* |
| 140 | comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 141 | star_expr: '*' expr |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 142 | expr: xor_expr ('|' xor_expr)* |
| 143 | xor_expr: and_expr ('^' and_expr)* |
| 144 | and_expr: shift_expr ('&' shift_expr)* |
| 145 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 146 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | 4ab92c8 | 2014-04-10 00:12:47 -0400 | [diff] [blame] | 147 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 148 | factor: ('+'|'-'|'~') factor | power |
Jelle Zijlstra | f64aae4 | 2018-03-18 09:54:33 -0700 | [diff] [blame] | 149 | power: [AWAIT] atom trailer* ['**' factor] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 150 | atom: ('(' [yield_expr|testlist_gexp] ')' | |
| 151 | '[' [listmaker] ']' | |
| 152 | '{' [dictsetmaker] '}' | |
| 153 | '`' testlist1 '`' | |
| 154 | NAME | NUMBER | STRING+ | '.' '.' '.') |
Tim Hatch | 3c3aa45 | 2020-04-02 15:34:54 -0700 | [diff] [blame] | 155 | listmaker: (namedexpr_test|star_expr) ( comp_for | (',' (namedexpr_test|star_expr))* [','] ) |
| 156 | testlist_gexp: (namedexpr_test|star_expr) ( comp_for | (',' (namedexpr_test|star_expr))* [','] ) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 157 | lambdef: 'lambda' [varargslist] ':' test |
| 158 | trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
| 159 | subscriptlist: subscript (',' subscript)* [','] |
| 160 | subscript: test | [test] ':' [test] [sliceop] |
| 161 | sliceop: ':' [test] |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 162 | exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 163 | testlist: test (',' test)* [','] |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | 2832574 | 2016-09-09 18:18:52 -0700 | [diff] [blame] | 164 | dictsetmaker: ( ((test ':' test | '**' expr) |
| 165 | (comp_for | (',' (test ':' test | '**' expr))* [','])) | |
| 166 | ((test | star_expr) |
| 167 | (comp_for | (',' (test | star_expr))* [','])) ) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 168 | |
| 169 | classdef: 'class' NAME ['(' [arglist] ')'] ':' suite |
| 170 | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | 2832574 | 2016-09-09 18:18:52 -0700 | [diff] [blame] | 171 | arglist: argument (',' argument)* [','] |
| 172 | |
| 173 | # "test '=' test" is really "keyword '=' test", but we have no such token. |
| 174 | # These need to be in a single rule to avoid grammar that is ambiguous |
| 175 | # to our LL(1) parser. Even though 'test' includes '*expr' in star_expr, |
| 176 | # we explicitly match '*' here, too, to give it proper precedence. |
| 177 | # Illegal combinations and orderings are blocked in ast.c: |
Martin Panter | d2a584b | 2016-10-10 00:24:34 +0000 | [diff] [blame] | 178 | # multiple (test comp_for) arguments are blocked; keyword unpackings |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | 2832574 | 2016-09-09 18:18:52 -0700 | [diff] [blame] | 179 | # that precede iterable unpackings are blocked; etc. |
| 180 | argument: ( test [comp_for] | |
Tim Hatch | 3c3aa45 | 2020-04-02 15:34:54 -0700 | [diff] [blame] | 181 | test ':=' test | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | 2832574 | 2016-09-09 18:18:52 -0700 | [diff] [blame] | 182 | test '=' test | |
Zsolt Dollenstein | 96b06ae | 2019-10-23 23:19:07 -0700 | [diff] [blame] | 183 | '**' test | |
| 184 | '*' test ) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 185 | |
| 186 | comp_iter: comp_for | comp_if |
Serhiy Storchaka | 4b8a7f5 | 2018-07-31 09:34:30 +0300 | [diff] [blame] | 187 | comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 188 | comp_if: 'if' old_test [comp_iter] |
| 189 | |
| 190 | testlist1: test (',' test)* |
| 191 | |
| 192 | # not used in grammar, but may appear in "node" passed from Parser to Compiler |
| 193 | encoding_decl: NAME |
| 194 | |
Benjamin Peterson | 0654be1 | 2014-04-10 00:23:18 -0400 | [diff] [blame] | 195 | yield_expr: 'yield' [yield_arg] |
Vlad Emelianov | 768d739 | 2020-03-01 20:59:26 +0100 | [diff] [blame] | 196 | yield_arg: 'from' test | testlist_star_expr |