blob: 354fe607691c22882dbc71c2001d9510264accad [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:
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+
Guido van Rossumd59da4b2007-05-22 18:11:13 +000024decorated: decorators (classdef | funcdef)
25funcdef: 'def' NAME parameters ['->' test] ':' suite
Neal Norwitzc1505362006-12-28 06:47:50 +000026parameters: '(' [typedargslist] ')'
Benjamin Peterson605a7742013-03-20 00:39:41 -050027typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [','
Mark Dickinson1c50d112010-07-12 14:14:18 +000028 ['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef]]
29 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
Guido van Rossum1bc535d2007-05-15 18:46:22 +000030tfpdef: NAME [':' test]
Mark Dickinson1c50d112010-07-12 14:14:18 +000031varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [','
32 ['*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef]]
33 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef)
Guido van Rossum1bc535d2007-05-15 18:46:22 +000034vfpdef: NAME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
36stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000037simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossum452bf512007-02-09 05:32:43 +000038small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
Jeremy Hylton81e95022007-02-27 06:50:52 +000039 import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
Benjamin Peterson4905e802009-09-27 02:43:28 +000040expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
41 ('=' (yield_expr|testlist_star_expr))*)
42testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -040043augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000044 '<<=' | '>>=' | '**=' | '//=')
Thomas Wouters434d0822000-08-24 20:11:32 +000045# For normal assignments, additional restrictions enforced by the interpreter
Guido van Rossum56f78371991-07-17 18:39:15 +000046del_stmt: 'del' exprlist
47pass_stmt: 'pass'
Tim Peters5ca576e2001-06-18 22:08:13 +000048flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000049break_stmt: 'break'
50continue_stmt: 'continue'
51return_stmt: 'return' [testlist]
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000052yield_stmt: yield_expr
Collin Winter828f04a2007-08-31 00:04:24 +000053raise_stmt: 'raise' [test ['from' test]]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +000054import_stmt: import_name | import_from
55import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +000056# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
57import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000058 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum45aecf42006-03-15 04:58:47 +000059import_as_name: NAME ['as' NAME]
60dotted_as_name: dotted_name ['as' NAME]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +000061import_as_names: import_as_name (',' import_as_name)* [',']
62dotted_as_names: dotted_as_name (',' dotted_as_name)*
Guido van Rossum4a1da261995-01-07 10:25:36 +000063dotted_name: NAME ('.' NAME)*
Guido van Rossum68fc3491991-12-10 13:51:08 +000064global_stmt: 'global' NAME (',' NAME)*
Jeremy Hylton81e95022007-02-27 06:50:52 +000065nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
Guido van Rossum03a74661997-04-16 00:34:46 +000066assert_stmt: 'assert' test [',' test]
Guido van Rossum25831651993-05-19 14:50:45 +000067
Guido van Rossumd59da4b2007-05-22 18:11:13 +000068compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossume5f6f451994-09-29 10:05:45 +000069if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000071for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000072try_stmt: ('try' ':' suite
73 ((except_clause ':' suite)+
Benjamin Petersonaefc1c72010-07-05 20:04:54 +000074 ['else' ':' suite]
75 ['finally' ':' suite] |
76 'finally' ':' suite))
Georg Brandl0c315622009-05-25 21:10:36 +000077with_stmt: 'with' with_item (',' with_item)* ':' suite
78with_item: test ['as' expr]
Guido van Rossumaf821411992-03-31 18:49:18 +000079# NB compile.c makes sure that the default except clause is last
Guido van Rossum16be03e2007-01-10 18:51:35 +000080except_clause: 'except' [test ['as' NAME]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +000081suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082
Thomas Woutersdca3b9c2006-02-27 00:24:13 +000083test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +000084test_nocond: or_test | lambdef_nocond
85lambdef: 'lambda' [varargslist] ':' test
86lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
Thomas Woutersdca3b9c2006-02-27 00:24:13 +000087or_test: and_test ('or' and_test)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088and_test: not_test ('and' not_test)*
89not_test: 'not' not_test | comparison
Benjamin Peterson4905e802009-09-27 02:43:28 +000090comparison: expr (comp_op expr)*
Eli Bendersky0e79b7e2011-11-14 01:16:31 +020091# <> isn't actually a valid comparison operator in Python. It's here for the
92# sake of a __future__ import described in PEP 401
Brett Cannone3944a52009-04-01 05:08:41 +000093comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Benjamin Peterson4905e802009-09-27 02:43:28 +000094star_expr: '*' expr
Guido van Rossum9eb4f531991-10-24 14:54:25 +000095expr: xor_expr ('|' xor_expr)*
96xor_expr: and_expr ('^' and_expr)*
97and_expr: shift_expr ('&' shift_expr)*
98shift_expr: arith_expr (('<<'|'>>') arith_expr)*
99arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -0400100term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Guido van Rossum0bfd6c31996-01-12 01:00:58 +0000101factor: ('+'|'-'|'~') factor | power
Tim Peters84ee3232002-05-23 20:05:40 +0000102power: atom trailer* ['**' factor]
Nick Coghlan650f0d02007-04-15 12:05:43 +0000103atom: ('(' [yield_expr|testlist_comp] ')' |
104 '[' [testlist_comp] ']' |
105 '{' [dictorsetmaker] '}' |
Guido van Rossume7ba4952007-06-06 23:52:48 +0000106 NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
Benjamin Peterson4905e802009-09-27 02:43:28 +0000107testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
Guido van Rossum14f44511996-07-30 16:43:44 +0000108trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
109subscriptlist: subscript (',' subscript)* [',']
Georg Brandl52318d62006-09-06 07:06:08 +0000110subscript: test | [test] ':' [test] [sliceop]
Guido van Rossum14f44511996-07-30 16:43:44 +0000111sliceop: ':' [test]
Benjamin Peterson4905e802009-09-27 02:43:28 +0000112exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113testlist: test (',' test)* [',']
Guido van Rossum992d4a32007-07-11 13:09:30 +0000114dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
Nick Coghlan650f0d02007-04-15 12:05:43 +0000115 (test (comp_for | (',' test)* [','])) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000117classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
Guido van Rossuma996b911995-07-07 22:26:23 +0000118
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000119arglist: (argument ',')* (argument [',']
120 |'*' test (',' argument)* [',' '**' test]
121 |'**' test)
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000122# The reason that keywords are test nodes instead of NAME is that using NAME
123# results in an ambiguity. ast.c makes sure it's a NAME.
Nick Coghlan4c1be9e2012-01-14 16:03:07 +1000124argument: test [comp_for] | test '=' test # Really [keyword '='] test
Nick Coghlan650f0d02007-04-15 12:05:43 +0000125comp_iter: comp_for | comp_if
126comp_for: 'for' exprlist 'in' or_test [comp_iter]
127comp_if: 'if' test_nocond [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +0000128
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000129# not used in grammar, but may appear in "node" passed from Parser to Compiler
130encoding_decl: NAME
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000131
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000132yield_expr: 'yield' [yield_arg]
133yield_arg: 'from' test | testlist