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 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10 | # NOTE WELL: You should also follow all the steps listed in PEP 306, |
| 11 | # "How to Change Python's Grammar" |
| 12 | |
Guido van Rossum | e5f6f45 | 1994-09-29 10:05:45 +0000 | [diff] [blame] | 13 | # Start symbols for the grammar: |
| 14 | # single_input is a single interactive statement; |
| 15 | # file_input is a module or sequence of commands read from an input file; |
| 16 | # eval_input is the input for the eval() and input() functions. |
| 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+ |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 24 | decorated: decorators (classdef | funcdef) |
| 25 | funcdef: 'def' NAME parameters ['->' test] ':' suite |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 26 | parameters: '(' [typedargslist] ')' |
| 27 | typedargslist: ((tfpdef ['=' test] ',')* |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 28 | ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 29 | | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 30 | tfpdef: NAME [':' test] |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 31 | varargslist: ((vfpdef ['=' test] ',')* |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 32 | ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 33 | | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 34 | vfpdef: NAME |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | |
| 36 | stmt: simple_stmt | compound_stmt |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 37 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
Guido van Rossum | 452bf51 | 2007-02-09 05:32:43 +0000 | [diff] [blame] | 38 | small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 39 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt) |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 40 | expr_stmt: testlist (augassign (yield_expr|testlist) | |
| 41 | ('=' (yield_expr|testlist))*) |
| 42 | augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 43 | '<<=' | '>>=' | '**=' | '//=') |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 44 | # For normal assignments, additional restrictions enforced by the interpreter |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 45 | del_stmt: 'del' exprlist |
| 46 | pass_stmt: 'pass' |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 47 | 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] | 48 | break_stmt: 'break' |
| 49 | continue_stmt: 'continue' |
| 50 | return_stmt: 'return' [testlist] |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 51 | yield_stmt: yield_expr |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 52 | raise_stmt: 'raise' [test ['from' test]] |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 53 | import_stmt: import_name | import_from |
| 54 | import_name: 'import' dotted_as_names |
Georg Brandl | e66c8c7 | 2007-03-19 18:56:50 +0000 | [diff] [blame] | 55 | # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS |
| 56 | import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+) |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 57 | 'import' ('*' | '(' import_as_names ')' | import_as_names)) |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 58 | import_as_name: NAME ['as' NAME] |
| 59 | dotted_as_name: dotted_name ['as' NAME] |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 60 | import_as_names: import_as_name (',' import_as_name)* [','] |
| 61 | dotted_as_names: dotted_as_name (',' dotted_as_name)* |
Guido van Rossum | 4a1da26 | 1995-01-07 10:25:36 +0000 | [diff] [blame] | 62 | dotted_name: NAME ('.' NAME)* |
Guido van Rossum | 68fc349 | 1991-12-10 13:51:08 +0000 | [diff] [blame] | 63 | global_stmt: 'global' NAME (',' NAME)* |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 64 | nonlocal_stmt: 'nonlocal' NAME (',' NAME)* |
Guido van Rossum | 03a7466 | 1997-04-16 00:34:46 +0000 | [diff] [blame] | 65 | assert_stmt: 'assert' test [',' test] |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 67 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated |
Guido van Rossum | e5f6f45 | 1994-09-29 10:05:45 +0000 | [diff] [blame] | 68 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 69 | while_stmt: 'while' test ':' suite ['else' ':' suite] |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 70 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 71 | try_stmt: ('try' ':' suite |
| 72 | ((except_clause ':' suite)+ |
| 73 | ['else' ':' suite] |
| 74 | ['finally' ':' suite] | |
| 75 | 'finally' ':' suite)) |
Georg Brandl | 0c31562 | 2009-05-25 21:10:36 +0000 | [diff] [blame] | 76 | with_stmt: 'with' with_item (',' with_item)* ':' suite |
| 77 | with_item: test ['as' expr] |
Guido van Rossum | af82141 | 1992-03-31 18:49:18 +0000 | [diff] [blame] | 78 | # 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] | 79 | except_clause: 'except' [test ['as' NAME]] |
Guido van Rossum | 7ac4a88 | 1991-07-27 21:29:47 +0000 | [diff] [blame] | 80 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 81 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 82 | test: or_test ['if' or_test 'else' test] | lambdef |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 83 | test_nocond: or_test | lambdef_nocond |
| 84 | lambdef: 'lambda' [varargslist] ':' test |
| 85 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 86 | or_test: and_test ('or' and_test)* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | and_test: not_test ('and' not_test)* |
| 88 | not_test: 'not' not_test | comparison |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 89 | comparison: star_expr (comp_op star_expr)* |
Brett Cannon | e3944a5 | 2009-04-01 05:08:41 +0000 | [diff] [blame] | 90 | comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 91 | star_expr: ['*'] expr |
Guido van Rossum | 9eb4f53 | 1991-10-24 14:54:25 +0000 | [diff] [blame] | 92 | expr: xor_expr ('|' xor_expr)* |
| 93 | xor_expr: and_expr ('^' and_expr)* |
| 94 | and_expr: shift_expr ('&' shift_expr)* |
| 95 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 96 | arith_expr: term (('+'|'-') term)* |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 97 | term: factor (('*'|'/'|'%'|'//') factor)* |
Guido van Rossum | 0bfd6c3 | 1996-01-12 01:00:58 +0000 | [diff] [blame] | 98 | factor: ('+'|'-'|'~') factor | power |
Tim Peters | 84ee323 | 2002-05-23 20:05:40 +0000 | [diff] [blame] | 99 | power: atom trailer* ['**' factor] |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 100 | atom: ('(' [yield_expr|testlist_comp] ')' | |
| 101 | '[' [testlist_comp] ']' | |
| 102 | '{' [dictorsetmaker] '}' | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 103 | NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False') |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 104 | testlist_comp: test ( comp_for | (',' test)* [','] ) |
Guido van Rossum | 14f4451 | 1996-07-30 16:43:44 +0000 | [diff] [blame] | 105 | trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
| 106 | subscriptlist: subscript (',' subscript)* [','] |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 107 | subscript: test | [test] ':' [test] [sliceop] |
Guido van Rossum | 14f4451 | 1996-07-30 16:43:44 +0000 | [diff] [blame] | 108 | sliceop: ':' [test] |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 109 | exprlist: star_expr (',' star_expr)* [','] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | testlist: test (',' test)* [','] |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 111 | dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 112 | (test (comp_for | (',' test)* [','])) ) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 114 | classdef: 'class' NAME ['(' [arglist] ')'] ':' suite |
Guido van Rossum | a996b91 | 1995-07-07 22:26:23 +0000 | [diff] [blame] | 115 | |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 116 | arglist: (argument ',')* (argument [','] |
| 117 | |'*' test (',' argument)* [',' '**' test] |
| 118 | |'**' test) |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame^] | 119 | # The reason that keywords are test nodes instead of NAME is that using NAME |
| 120 | # results in an ambiguity. ast.c makes sure it's a NAME. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 121 | argument: test [comp_for] | test '=' test # Really [keyword '='] test |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 122 | comp_iter: comp_for | comp_if |
| 123 | comp_for: 'for' exprlist 'in' or_test [comp_iter] |
| 124 | comp_if: 'if' test_nocond [comp_iter] |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 126 | testlist1: test (',' test)* |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 127 | |
| 128 | # not used in grammar, but may appear in "node" passed from Parser to Compiler |
| 129 | encoding_decl: NAME |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 130 | |
| 131 | yield_expr: 'yield' [testlist] |