blob: 59c807ef0c8fa9eee110ceb94c4eb41eb793e4d6 [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
4# https://docs.python.org/devguide/grammar.html
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 Rossume5f6f451994-09-29 10:05:45 +000010# NB: compound_stmt in single_input is followed by extra NEWLINE!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
12file_input: (NEWLINE | stmt)* ENDMARKER
Guido van Rossume785fbc1992-03-04 16:41:24 +000013eval_input: testlist NEWLINE* ENDMARKER
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Michael W. Hudson0ccff072004-08-17 17:29:16 +000015decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
16decorators: decorator+
Yury Selivanov75445082015-05-11 22:57:16 -040017decorated: decorators (classdef | funcdef | async_funcdef)
18
19async_funcdef: ASYNC funcdef
Guido van Rossumd59da4b2007-05-22 18:11:13 +000020funcdef: 'def' NAME parameters ['->' test] ':' suite
Yury Selivanov75445082015-05-11 22:57:16 -040021
Neal Norwitzc1505362006-12-28 06:47:50 +000022parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +120023typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
24 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
25 | '**' tfpdef [',']]]
26 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
27 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +000028tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +120029varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
30 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
31 | '**' vfpdef [',']]]
32 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
33 | '**' vfpdef [',']
34)
Guido van Rossum1bc535d2007-05-15 18:46:22 +000035vfpdef: NAME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
37stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000038simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossum452bf512007-02-09 05:32:43 +000039small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
Jeremy Hylton81e95022007-02-27 06:50:52 +000040 import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070041expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Benjamin Peterson4905e802009-09-27 02:43:28 +000042 ('=' (yield_expr|testlist_star_expr))*)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070043annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +000044testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -040045augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000046 '<<=' | '>>=' | '**=' | '//=')
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070047# For normal and annotated 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
Collin Winter828f04a2007-08-31 00:04:24 +000055raise_stmt: 'raise' [test ['from' 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
Yury Selivanov75445082015-05-11 22:57:16 -040070compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
71async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
Guido van Rossume5f6f451994-09-29 10:05:45 +000072if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000074for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000075try_stmt: ('try' ':' suite
76 ((except_clause ':' suite)+
Benjamin Petersonaefc1c72010-07-05 20:04:54 +000077 ['else' ':' suite]
78 ['finally' ':' suite] |
79 'finally' ':' suite))
Georg Brandl0c315622009-05-25 21:10:36 +000080with_stmt: 'with' with_item (',' with_item)* ':' suite
81with_item: test ['as' expr]
Guido van Rossumaf821411992-03-31 18:49:18 +000082# NB compile.c makes sure that the default except clause is last
Guido van Rossum16be03e2007-01-10 18:51:35 +000083except_clause: 'except' [test ['as' NAME]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +000084suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085
Thomas Woutersdca3b9c2006-02-27 00:24:13 +000086test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +000087test_nocond: or_test | lambdef_nocond
88lambdef: 'lambda' [varargslist] ':' test
89lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
Thomas Woutersdca3b9c2006-02-27 00:24:13 +000090or_test: and_test ('or' and_test)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091and_test: not_test ('and' not_test)*
92not_test: 'not' not_test | comparison
Benjamin Peterson4905e802009-09-27 02:43:28 +000093comparison: expr (comp_op expr)*
Eli Bendersky0e79b7e2011-11-14 01:16:31 +020094# <> isn't actually a valid comparison operator in Python. It's here for the
Martin v. Löwisec3af4e2014-08-05 17:56:52 +020095# sake of a __future__ import described in PEP 401 (which really works :-)
Brett Cannone3944a52009-04-01 05:08:41 +000096comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Benjamin Peterson4905e802009-09-27 02:43:28 +000097star_expr: '*' expr
Guido van Rossum9eb4f531991-10-24 14:54:25 +000098expr: xor_expr ('|' xor_expr)*
99xor_expr: and_expr ('^' and_expr)*
100and_expr: shift_expr ('&' shift_expr)*
101shift_expr: arith_expr (('<<'|'>>') arith_expr)*
102arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -0400103term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Guido van Rossum0bfd6c31996-01-12 01:00:58 +0000104factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -0400105power: atom_expr ['**' factor]
106atom_expr: [AWAIT] atom trailer*
Nick Coghlan650f0d02007-04-15 12:05:43 +0000107atom: ('(' [yield_expr|testlist_comp] ')' |
108 '[' [testlist_comp] ']' |
109 '{' [dictorsetmaker] '}' |
Guido van Rossume7ba4952007-06-06 23:52:48 +0000110 NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
Benjamin Peterson4905e802009-09-27 02:43:28 +0000111testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
Guido van Rossum14f44511996-07-30 16:43:44 +0000112trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
113subscriptlist: subscript (',' subscript)* [',']
Georg Brandl52318d62006-09-06 07:06:08 +0000114subscript: test | [test] ':' [test] [sliceop]
Guido van Rossum14f44511996-07-30 16:43:44 +0000115sliceop: ':' [test]
Benjamin Peterson4905e802009-09-27 02:43:28 +0000116exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117testlist: test (',' test)* [',']
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400118dictorsetmaker: ( ((test ':' test | '**' expr)
119 (comp_for | (',' (test ':' test | '**' expr))* [','])) |
120 ((test | star_expr)
121 (comp_for | (',' (test | star_expr))* [','])) )
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
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400125arglist: argument (',' argument)* [',']
126
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000127# The reason that keywords are test nodes instead of NAME is that using NAME
128# results in an ambiguity. ast.c makes sure it's a NAME.
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400129# "test '=' test" is really "keyword '=' test", but we have no such token.
130# These need to be in a single rule to avoid grammar that is ambiguous
131# to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,
132# we explicitly match '*' here, too, to give it proper precedence.
133# Illegal combinations and orderings are blocked in ast.c:
Berker Peksag5d9c7ed2016-07-15 16:12:39 +0300134# multiple (test comp_for) arguments are blocked; keyword unpackings
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400135# that precede iterable unpackings are blocked; etc.
136argument: ( test [comp_for] |
137 test '=' test |
Benjamin Petersonde12b792015-05-16 09:44:45 -0400138 '**' test |
Yury Selivanov14acf5f2015-08-05 17:54:10 -0400139 '*' test )
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400140
Nick Coghlan650f0d02007-04-15 12:05:43 +0000141comp_iter: comp_for | comp_if
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700142comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
Nick Coghlan650f0d02007-04-15 12:05:43 +0000143comp_if: 'if' test_nocond [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +0000144
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000145# not used in grammar, but may appear in "node" passed from Parser to Compiler
146encoding_decl: NAME
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000147
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000148yield_expr: 'yield' [yield_arg]
149yield_arg: 'from' test | testlist