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+ |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 24 | funcdef: [decorators] 'def' NAME parameters ':' suite |
Guido van Rossum | 526e909 | 1992-01-14 18:27:17 +0000 | [diff] [blame] | 25 | parameters: '(' [varargslist] ')' |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 26 | varargslist: ((fpdef ['=' test] ',')* |
| 27 | ('*' NAME [',' '**' NAME] | '**' NAME) | |
| 28 | fpdef ['=' test] (',' fpdef ['=' test])* [',']) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 29 | fpdef: NAME | '(' fplist ')' |
Guido van Rossum | 526e909 | 1992-01-14 18:27:17 +0000 | [diff] [blame] | 30 | fplist: fpdef (',' fpdef)* [','] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | |
| 32 | stmt: simple_stmt | compound_stmt |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 33 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 34 | small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 35 | import_stmt | global_stmt | assert_stmt) |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 36 | expr_stmt: testlist (augassign (yield_expr|testlist) | |
| 37 | ('=' (yield_expr|testlist))*) |
| 38 | augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 39 | '<<=' | '>>=' | '**=' | '//=') |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 40 | # For normal assignments, additional restrictions enforced by the interpreter |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 41 | print_stmt: 'print' ( [ test (',' test)* [','] ] | |
| 42 | '>>' test [ (',' test)+ [','] ] ) |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 43 | del_stmt: 'del' exprlist |
| 44 | pass_stmt: 'pass' |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 45 | 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] | 46 | break_stmt: 'break' |
| 47 | continue_stmt: 'continue' |
| 48 | return_stmt: 'return' [testlist] |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 49 | yield_stmt: yield_expr |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 50 | raise_stmt: 'raise' [test [',' test [',' test]]] |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 51 | import_stmt: import_name | import_from |
| 52 | import_name: 'import' dotted_as_names |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 53 | import_from: ('from' ('.'* dotted_name | '.'+) |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 54 | 'import' ('*' | '(' import_as_names ')' | import_as_names)) |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 55 | import_as_name: NAME ['as' NAME] |
| 56 | dotted_as_name: dotted_name ['as' NAME] |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 57 | import_as_names: import_as_name (',' import_as_name)* [','] |
| 58 | dotted_as_names: dotted_as_name (',' dotted_as_name)* |
Guido van Rossum | 4a1da26 | 1995-01-07 10:25:36 +0000 | [diff] [blame] | 59 | dotted_name: NAME ('.' NAME)* |
Guido van Rossum | 68fc349 | 1991-12-10 13:51:08 +0000 | [diff] [blame] | 60 | global_stmt: 'global' NAME (',' NAME)* |
Guido van Rossum | 03a7466 | 1997-04-16 00:34:46 +0000 | [diff] [blame] | 61 | assert_stmt: 'assert' test [',' test] |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 63 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef |
Guido van Rossum | e5f6f45 | 1994-09-29 10:05:45 +0000 | [diff] [blame] | 64 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | while_stmt: 'while' test ':' suite ['else' ':' suite] |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 66 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 67 | try_stmt: ('try' ':' suite |
| 68 | ((except_clause ':' suite)+ |
| 69 | ['else' ':' suite] |
| 70 | ['finally' ':' suite] | |
| 71 | 'finally' ':' suite)) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 72 | with_stmt: 'with' test [ with_var ] ':' suite |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 73 | with_var: 'as' expr |
Guido van Rossum | af82141 | 1992-03-31 18:49:18 +0000 | [diff] [blame] | 74 | # NB compile.c makes sure that the default except clause is last |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 75 | except_clause: 'except' [test [',' test]] |
Guido van Rossum | 7ac4a88 | 1991-07-27 21:29:47 +0000 | [diff] [blame] | 76 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 78 | # Backward compatibility cruft to support: |
| 79 | # [ x for x in lambda: True, lambda: False if x() ] |
| 80 | # even while also allowing: |
| 81 | # lambda x: 5 if x else 2 |
| 82 | # (But not a mix of the two) |
| 83 | testlist_safe: old_test [(',' old_test)+ [',']] |
| 84 | old_test: or_test | old_lambdef |
| 85 | old_lambdef: 'lambda' [varargslist] ':' old_test |
| 86 | |
| 87 | test: or_test ['if' or_test 'else' test] | lambdef |
| 88 | or_test: and_test ('or' and_test)* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | and_test: not_test ('and' not_test)* |
| 90 | not_test: 'not' not_test | comparison |
| 91 | comparison: expr (comp_op expr)* |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 92 | comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
Guido van Rossum | 9eb4f53 | 1991-10-24 14:54:25 +0000 | [diff] [blame] | 93 | expr: xor_expr ('|' xor_expr)* |
| 94 | xor_expr: and_expr ('^' and_expr)* |
| 95 | and_expr: shift_expr ('&' shift_expr)* |
| 96 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 97 | arith_expr: term (('+'|'-') term)* |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 98 | term: factor (('*'|'/'|'%'|'//') factor)* |
Guido van Rossum | 0bfd6c3 | 1996-01-12 01:00:58 +0000 | [diff] [blame] | 99 | factor: ('+'|'-'|'~') factor | power |
Tim Peters | 84ee323 | 2002-05-23 20:05:40 +0000 | [diff] [blame] | 100 | power: atom trailer* ['**' factor] |
Neal Norwitz | 3c52c5a | 2005-12-18 04:12:30 +0000 | [diff] [blame] | 101 | atom: ('(' [yield_expr|testlist_gexp] ')' | |
| 102 | '[' [listmaker] ']' | |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 103 | '{' [dictsetmaker] '}' | |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame^] | 104 | NAME | NUMBER | STRING+ | '.' '.' '.') |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 105 | listmaker: test ( list_for | (',' test)* [','] ) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 106 | testlist_gexp: test ( gen_for | (',' test)* [','] ) |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 107 | lambdef: 'lambda' [varargslist] ':' test |
Guido van Rossum | 14f4451 | 1996-07-30 16:43:44 +0000 | [diff] [blame] | 108 | trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
| 109 | subscriptlist: subscript (',' subscript)* [','] |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame^] | 110 | subscript: test | [test] ':' [test] [sliceop] |
Guido van Rossum | 14f4451 | 1996-07-30 16:43:44 +0000 | [diff] [blame] | 111 | sliceop: ':' [test] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | exprlist: expr (',' expr)* [','] |
| 113 | testlist: test (',' test)* [','] |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 114 | dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | |
Brett Cannon | 409d8f2 | 2005-03-05 06:47:57 +0000 | [diff] [blame] | 116 | classdef: 'class' NAME ['(' [testlist] ')'] ':' suite |
Guido van Rossum | a996b91 | 1995-07-07 22:26:23 +0000 | [diff] [blame] | 117 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 118 | arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test) |
Neal Norwitz | d074beb | 2006-02-24 23:11:14 +0000 | [diff] [blame] | 119 | argument: test [gen_for] | test '=' test # Really [keyword '='] test |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 120 | |
| 121 | list_iter: list_for | list_if |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 122 | list_for: 'for' exprlist 'in' testlist_safe [list_iter] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 123 | list_if: 'if' old_test [list_iter] |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 124 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 125 | gen_iter: gen_for | gen_if |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 126 | gen_for: 'for' exprlist 'in' or_test [gen_iter] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 127 | gen_if: 'if' old_test [gen_iter] |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 129 | testlist1: test (',' test)* |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 130 | |
| 131 | # not used in grammar, but may appear in "node" passed from Parser to Compiler |
| 132 | encoding_decl: NAME |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 133 | |
| 134 | yield_expr: 'yield' [testlist] |