blob: b139e9f66c8ce0bd2d3ed585e81ff13b00445395 [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
Berker Peksag192c7502015-06-13 11:18:33 +030010# NOTE WELL: You should also follow all the steps listed at
11# https://docs.python.org/devguide/grammar.html
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000012
Guido van Rossume5f6f451994-09-29 10:05:45 +000013# Start symbols for the grammar:
Benjamin Petersonaefc1c72010-07-05 20:04:54 +000014# single_input is a single interactive statement;
15# file_input is a module or sequence of commands read from an input file;
Benjamin Petersonb3132bd2011-12-15 15:43:56 -050016# eval_input is the input for the eval() functions.
Guido van Rossume5f6f451994-09-29 10:05:45 +000017# 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+
Yury Selivanov75445082015-05-11 22:57:16 -040024decorated: decorators (classdef | funcdef | async_funcdef)
25
26async_funcdef: ASYNC funcdef
Guido van Rossumd59da4b2007-05-22 18:11:13 +000027funcdef: 'def' NAME parameters ['->' test] ':' suite
Yury Selivanov75445082015-05-11 22:57:16 -040028
Neal Norwitzc1505362006-12-28 06:47:50 +000029parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +120030typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
31 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
32 | '**' tfpdef [',']]]
33 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
34 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +000035tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +120036varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
37 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
38 | '**' vfpdef [',']]]
39 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
40 | '**' vfpdef [',']
41)
Guido van Rossum1bc535d2007-05-15 18:46:22 +000042vfpdef: NAME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043
44stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000045simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossum452bf512007-02-09 05:32:43 +000046small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
Jeremy Hylton81e95022007-02-27 06:50:52 +000047 import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070048expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Benjamin Peterson4905e802009-09-27 02:43:28 +000049 ('=' (yield_expr|testlist_star_expr))*)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070050annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +000051testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -040052augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000053 '<<=' | '>>=' | '**=' | '//=')
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070054# For normal and annotated assignments, additional restrictions enforced by the interpreter
Guido van Rossum56f78371991-07-17 18:39:15 +000055del_stmt: 'del' exprlist
56pass_stmt: 'pass'
Tim Peters5ca576e2001-06-18 22:08:13 +000057flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000058break_stmt: 'break'
59continue_stmt: 'continue'
60return_stmt: 'return' [testlist]
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000061yield_stmt: yield_expr
Collin Winter828f04a2007-08-31 00:04:24 +000062raise_stmt: 'raise' [test ['from' test]]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +000063import_stmt: import_name | import_from
64import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +000065# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
66import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000067 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum45aecf42006-03-15 04:58:47 +000068import_as_name: NAME ['as' NAME]
69dotted_as_name: dotted_name ['as' NAME]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +000070import_as_names: import_as_name (',' import_as_name)* [',']
71dotted_as_names: dotted_as_name (',' dotted_as_name)*
Guido van Rossum4a1da261995-01-07 10:25:36 +000072dotted_name: NAME ('.' NAME)*
Guido van Rossum68fc3491991-12-10 13:51:08 +000073global_stmt: 'global' NAME (',' NAME)*
Jeremy Hylton81e95022007-02-27 06:50:52 +000074nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
Guido van Rossum03a74661997-04-16 00:34:46 +000075assert_stmt: 'assert' test [',' test]
Guido van Rossum25831651993-05-19 14:50:45 +000076
Yury Selivanov75445082015-05-11 22:57:16 -040077compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
78async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
Guido van Rossume5f6f451994-09-29 10:05:45 +000079if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000081for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000082try_stmt: ('try' ':' suite
83 ((except_clause ':' suite)+
Benjamin Petersonaefc1c72010-07-05 20:04:54 +000084 ['else' ':' suite]
85 ['finally' ':' suite] |
86 'finally' ':' suite))
Georg Brandl0c315622009-05-25 21:10:36 +000087with_stmt: 'with' with_item (',' with_item)* ':' suite
88with_item: test ['as' expr]
Guido van Rossumaf821411992-03-31 18:49:18 +000089# NB compile.c makes sure that the default except clause is last
Guido van Rossum16be03e2007-01-10 18:51:35 +000090except_clause: 'except' [test ['as' NAME]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +000091suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092
Thomas Woutersdca3b9c2006-02-27 00:24:13 +000093test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +000094test_nocond: or_test | lambdef_nocond
95lambdef: 'lambda' [varargslist] ':' test
96lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
Thomas Woutersdca3b9c2006-02-27 00:24:13 +000097or_test: and_test ('or' and_test)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098and_test: not_test ('and' not_test)*
99not_test: 'not' not_test | comparison
Benjamin Peterson4905e802009-09-27 02:43:28 +0000100comparison: expr (comp_op expr)*
Eli Bendersky0e79b7e2011-11-14 01:16:31 +0200101# <> isn't actually a valid comparison operator in Python. It's here for the
Martin v. Löwisec3af4e2014-08-05 17:56:52 +0200102# sake of a __future__ import described in PEP 401 (which really works :-)
Brett Cannone3944a52009-04-01 05:08:41 +0000103comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Benjamin Peterson4905e802009-09-27 02:43:28 +0000104star_expr: '*' expr
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000105expr: xor_expr ('|' xor_expr)*
106xor_expr: and_expr ('^' and_expr)*
107and_expr: shift_expr ('&' shift_expr)*
108shift_expr: arith_expr (('<<'|'>>') arith_expr)*
109arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -0400110term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Guido van Rossum0bfd6c31996-01-12 01:00:58 +0000111factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -0400112power: atom_expr ['**' factor]
113atom_expr: [AWAIT] atom trailer*
Nick Coghlan650f0d02007-04-15 12:05:43 +0000114atom: ('(' [yield_expr|testlist_comp] ')' |
115 '[' [testlist_comp] ']' |
116 '{' [dictorsetmaker] '}' |
Guido van Rossume7ba4952007-06-06 23:52:48 +0000117 NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
Benjamin Peterson4905e802009-09-27 02:43:28 +0000118testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
Guido van Rossum14f44511996-07-30 16:43:44 +0000119trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
120subscriptlist: subscript (',' subscript)* [',']
Georg Brandl52318d62006-09-06 07:06:08 +0000121subscript: test | [test] ':' [test] [sliceop]
Guido van Rossum14f44511996-07-30 16:43:44 +0000122sliceop: ':' [test]
Benjamin Peterson4905e802009-09-27 02:43:28 +0000123exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124testlist: test (',' test)* [',']
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400125dictorsetmaker: ( ((test ':' test | '**' expr)
126 (comp_for | (',' (test ':' test | '**' expr))* [','])) |
127 ((test | star_expr)
128 (comp_for | (',' (test | star_expr))* [','])) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000130classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
Guido van Rossuma996b911995-07-07 22:26:23 +0000131
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400132arglist: argument (',' argument)* [',']
133
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000134# The reason that keywords are test nodes instead of NAME is that using NAME
135# results in an ambiguity. ast.c makes sure it's a NAME.
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400136# "test '=' test" is really "keyword '=' test", but we have no such token.
137# These need to be in a single rule to avoid grammar that is ambiguous
138# to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,
139# we explicitly match '*' here, too, to give it proper precedence.
140# Illegal combinations and orderings are blocked in ast.c:
Berker Peksag5d9c7ed2016-07-15 16:12:39 +0300141# multiple (test comp_for) arguments are blocked; keyword unpackings
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400142# that precede iterable unpackings are blocked; etc.
143argument: ( test [comp_for] |
144 test '=' test |
Benjamin Petersonde12b792015-05-16 09:44:45 -0400145 '**' test |
Yury Selivanov14acf5f2015-08-05 17:54:10 -0400146 '*' test )
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400147
Nick Coghlan650f0d02007-04-15 12:05:43 +0000148comp_iter: comp_for | comp_if
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700149comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
Nick Coghlan650f0d02007-04-15 12:05:43 +0000150comp_if: 'if' test_nocond [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +0000151
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000152# not used in grammar, but may appear in "node" passed from Parser to Compiler
153encoding_decl: NAME
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000154
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000155yield_expr: 'yield' [yield_arg]
156yield_arg: 'from' test | testlist