blob: 41d8be8a9ef9fd9a533133573edc13317113c258 [file] [log] [blame]
Guido van Rossum526e9091992-01-14 18:27:17 +00001# Grammar for Python
Guido van Rossum6cf12731991-12-31 13:11:56 +00002
Fred Drakeabca14d2000-08-23 15:45:28 +00003# 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 Wouters49fd7fa2006-04-21 10:40:58 +000010# NOTE WELL: You should also follow all the steps listed in PEP 306,
11# "How to Change Python's Grammar"
12
Guido van Rossume5f6f451994-09-29 10:05:45 +000013# 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 Rossum85a5fbb1990-10-14 12:07:46 +000018single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
19file_input: (NEWLINE | stmt)* ENDMARKER
Guido van Rossume785fbc1992-03-04 16:41:24 +000020eval_input: testlist NEWLINE* ENDMARKER
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021
Michael W. Hudson0ccff072004-08-17 17:29:16 +000022decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
23decorators: decorator+
Neal Norwitzc1505362006-12-28 06:47:50 +000024funcdef: [decorators] 'def' NAME parameters ['->' test] ':' suite
25parameters: '(' [typedargslist] ')'
26typedargslist: ((tfpdef ['=' test] ',')*
27 ('*' [tname] (',' tname ['=' test])* [',' '**' tname] | '**' tname)
28 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
29tname: NAME [':' test]
30tfpdef: tname | '(' tfplist ')'
31tfplist: tfpdef (',' tfpdef)* [',']
32varargslist: ((vfpdef ['=' test] ',')*
33 ('*' [vname] (',' vname ['=' test])* [',' '**' vname] | '**' vname)
34 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
35vname: NAME
36vfpdef: vname | '(' vfplist ')'
37vfplist: vfpdef (',' vfpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038
39stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000040simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossum452bf512007-02-09 05:32:43 +000041small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
Jeremy Hylton81e95022007-02-27 06:50:52 +000042 import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000043expr_stmt: testlist (augassign (yield_expr|testlist) |
44 ('=' (yield_expr|testlist))*)
45augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
46 '<<=' | '>>=' | '**=' | '//=')
Thomas Wouters434d0822000-08-24 20:11:32 +000047# For normal assignments, additional restrictions enforced by the interpreter
Guido van Rossum56f78371991-07-17 18:39:15 +000048del_stmt: 'del' exprlist
49pass_stmt: 'pass'
Tim Peters5ca576e2001-06-18 22:08:13 +000050flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000051break_stmt: 'break'
52continue_stmt: 'continue'
53return_stmt: 'return' [testlist]
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000054yield_stmt: yield_expr
Guido van Rossumd295f121998-04-09 21:39:57 +000055raise_stmt: 'raise' [test [',' test [',' test]]]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +000056import_stmt: import_name | import_from
57import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +000058# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
59import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000060 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum45aecf42006-03-15 04:58:47 +000061import_as_name: NAME ['as' NAME]
62dotted_as_name: dotted_name ['as' NAME]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +000063import_as_names: import_as_name (',' import_as_name)* [',']
64dotted_as_names: dotted_as_name (',' dotted_as_name)*
Guido van Rossum4a1da261995-01-07 10:25:36 +000065dotted_name: NAME ('.' NAME)*
Guido van Rossum68fc3491991-12-10 13:51:08 +000066global_stmt: 'global' NAME (',' NAME)*
Jeremy Hylton81e95022007-02-27 06:50:52 +000067nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
Guido van Rossum03a74661997-04-16 00:34:46 +000068assert_stmt: 'assert' test [',' test]
Guido van Rossum25831651993-05-19 14:50:45 +000069
Guido van Rossumc2e20742006-02-27 22:32:47 +000070compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef
Guido van Rossume5f6f451994-09-29 10:05:45 +000071if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000073for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000074try_stmt: ('try' ':' suite
75 ((except_clause ':' suite)+
76 ['else' ':' suite]
77 ['finally' ':' suite] |
78 'finally' ':' suite))
Guido van Rossumc2e20742006-02-27 22:32:47 +000079with_stmt: 'with' test [ with_var ] ':' suite
Guido van Rossum45aecf42006-03-15 04:58:47 +000080with_var: 'as' expr
Guido van Rossumaf821411992-03-31 18:49:18 +000081# NB compile.c makes sure that the default except clause is last
Guido van Rossum16be03e2007-01-10 18:51:35 +000082except_clause: 'except' [test ['as' NAME]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +000083suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084
Thomas Woutersdca3b9c2006-02-27 00:24:13 +000085# Backward compatibility cruft to support:
86# [ x for x in lambda: True, lambda: False if x() ]
87# even while also allowing:
88# lambda x: 5 if x else 2
89# (But not a mix of the two)
90testlist_safe: old_test [(',' old_test)+ [',']]
91old_test: or_test | old_lambdef
92old_lambdef: 'lambda' [varargslist] ':' old_test
93
94test: or_test ['if' or_test 'else' test] | lambdef
95or_test: and_test ('or' and_test)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096and_test: not_test ('and' not_test)*
97not_test: 'not' not_test | comparison
98comparison: expr (comp_op expr)*
Guido van Rossumb053cd82006-08-24 03:53:23 +000099comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000100expr: xor_expr ('|' xor_expr)*
101xor_expr: and_expr ('^' and_expr)*
102and_expr: shift_expr ('&' shift_expr)*
103shift_expr: arith_expr (('<<'|'>>') arith_expr)*
104arith_expr: term (('+'|'-') term)*
Guido van Rossum4668b002001-08-08 05:00:18 +0000105term: factor (('*'|'/'|'%'|'//') factor)*
Guido van Rossum0bfd6c31996-01-12 01:00:58 +0000106factor: ('+'|'-'|'~') factor | power
Tim Peters84ee3232002-05-23 20:05:40 +0000107power: atom trailer* ['**' factor]
Neal Norwitz3c52c5a2005-12-18 04:12:30 +0000108atom: ('(' [yield_expr|testlist_gexp] ')' |
109 '[' [listmaker] ']' |
Guido van Rossum86e58e22006-08-28 15:27:34 +0000110 '{' [dictsetmaker] '}' |
Georg Brandldde00282007-03-18 19:01:53 +0000111 NAME | NUMBER | STRING+ | '...')
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000112listmaker: test ( list_for | (',' test)* [','] )
Raymond Hettinger354433a2004-05-19 08:20:33 +0000113testlist_gexp: test ( gen_for | (',' test)* [','] )
Guido van Rossum590baa41993-11-30 13:40:46 +0000114lambdef: 'lambda' [varargslist] ':' test
Guido van Rossum14f44511996-07-30 16:43:44 +0000115trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
116subscriptlist: subscript (',' subscript)* [',']
Georg Brandl52318d62006-09-06 07:06:08 +0000117subscript: test | [test] ':' [test] [sliceop]
Guido van Rossum14f44511996-07-30 16:43:44 +0000118sliceop: ':' [test]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119exprlist: expr (',' expr)* [',']
120testlist: test (',' test)* [',']
Guido van Rossum86e58e22006-08-28 15:27:34 +0000121dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000123classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
Guido van Rossuma996b911995-07-07 22:26:23 +0000124
Jeremy Hylton76901512000-03-28 23:49:17 +0000125arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
Neal Norwitzd074beb2006-02-24 23:11:14 +0000126argument: test [gen_for] | test '=' test # Really [keyword '='] test
Skip Montanaro803d6e52000-08-12 18:09:51 +0000127
128list_iter: list_for | list_if
Guido van Rossum1c917072001-10-15 15:44:05 +0000129list_for: 'for' exprlist 'in' testlist_safe [list_iter]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130list_if: 'if' old_test [list_iter]
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000131
Raymond Hettinger354433a2004-05-19 08:20:33 +0000132gen_iter: gen_for | gen_if
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000133gen_for: 'for' exprlist 'in' or_test [gen_iter]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134gen_if: 'if' old_test [gen_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +0000135
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000136testlist1: test (',' test)*
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000137
138# not used in grammar, but may appear in "node" passed from Parser to Compiler
139encoding_decl: NAME
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000140
141yield_expr: 'yield' [testlist]