blob: 21f7e1a89115b29c96470dc5e84bbc341408501c [file] [log] [blame]
Guido van Rossum526e9091992-01-14 18:27:17 +00001# Grammar for Python
Guido van Rossum6cf12731991-12-31 13:11:56 +00002
Berker Peksag192c7502015-06-13 11:18:33 +03003# NOTE WELL: You should also follow all the steps listed at
Lisa Hewus Fresh384899d2017-08-30 09:37:43 -07004# https://devguide.python.org/grammar/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005
Guido van Rossume5f6f451994-09-29 10:05:45 +00006# Start symbols for the grammar:
Benjamin Petersonaefc1c72010-07-05 20:04:54 +00007# single_input is a single interactive statement;
8# file_input is a module or sequence of commands read from an input file;
Benjamin Petersonb3132bd2011-12-15 15:43:56 -05009# eval_input is the input for the eval() functions.
Guido van Rossumdcfcd142019-01-31 03:40:27 -080010# func_type_input is a PEP 484 Python 2 function type comment
Guido van Rossume5f6f451994-09-29 10:05:45 +000011# NB: compound_stmt in single_input is followed by extra NEWLINE!
Guido van Rossumdcfcd142019-01-31 03:40:27 -080012# NB: due to the way TYPE_COMMENT is tokenized it will always be followed by a NEWLINE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
14file_input: (NEWLINE | stmt)* ENDMARKER
Guido van Rossume785fbc1992-03-04 16:41:24 +000015eval_input: testlist NEWLINE* ENDMARKER
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000016
Michael W. Hudson0ccff072004-08-17 17:29:16 +000017decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
18decorators: decorator+
Yury Selivanov75445082015-05-11 22:57:16 -040019decorated: decorators (classdef | funcdef | async_funcdef)
20
Guido van Rossum495da292019-03-07 12:38:08 -080021async_funcdef: ASYNC funcdef
Guido van Rossumdcfcd142019-01-31 03:40:27 -080022funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] func_body_suite
Yury Selivanov75445082015-05-11 22:57:16 -040023
Neal Norwitzc1505362006-12-28 06:47:50 +000024parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010025
26# The following definition for typedarglist is equivalent to this set of rules:
27#
28# arguments = argument (',' [TYPE_COMMENT] argument)*
29# argument = tfpdef ['=' test]
30# kwargs = '**' tfpdef [','] [TYPE_COMMENT]
31# args = '*' [tfpdef]
32# kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [',' [TYPE_COMMENT] [kwargs]])
33# args_kwonly_kwargs = args kwonly_kwargs | kwargs
34# poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [',' [TYPE_COMMENT] [args_kwonly_kwargs]])
35# typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
36# typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT] typedargslist_no_posonly]])|(typedargslist_no_posonly)"
37#
38# It needs to be fully expanded to allow our LL(1) parser to work on it.
39
40typedargslist: (
41 (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])* ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] (
42 ',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [
Guido van Rossumdcfcd142019-01-31 03:40:27 -080043 '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]])
44 | '**' tfpdef [','] [TYPE_COMMENT]]])
45 | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]])
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010046 | '**' tfpdef [','] [TYPE_COMMENT]]] )
47| (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [
48 '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]])
49 | '**' tfpdef [','] [TYPE_COMMENT]]])
50 | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]])
Guido van Rossumdcfcd142019-01-31 03:40:27 -080051 | '**' tfpdef [','] [TYPE_COMMENT])
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010052)
Guido van Rossum1bc535d2007-05-15 18:46:22 +000053tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010054
55# The following definition for varargslist is equivalent to this set of rules:
56#
57# arguments = argument (',' argument )*
58# argument = vfpdef ['=' test]
59# kwargs = '**' vfpdef [',']
60# args = '*' [vfpdef]
61# kwonly_kwargs = (',' argument )* [',' [kwargs]]
62# args_kwonly_kwargs = args kwonly_kwargs | kwargs
63# poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
64# vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
65# varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | (vararglist_no_posonly)
66#
67# It needs to be fully expanded to allow our LL(1) parser to work on it.
68
69varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
70 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
71 | '**' vfpdef [',']]]
72 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
73 | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
Robert Collinsdf395992015-08-12 08:00:06 +120074 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
75 | '**' vfpdef [',']]]
76 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
77 | '**' vfpdef [',']
78)
Guido van Rossum1bc535d2007-05-15 18:46:22 +000079vfpdef: NAME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080
81stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000082simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossum452bf512007-02-09 05:32:43 +000083small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
Jeremy Hylton81e95022007-02-27 06:50:52 +000084 import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070085expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -080086 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
Pablo Galindo8565f6b2019-06-03 08:34:20 +010087annassign: ':' test ['=' (yield_expr|testlist_star_expr)]
Benjamin Peterson4905e802009-09-27 02:43:28 +000088testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -040089augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000090 '<<=' | '>>=' | '**=' | '//=')
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070091# For normal and annotated assignments, additional restrictions enforced by the interpreter
Guido van Rossum56f78371991-07-17 18:39:15 +000092del_stmt: 'del' exprlist
93pass_stmt: 'pass'
Tim Peters5ca576e2001-06-18 22:08:13 +000094flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000095break_stmt: 'break'
96continue_stmt: 'continue'
David Cuthbertfd97d1f2018-09-21 18:31:15 -070097return_stmt: 'return' [testlist_star_expr]
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000098yield_stmt: yield_expr
Collin Winter828f04a2007-08-31 00:04:24 +000099raise_stmt: 'raise' [test ['from' test]]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000100import_stmt: import_name | import_from
101import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +0000102# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
103import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
Neal Norwitz3c52c5a2005-12-18 04:12:30 +0000104 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum45aecf42006-03-15 04:58:47 +0000105import_as_name: NAME ['as' NAME]
106dotted_as_name: dotted_name ['as' NAME]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000107import_as_names: import_as_name (',' import_as_name)* [',']
108dotted_as_names: dotted_as_name (',' dotted_as_name)*
Guido van Rossum4a1da261995-01-07 10:25:36 +0000109dotted_name: NAME ('.' NAME)*
Guido van Rossum68fc3491991-12-10 13:51:08 +0000110global_stmt: 'global' NAME (',' NAME)*
Jeremy Hylton81e95022007-02-27 06:50:52 +0000111nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
Guido van Rossum03a74661997-04-16 00:34:46 +0000112assert_stmt: 'assert' test [',' test]
Guido van Rossum25831651993-05-19 14:50:45 +0000113
Yury Selivanov75445082015-05-11 22:57:16 -0400114compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
Guido van Rossum495da292019-03-07 12:38:08 -0800115async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700116if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite]
Xtreakd4fceaa2019-02-02 03:10:16 +0530117while_stmt: 'while' namedexpr_test ':' suite ['else' ':' suite]
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800118for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite]
Neal Norwitz3c52c5a2005-12-18 04:12:30 +0000119try_stmt: ('try' ':' suite
120 ((except_clause ':' suite)+
Benjamin Petersonaefc1c72010-07-05 20:04:54 +0000121 ['else' ':' suite]
122 ['finally' ':' suite] |
123 'finally' ':' suite))
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800124with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite
Georg Brandl0c315622009-05-25 21:10:36 +0000125with_item: test ['as' expr]
Guido van Rossumaf821411992-03-31 18:49:18 +0000126# NB compile.c makes sure that the default except clause is last
Guido van Rossum16be03e2007-01-10 18:51:35 +0000127except_clause: 'except' [test ['as' NAME]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +0000128suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700130namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000131test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +0000132test_nocond: or_test | lambdef_nocond
133lambdef: 'lambda' [varargslist] ':' test
134lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000135or_test: and_test ('or' and_test)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136and_test: not_test ('and' not_test)*
137not_test: 'not' not_test | comparison
Benjamin Peterson4905e802009-09-27 02:43:28 +0000138comparison: expr (comp_op expr)*
Eli Bendersky0e79b7e2011-11-14 01:16:31 +0200139# <> isn't actually a valid comparison operator in Python. It's here for the
Martin v. Löwisec3af4e2014-08-05 17:56:52 +0200140# sake of a __future__ import described in PEP 401 (which really works :-)
Brett Cannone3944a52009-04-01 05:08:41 +0000141comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Benjamin Peterson4905e802009-09-27 02:43:28 +0000142star_expr: '*' expr
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000143expr: xor_expr ('|' xor_expr)*
144xor_expr: and_expr ('^' and_expr)*
145and_expr: shift_expr ('&' shift_expr)*
146shift_expr: arith_expr (('<<'|'>>') arith_expr)*
147arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -0400148term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Guido van Rossum0bfd6c31996-01-12 01:00:58 +0000149factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -0400150power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -0800151atom_expr: [AWAIT] atom trailer*
Nick Coghlan650f0d02007-04-15 12:05:43 +0000152atom: ('(' [yield_expr|testlist_comp] ')' |
153 '[' [testlist_comp] ']' |
154 '{' [dictorsetmaker] '}' |
Guido van Rossume7ba4952007-06-06 23:52:48 +0000155 NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700156testlist_comp: (namedexpr_test|star_expr) ( comp_for | (',' (namedexpr_test|star_expr))* [','] )
Guido van Rossum14f44511996-07-30 16:43:44 +0000157trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
158subscriptlist: subscript (',' subscript)* [',']
Georg Brandl52318d62006-09-06 07:06:08 +0000159subscript: test | [test] ':' [test] [sliceop]
Guido van Rossum14f44511996-07-30 16:43:44 +0000160sliceop: ':' [test]
Benjamin Peterson4905e802009-09-27 02:43:28 +0000161exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162testlist: test (',' test)* [',']
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400163dictorsetmaker: ( ((test ':' test | '**' expr)
164 (comp_for | (',' (test ':' test | '**' expr))* [','])) |
165 ((test | star_expr)
166 (comp_for | (',' (test | star_expr))* [','])) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000168classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
Guido van Rossuma996b911995-07-07 22:26:23 +0000169
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400170arglist: argument (',' argument)* [',']
171
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000172# The reason that keywords are test nodes instead of NAME is that using NAME
173# results in an ambiguity. ast.c makes sure it's a NAME.
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400174# "test '=' test" is really "keyword '=' test", but we have no such token.
175# These need to be in a single rule to avoid grammar that is ambiguous
176# to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,
177# we explicitly match '*' here, too, to give it proper precedence.
178# Illegal combinations and orderings are blocked in ast.c:
Berker Peksag5d9c7ed2016-07-15 16:12:39 +0300179# multiple (test comp_for) arguments are blocked; keyword unpackings
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400180# that precede iterable unpackings are blocked; etc.
181argument: ( test [comp_for] |
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700182 test ':=' test |
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400183 test '=' test |
Benjamin Petersonde12b792015-05-16 09:44:45 -0400184 '**' test |
Yury Selivanov14acf5f2015-08-05 17:54:10 -0400185 '*' test )
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400186
Nick Coghlan650f0d02007-04-15 12:05:43 +0000187comp_iter: comp_for | comp_if
Jelle Zijlstraac317702017-10-05 20:24:46 -0700188sync_comp_for: 'for' exprlist 'in' or_test [comp_iter]
Guido van Rossum495da292019-03-07 12:38:08 -0800189comp_for: [ASYNC] sync_comp_for
Nick Coghlan650f0d02007-04-15 12:05:43 +0000190comp_if: 'if' test_nocond [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +0000191
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000192# not used in grammar, but may appear in "node" passed from Parser to Compiler
193encoding_decl: NAME
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000194
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000195yield_expr: 'yield' [yield_arg]
David Cuthbertfd97d1f2018-09-21 18:31:15 -0700196yield_arg: 'from' test | testlist_star_expr
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800197
198# the TYPE_COMMENT in suites is only parsed for funcdefs,
199# but can't go elsewhere due to ambiguity
200func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT
201
202func_type_input: func_type NEWLINE* ENDMARKER
203func_type: '(' [typelist] ')' '->' test
204# typelist is a modified typedargslist (see above)
205typelist: (test (',' test)* [','
206 ['*' [test] (',' test)* [',' '**' test] | '**' test]]
207 | '*' [test] (',' test)* [',' '**' test] | '**' test)