Guido van Rossum | 526e909 | 1992-01-14 18:27:17 +0000 | [diff] [blame] | 1 | # Grammar for Python |
Guido van Rossum | 6cf1273 | 1991-12-31 13:11:56 +0000 | [diff] [blame] | 2 | |
Fred Drake | abca14d | 2000-08-23 15:45:28 +0000 | [diff] [blame] | 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 | |
Berker Peksag | 192c750 | 2015-06-13 11:18:33 +0300 | [diff] [blame] | 10 | # NOTE WELL: You should also follow all the steps listed at |
| 11 | # https://docs.python.org/devguide/grammar.html |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | e5f6f45 | 1994-09-29 10:05:45 +0000 | [diff] [blame] | 13 | # Start symbols for the grammar: |
Benjamin Peterson | aefc1c7 | 2010-07-05 20:04:54 +0000 | [diff] [blame] | 14 | # single_input is a single interactive statement; |
| 15 | # file_input is a module or sequence of commands read from an input file; |
Benjamin Peterson | b3132bd | 2011-12-15 15:43:56 -0500 | [diff] [blame] | 16 | # eval_input is the input for the eval() functions. |
Guido van Rossum | e5f6f45 | 1994-09-29 10:05:45 +0000 | [diff] [blame] | 17 | # NB: compound_stmt in single_input is followed by extra NEWLINE! |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 18 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 19 | file_input: (NEWLINE | stmt)* ENDMARKER |
Guido van Rossum | e785fbc | 1992-03-04 16:41:24 +0000 | [diff] [blame] | 20 | eval_input: testlist NEWLINE* ENDMARKER |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 21 | |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 22 | decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 23 | decorators: decorator+ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 24 | decorated: decorators (classdef | funcdef | async_funcdef) |
| 25 | |
| 26 | async_funcdef: ASYNC funcdef |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 27 | funcdef: 'def' NAME parameters ['->' test] ':' suite |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 28 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 29 | parameters: '(' [typedargslist] ')' |
Benjamin Peterson | 605a774 | 2013-03-20 00:39:41 -0500 | [diff] [blame] | 30 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' |
Mark Dickinson | 1c50d11 | 2010-07-12 14:14:18 +0000 | [diff] [blame] | 31 | ['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef]] |
| 32 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 33 | tfpdef: NAME [':' test] |
Mark Dickinson | 1c50d11 | 2010-07-12 14:14:18 +0000 | [diff] [blame] | 34 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' |
| 35 | ['*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef]] |
| 36 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 37 | vfpdef: NAME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | |
| 39 | stmt: simple_stmt | compound_stmt |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 40 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
Guido van Rossum | 452bf51 | 2007-02-09 05:32:43 +0000 | [diff] [blame] | 41 | small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 42 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt) |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 43 | expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) | |
| 44 | ('=' (yield_expr|testlist_star_expr))*) |
| 45 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 46 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 47 | '<<=' | '>>=' | '**=' | '//=') |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 48 | # For normal assignments, additional restrictions enforced by the interpreter |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 49 | del_stmt: 'del' exprlist |
| 50 | pass_stmt: 'pass' |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 51 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 52 | break_stmt: 'break' |
| 53 | continue_stmt: 'continue' |
| 54 | return_stmt: 'return' [testlist] |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 55 | yield_stmt: yield_expr |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 56 | raise_stmt: 'raise' [test ['from' test]] |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 57 | import_stmt: import_name | import_from |
| 58 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 59 | # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS |
| 60 | import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+) |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 61 | 'import' ('*' | '(' import_as_names ')' | import_as_names)) |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 62 | import_as_name: NAME ['as' NAME] |
| 63 | dotted_as_name: dotted_name ['as' NAME] |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 64 | import_as_names: import_as_name (',' import_as_name)* [','] |
| 65 | dotted_as_names: dotted_as_name (',' dotted_as_name)* |
Guido van Rossum | 4a1da26 | 1995-01-07 10:25:36 +0000 | [diff] [blame] | 66 | dotted_name: NAME ('.' NAME)* |
Guido van Rossum | 68fc349 | 1991-12-10 13:51:08 +0000 | [diff] [blame] | 67 | global_stmt: 'global' NAME (',' NAME)* |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 68 | nonlocal_stmt: 'nonlocal' NAME (',' NAME)* |
Guido van Rossum | 03a7466 | 1997-04-16 00:34:46 +0000 | [diff] [blame] | 69 | assert_stmt: 'assert' test [',' test] |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 70 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 71 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt |
| 72 | async_stmt: ASYNC (funcdef | with_stmt | for_stmt) |
Guido van Rossum | e5f6f45 | 1994-09-29 10:05:45 +0000 | [diff] [blame] | 73 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | while_stmt: 'while' test ':' suite ['else' ':' suite] |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 75 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 76 | try_stmt: ('try' ':' suite |
| 77 | ((except_clause ':' suite)+ |
Benjamin Peterson | aefc1c7 | 2010-07-05 20:04:54 +0000 | [diff] [blame] | 78 | ['else' ':' suite] |
| 79 | ['finally' ':' suite] | |
| 80 | 'finally' ':' suite)) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 81 | with_stmt: 'with' with_item (',' with_item)* ':' suite |
| 82 | with_item: test ['as' expr] |
Guido van Rossum | af82141 | 1992-03-31 18:49:18 +0000 | [diff] [blame] | 83 | # NB compile.c makes sure that the default except clause is last |
Guido van Rossum | 16be03e | 2007-01-10 18:51:35 +0000 | [diff] [blame] | 84 | except_clause: 'except' [test ['as' NAME]] |
Guido van Rossum | 7ac4a88 | 1991-07-27 21:29:47 +0000 | [diff] [blame] | 85 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 87 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 88 | test_nocond: or_test | lambdef_nocond |
| 89 | lambdef: 'lambda' [varargslist] ':' test |
| 90 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 91 | or_test: and_test ('or' and_test)* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | and_test: not_test ('and' not_test)* |
| 93 | not_test: 'not' not_test | comparison |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 94 | comparison: expr (comp_op expr)* |
Eli Bendersky | 0e79b7e | 2011-11-14 01:16:31 +0200 | [diff] [blame] | 95 | # <> isn't actually a valid comparison operator in Python. It's here for the |
Martin v. Löwis | ec3af4e | 2014-08-05 17:56:52 +0200 | [diff] [blame] | 96 | # sake of a __future__ import described in PEP 401 (which really works :-) |
Brett Cannon | e3944a5 | 2009-04-01 05:08:41 +0000 | [diff] [blame] | 97 | comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 98 | star_expr: '*' expr |
Guido van Rossum | 9eb4f53 | 1991-10-24 14:54:25 +0000 | [diff] [blame] | 99 | expr: xor_expr ('|' xor_expr)* |
| 100 | xor_expr: and_expr ('^' and_expr)* |
| 101 | and_expr: shift_expr ('&' shift_expr)* |
| 102 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 103 | arith_expr: term (('+'|'-') term)* |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 104 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
Guido van Rossum | 0bfd6c3 | 1996-01-12 01:00:58 +0000 | [diff] [blame] | 105 | factor: ('+'|'-'|'~') factor | power |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 106 | power: atom_expr ['**' factor] |
| 107 | atom_expr: [AWAIT] atom trailer* |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 108 | atom: ('(' [yield_expr|testlist_comp] ')' | |
| 109 | '[' [testlist_comp] ']' | |
| 110 | '{' [dictorsetmaker] '}' | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 111 | NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False') |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 112 | testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) |
Guido van Rossum | 14f4451 | 1996-07-30 16:43:44 +0000 | [diff] [blame] | 113 | trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
| 114 | subscriptlist: subscript (',' subscript)* [','] |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 115 | subscript: test | [test] ':' [test] [sliceop] |
Guido van Rossum | 14f4451 | 1996-07-30 16:43:44 +0000 | [diff] [blame] | 116 | sliceop: ':' [test] |
Benjamin Peterson | 4905e80 | 2009-09-27 02:43:28 +0000 | [diff] [blame] | 117 | exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | testlist: test (',' test)* [','] |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 119 | dictorsetmaker: ( ((test ':' test | '**' expr) |
| 120 | (comp_for | (',' (test ':' test | '**' expr))* [','])) | |
| 121 | ((test | star_expr) |
| 122 | (comp_for | (',' (test | star_expr))* [','])) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 124 | classdef: 'class' NAME ['(' [arglist] ')'] ':' suite |
Guido van Rossum | a996b91 | 1995-07-07 22:26:23 +0000 | [diff] [blame] | 125 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 126 | arglist: argument (',' argument)* [','] |
| 127 | |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 128 | # The reason that keywords are test nodes instead of NAME is that using NAME |
| 129 | # results in an ambiguity. ast.c makes sure it's a NAME. |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 130 | # "test '=' test" is really "keyword '=' test", but we have no such token. |
| 131 | # These need to be in a single rule to avoid grammar that is ambiguous |
| 132 | # to our LL(1) parser. Even though 'test' includes '*expr' in star_expr, |
| 133 | # we explicitly match '*' here, too, to give it proper precedence. |
| 134 | # Illegal combinations and orderings are blocked in ast.c: |
| 135 | # multiple (test comp_for) arguements are blocked; keyword unpackings |
| 136 | # that precede iterable unpackings are blocked; etc. |
| 137 | argument: ( test [comp_for] | |
| 138 | test '=' test | |
Benjamin Peterson | de12b79 | 2015-05-16 09:44:45 -0400 | [diff] [blame] | 139 | '**' test | |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame^] | 140 | '*' test ) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 141 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 142 | comp_iter: comp_for | comp_if |
| 143 | comp_for: 'for' exprlist 'in' or_test [comp_iter] |
| 144 | comp_if: 'if' test_nocond [comp_iter] |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 145 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 146 | # not used in grammar, but may appear in "node" passed from Parser to Compiler |
| 147 | encoding_decl: NAME |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 148 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 149 | yield_expr: 'yield' [yield_arg] |
| 150 | yield_arg: 'from' test | testlist |