blob: 33c37d264e34818c54cd0ef482d82a3f1249f050 [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
Guido van Rossume5f6f451994-09-29 10:05:45 +000010# Start symbols for the grammar:
11# single_input is a single interactive statement;
12# file_input is a module or sequence of commands read from an input file;
13# eval_input is the input for the eval() and input() functions.
14# NB: compound_stmt in single_input is followed by extra NEWLINE!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
16file_input: (NEWLINE | stmt)* ENDMARKER
Guido van Rossume785fbc1992-03-04 16:41:24 +000017eval_input: testlist NEWLINE* ENDMARKER
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018
Michael W. Hudson0ccff072004-08-17 17:29:16 +000019decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
20decorators: decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +000021funcdef: [decorators] 'def' NAME parameters ':' suite
Guido van Rossum526e9091992-01-14 18:27:17 +000022parameters: '(' [varargslist] ')'
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000023varargslist: ((fpdef ['=' test] ',')*
24 ('*' NAME [',' '**' NAME] | '**' NAME) |
25 fpdef ['=' test] (',' fpdef ['=' test])* [','])
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026fpdef: NAME | '(' fplist ')'
Guido van Rossum526e9091992-01-14 18:27:17 +000027fplist: fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
29stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000030simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000031small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
32 import_stmt | global_stmt | exec_stmt | assert_stmt)
33expr_stmt: testlist (augassign (yield_expr|testlist) |
34 ('=' (yield_expr|testlist))*)
35augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
36 '<<=' | '>>=' | '**=' | '//=')
Thomas Wouters434d0822000-08-24 20:11:32 +000037# For normal assignments, additional restrictions enforced by the interpreter
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000038print_stmt: 'print' ( [ test (',' test)* [','] ] |
39 '>>' test [ (',' test)+ [','] ] )
Guido van Rossum56f78371991-07-17 18:39:15 +000040del_stmt: 'del' exprlist
41pass_stmt: 'pass'
Tim Peters5ca576e2001-06-18 22:08:13 +000042flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000043break_stmt: 'break'
44continue_stmt: 'continue'
45return_stmt: 'return' [testlist]
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000046yield_stmt: yield_expr
Guido van Rossumd295f121998-04-09 21:39:57 +000047raise_stmt: 'raise' [test [',' test [',' test]]]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +000048import_stmt: import_name | import_from
49import_name: 'import' dotted_as_names
Thomas Woutersf7f438b2006-02-28 16:09:29 +000050import_from: ('from' ('.'* dotted_name | '.')
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000051 'import' ('*' | '(' import_as_names ')' | import_as_names))
Guido van Rossum45aecf42006-03-15 04:58:47 +000052import_as_name: NAME ['as' NAME]
53dotted_as_name: dotted_name ['as' NAME]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +000054import_as_names: import_as_name (',' import_as_name)* [',']
55dotted_as_names: dotted_as_name (',' dotted_as_name)*
Guido van Rossum4a1da261995-01-07 10:25:36 +000056dotted_name: NAME ('.' NAME)*
Guido van Rossum68fc3491991-12-10 13:51:08 +000057global_stmt: 'global' NAME (',' NAME)*
Guido van Rossum12d12c51993-10-26 17:58:25 +000058exec_stmt: 'exec' expr ['in' test [',' test]]
Guido van Rossum03a74661997-04-16 00:34:46 +000059assert_stmt: 'assert' test [',' test]
Guido van Rossum25831651993-05-19 14:50:45 +000060
Guido van Rossumc2e20742006-02-27 22:32:47 +000061compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef
Guido van Rossume5f6f451994-09-29 10:05:45 +000062if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000064for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000065try_stmt: ('try' ':' suite
66 ((except_clause ':' suite)+
67 ['else' ':' suite]
68 ['finally' ':' suite] |
69 'finally' ':' suite))
Guido van Rossumc2e20742006-02-27 22:32:47 +000070with_stmt: 'with' test [ with_var ] ':' suite
Guido van Rossum45aecf42006-03-15 04:58:47 +000071with_var: 'as' expr
Guido van Rossumaf821411992-03-31 18:49:18 +000072# NB compile.c makes sure that the default except clause is last
Guido van Rossum56f78371991-07-17 18:39:15 +000073except_clause: 'except' [test [',' test]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +000074suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075
Thomas Woutersdca3b9c2006-02-27 00:24:13 +000076# Backward compatibility cruft to support:
77# [ x for x in lambda: True, lambda: False if x() ]
78# even while also allowing:
79# lambda x: 5 if x else 2
80# (But not a mix of the two)
81testlist_safe: old_test [(',' old_test)+ [',']]
82old_test: or_test | old_lambdef
83old_lambdef: 'lambda' [varargslist] ':' old_test
84
85test: or_test ['if' or_test 'else' test] | lambdef
86or_test: and_test ('or' and_test)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087and_test: not_test ('and' not_test)*
88not_test: 'not' not_test | comparison
89comparison: expr (comp_op expr)*
Guido van Rossum6cf12731991-12-31 13:11:56 +000090comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Guido van Rossum9eb4f531991-10-24 14:54:25 +000091expr: xor_expr ('|' xor_expr)*
92xor_expr: and_expr ('^' and_expr)*
93and_expr: shift_expr ('&' shift_expr)*
94shift_expr: arith_expr (('<<'|'>>') arith_expr)*
95arith_expr: term (('+'|'-') term)*
Guido van Rossum4668b002001-08-08 05:00:18 +000096term: factor (('*'|'/'|'%'|'//') factor)*
Guido van Rossum0bfd6c31996-01-12 01:00:58 +000097factor: ('+'|'-'|'~') factor | power
Tim Peters84ee3232002-05-23 20:05:40 +000098power: atom trailer* ['**' factor]
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000099atom: ('(' [yield_expr|testlist_gexp] ')' |
100 '[' [listmaker] ']' |
101 '{' [dictmaker] '}' |
102 '`' testlist1 '`' |
103 NAME | NUMBER | STRING+)
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000104listmaker: test ( list_for | (',' test)* [','] )
Raymond Hettinger354433a2004-05-19 08:20:33 +0000105testlist_gexp: test ( gen_for | (',' test)* [','] )
Guido van Rossum590baa41993-11-30 13:40:46 +0000106lambdef: 'lambda' [varargslist] ':' test
Guido van Rossum14f44511996-07-30 16:43:44 +0000107trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
108subscriptlist: subscript (',' subscript)* [',']
109subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
110sliceop: ':' [test]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111exprlist: expr (',' expr)* [',']
112testlist: test (',' test)* [',']
Guido van Rossum56f78371991-07-17 18:39:15 +0000113dictmaker: test ':' test (',' test ':' test)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114
Brett Cannon409d8f22005-03-05 06:47:57 +0000115classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
Guido van Rossuma996b911995-07-07 22:26:23 +0000116
Jeremy Hylton76901512000-03-28 23:49:17 +0000117arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
Neal Norwitzd074beb2006-02-24 23:11:14 +0000118argument: test [gen_for] | test '=' test # Really [keyword '='] test
Skip Montanaro803d6e52000-08-12 18:09:51 +0000119
120list_iter: list_for | list_if
Guido van Rossum1c917072001-10-15 15:44:05 +0000121list_for: 'for' exprlist 'in' testlist_safe [list_iter]
Skip Montanaro803d6e52000-08-12 18:09:51 +0000122list_if: 'if' test [list_iter]
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000123
Raymond Hettinger354433a2004-05-19 08:20:33 +0000124gen_iter: gen_for | gen_if
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000125gen_for: 'for' exprlist 'in' or_test [gen_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +0000126gen_if: 'if' test [gen_iter]
127
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000128testlist1: test (',' test)*
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000129
130# not used in grammar, but may appear in "node" passed from Parser to Compiler
131encoding_decl: NAME
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000132
133yield_expr: 'yield' [testlist]