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 | |
| 3 | # Note: Changing the grammar specified in this file will most likely |
| 4 | # require corresponding changes in the parser module |
| 5 | # (../Modules/parsermodule.c). If you can't make the changes to |
| 6 | # that module yourself, please co-ordinate the required changes |
| 7 | # with someone who can; ask around on python-dev for help. Fred |
| 8 | # Drake <fdrake@acm.org> will probably be listening there. |
| 9 | |
| 10 | # NOTE WELL: You should also follow all the steps listed in PEP 306, |
| 11 | # "How to Change Python's Grammar" |
| 12 | |
| 13 | # Commands for Kees Blom's railroad program |
| 14 | #diagram:token NAME |
| 15 | #diagram:token NUMBER |
| 16 | #diagram:token STRING |
| 17 | #diagram:token NEWLINE |
| 18 | #diagram:token ENDMARKER |
| 19 | #diagram:token INDENT |
| 20 | #diagram:output\input python.bla |
| 21 | #diagram:token DEDENT |
| 22 | #diagram:output\textwidth 20.04cm\oddsidemargin 0.0cm\evensidemargin 0.0cm |
| 23 | #diagram:rules |
| 24 | |
| 25 | # Start symbols for the grammar: |
| 26 | # file_input is a module or sequence of commands read from an input file; |
| 27 | # single_input is a single interactive statement; |
| 28 | # eval_input is the input for the eval() and input() functions. |
| 29 | # NB: compound_stmt in single_input is followed by extra NEWLINE! |
| 30 | file_input: (NEWLINE | stmt)* ENDMARKER |
| 31 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 32 | eval_input: testlist NEWLINE* ENDMARKER |
| 33 | |
| 34 | decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 35 | decorators: decorator+ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 36 | decorated: decorators (classdef | funcdef | async_funcdef) |
| 37 | async_funcdef: ASYNC funcdef |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 38 | funcdef: 'def' NAME parameters ['->' test] ':' suite |
| 39 | parameters: '(' [typedargslist] ')' |
| 40 | typedargslist: ((tfpdef ['=' test] ',')* |
| 41 | ('*' [tname] (',' tname ['=' test])* [',' '**' tname] | '**' tname) |
| 42 | | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
| 43 | tname: NAME [':' test] |
| 44 | tfpdef: tname | '(' tfplist ')' |
| 45 | tfplist: tfpdef (',' tfpdef)* [','] |
| 46 | varargslist: ((vfpdef ['=' test] ',')* |
| 47 | ('*' [vname] (',' vname ['=' test])* [',' '**' vname] | '**' vname) |
| 48 | | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
| 49 | vname: NAME |
| 50 | vfpdef: vname | '(' vfplist ')' |
| 51 | vfplist: vfpdef (',' vfpdef)* [','] |
| 52 | |
| 53 | stmt: simple_stmt | compound_stmt |
| 54 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 55 | small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | |
| 56 | import_stmt | global_stmt | exec_stmt | assert_stmt) |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 57 | expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) | |
| 58 | ('=' (yield_expr|testlist_star_expr))*) |
| 59 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
Benjamin Peterson | 4ab92c8 | 2014-04-10 00:12:47 -0400 | [diff] [blame] | 60 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 61 | '<<=' | '>>=' | '**=' | '//=') |
| 62 | # For normal assignments, additional restrictions enforced by the interpreter |
| 63 | print_stmt: 'print' ( [ test (',' test)* [','] ] | |
| 64 | '>>' test [ (',' test)+ [','] ] ) |
| 65 | del_stmt: 'del' exprlist |
| 66 | pass_stmt: 'pass' |
| 67 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt |
| 68 | break_stmt: 'break' |
| 69 | continue_stmt: 'continue' |
| 70 | return_stmt: 'return' [testlist] |
| 71 | yield_stmt: yield_expr |
| 72 | raise_stmt: 'raise' [test ['from' test | ',' test [',' test]]] |
| 73 | import_stmt: import_name | import_from |
| 74 | import_name: 'import' dotted_as_names |
| 75 | import_from: ('from' ('.'* dotted_name | '.'+) |
| 76 | 'import' ('*' | '(' import_as_names ')' | import_as_names)) |
| 77 | import_as_name: NAME ['as' NAME] |
| 78 | dotted_as_name: dotted_name ['as' NAME] |
| 79 | import_as_names: import_as_name (',' import_as_name)* [','] |
| 80 | dotted_as_names: dotted_as_name (',' dotted_as_name)* |
| 81 | dotted_name: NAME ('.' NAME)* |
| 82 | global_stmt: ('global' | 'nonlocal') NAME (',' NAME)* |
| 83 | exec_stmt: 'exec' expr ['in' test [',' test]] |
| 84 | assert_stmt: 'assert' test [',' test] |
| 85 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 86 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt |
| 87 | async_stmt: ASYNC (funcdef | with_stmt | for_stmt) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 88 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 89 | while_stmt: 'while' test ':' suite ['else' ':' suite] |
| 90 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] |
| 91 | try_stmt: ('try' ':' suite |
| 92 | ((except_clause ':' suite)+ |
| 93 | ['else' ':' suite] |
| 94 | ['finally' ':' suite] | |
| 95 | 'finally' ':' suite)) |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 96 | with_stmt: 'with' with_item (',' with_item)* ':' suite |
| 97 | with_item: test ['as' expr] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 98 | with_var: 'as' expr |
| 99 | # NB compile.c makes sure that the default except clause is last |
| 100 | except_clause: 'except' [test [(',' | 'as') test]] |
| 101 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT |
| 102 | |
| 103 | # Backward compatibility cruft to support: |
| 104 | # [ x for x in lambda: True, lambda: False if x() ] |
| 105 | # even while also allowing: |
| 106 | # lambda x: 5 if x else 2 |
| 107 | # (But not a mix of the two) |
| 108 | testlist_safe: old_test [(',' old_test)+ [',']] |
| 109 | old_test: or_test | old_lambdef |
| 110 | old_lambdef: 'lambda' [varargslist] ':' old_test |
| 111 | |
| 112 | test: or_test ['if' or_test 'else' test] | lambdef |
| 113 | or_test: and_test ('or' and_test)* |
| 114 | and_test: not_test ('and' not_test)* |
| 115 | not_test: 'not' not_test | comparison |
| 116 | comparison: expr (comp_op expr)* |
| 117 | comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 118 | star_expr: '*' expr |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 119 | expr: xor_expr ('|' xor_expr)* |
| 120 | xor_expr: and_expr ('^' and_expr)* |
| 121 | and_expr: shift_expr ('&' shift_expr)* |
| 122 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 123 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | 4ab92c8 | 2014-04-10 00:12:47 -0400 | [diff] [blame] | 124 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 125 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 126 | power: [AWAIT] atom trailer* ['**' factor] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 127 | atom: ('(' [yield_expr|testlist_gexp] ')' | |
| 128 | '[' [listmaker] ']' | |
| 129 | '{' [dictsetmaker] '}' | |
| 130 | '`' testlist1 '`' | |
| 131 | NAME | NUMBER | STRING+ | '.' '.' '.') |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 132 | listmaker: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) |
Benjamin Peterson | f37eb3a | 2010-10-14 23:00:04 +0000 | [diff] [blame] | 133 | testlist_gexp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 134 | lambdef: 'lambda' [varargslist] ':' test |
| 135 | trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
| 136 | subscriptlist: subscript (',' subscript)* [','] |
| 137 | subscript: test | [test] ':' [test] [sliceop] |
| 138 | sliceop: ':' [test] |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 139 | exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 140 | testlist: test (',' test)* [','] |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | 2832574 | 2016-09-09 18:18:52 -0700 | [diff] [blame^] | 141 | dictsetmaker: ( ((test ':' test | '**' expr) |
| 142 | (comp_for | (',' (test ':' test | '**' expr))* [','])) | |
| 143 | ((test | star_expr) |
| 144 | (comp_for | (',' (test | star_expr))* [','])) ) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 145 | |
| 146 | classdef: 'class' NAME ['(' [arglist] ')'] ':' suite |
| 147 | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | 2832574 | 2016-09-09 18:18:52 -0700 | [diff] [blame^] | 148 | arglist: argument (',' argument)* [','] |
| 149 | |
| 150 | # "test '=' test" is really "keyword '=' test", but we have no such token. |
| 151 | # These need to be in a single rule to avoid grammar that is ambiguous |
| 152 | # to our LL(1) parser. Even though 'test' includes '*expr' in star_expr, |
| 153 | # we explicitly match '*' here, too, to give it proper precedence. |
| 154 | # Illegal combinations and orderings are blocked in ast.c: |
| 155 | # multiple (test comp_for) arguements are blocked; keyword unpackings |
| 156 | # that precede iterable unpackings are blocked; etc. |
| 157 | argument: ( test [comp_for] | |
| 158 | test '=' test | |
| 159 | '**' expr | |
| 160 | star_expr ) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 161 | |
| 162 | comp_iter: comp_for | comp_if |
| 163 | comp_for: 'for' exprlist 'in' testlist_safe [comp_iter] |
| 164 | comp_if: 'if' old_test [comp_iter] |
| 165 | |
| 166 | testlist1: test (',' test)* |
| 167 | |
| 168 | # not used in grammar, but may appear in "node" passed from Parser to Compiler |
| 169 | encoding_decl: NAME |
| 170 | |
Benjamin Peterson | 0654be1 | 2014-04-10 00:23:18 -0400 | [diff] [blame] | 171 | yield_expr: 'yield' [yield_arg] |
| 172 | yield_arg: 'from' test | testlist |