blob: b014c72e8dfa3615e4147b7f4b967c9d3be6dde8 [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
Anthony Baxter623acf62006-04-12 05:16:30 +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# Commands for Kees Blom's railroad program
Guido van Rossuma3228621994-08-17 13:19:13 +000014#diagram:token NAME
15#diagram:token NUMBER
16#diagram:token STRING
17#diagram:token NEWLINE
18#diagram:token ENDMARKER
19#diagram:token INDENT
20#diagram:output\input python.bla
21#diagram:token DEDENT
22#diagram:output\textwidth 20.04cm\oddsidemargin 0.0cm\evensidemargin 0.0cm
23#diagram:rules
Guido van Rossume5f6f451994-09-29 10:05:45 +000024
25# Start symbols for the grammar:
26# single_input is a single interactive statement;
27# file_input is a module or sequence of commands read from an input file;
28# eval_input is the input for the eval() and input() functions.
29# NB: compound_stmt in single_input is followed by extra NEWLINE!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
31file_input: (NEWLINE | stmt)* ENDMARKER
Guido van Rossume785fbc1992-03-04 16:41:24 +000032eval_input: testlist NEWLINE* ENDMARKER
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033
Michael W. Hudson0ccff072004-08-17 17:29:16 +000034decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
35decorators: decorator+
Christian Heimes5224d282008-02-23 15:01:05 +000036decorated: decorators (classdef | funcdef)
37funcdef: 'def' NAME parameters ':' suite
Guido van Rossum526e9091992-01-14 18:27:17 +000038parameters: '(' [varargslist] ')'
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000039varargslist: ((fpdef ['=' test] ',')*
40 ('*' NAME [',' '**' NAME] | '**' NAME) |
41 fpdef ['=' test] (',' fpdef ['=' test])* [','])
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042fpdef: NAME | '(' fplist ')'
Guido van Rossum526e9091992-01-14 18:27:17 +000043fplist: fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
45stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000046simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000047small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
48 import_stmt | global_stmt | exec_stmt | assert_stmt)
49expr_stmt: testlist (augassign (yield_expr|testlist) |
50 ('=' (yield_expr|testlist))*)
51augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
52 '<<=' | '>>=' | '**=' | '//=')
Thomas Wouters434d0822000-08-24 20:11:32 +000053# For normal assignments, additional restrictions enforced by the interpreter
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000054print_stmt: 'print' ( [ test (',' test)* [','] ] |
55 '>>' test [ (',' test)+ [','] ] )
Guido van Rossum56f78371991-07-17 18:39:15 +000056del_stmt: 'del' exprlist
57pass_stmt: 'pass'
Tim Peters5ca576e2001-06-18 22:08:13 +000058flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000059break_stmt: 'break'
60continue_stmt: 'continue'
61return_stmt: 'return' [testlist]
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000062yield_stmt: yield_expr
Guido van Rossumd295f121998-04-09 21:39:57 +000063raise_stmt: 'raise' [test [',' test [',' test]]]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +000064import_stmt: import_name | import_from
65import_name: 'import' dotted_as_names
Thomas Wouterscf8229e2006-05-25 11:25:51 +000066import_from: ('from' ('.'* dotted_name | '.'+)
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000067 'import' ('*' | '(' import_as_names ')' | import_as_names))
Neal Norwitzca460d92006-09-06 06:28:06 +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)*
Guido van Rossum12d12c51993-10-26 17:58:25 +000074exec_stmt: 'exec' expr ['in' test [',' test]]
Guido van Rossum03a74661997-04-16 00:34:46 +000075assert_stmt: 'assert' test [',' test]
Guido van Rossum25831651993-05-19 14:50:45 +000076
Christian Heimes5224d282008-02-23 15:01:05 +000077compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Guido van Rossume5f6f451994-09-29 10:05:45 +000078if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000080for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Neal Norwitz3c52c5a2005-12-18 04:12:30 +000081try_stmt: ('try' ':' suite
82 ((except_clause ':' suite)+
83 ['else' ':' suite]
84 ['finally' ':' suite] |
85 'finally' ':' suite))
Guido van Rossumc2e20742006-02-27 22:32:47 +000086with_stmt: 'with' test [ with_var ] ':' suite
Neal Norwitzca460d92006-09-06 06:28:06 +000087with_var: 'as' expr
Guido van Rossumaf821411992-03-31 18:49:18 +000088# NB compile.c makes sure that the default except clause is last
Collin Winter62903052007-05-18 23:11:24 +000089except_clause: 'except' [test [('as' | ',') test]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +000090suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091
Thomas Woutersdca3b9c2006-02-27 00:24:13 +000092# Backward compatibility cruft to support:
93# [ x for x in lambda: True, lambda: False if x() ]
94# even while also allowing:
95# lambda x: 5 if x else 2
96# (But not a mix of the two)
97testlist_safe: old_test [(',' old_test)+ [',']]
98old_test: or_test | old_lambdef
99old_lambdef: 'lambda' [varargslist] ':' old_test
100
101test: or_test ['if' or_test 'else' test] | lambdef
102or_test: and_test ('or' and_test)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103and_test: not_test ('and' not_test)*
104not_test: 'not' not_test | comparison
105comparison: expr (comp_op expr)*
Guido van Rossum6cf12731991-12-31 13:11:56 +0000106comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000107expr: xor_expr ('|' xor_expr)*
108xor_expr: and_expr ('^' and_expr)*
109and_expr: shift_expr ('&' shift_expr)*
110shift_expr: arith_expr (('<<'|'>>') arith_expr)*
111arith_expr: term (('+'|'-') term)*
Guido van Rossum4668b002001-08-08 05:00:18 +0000112term: factor (('*'|'/'|'%'|'//') factor)*
Guido van Rossum0bfd6c31996-01-12 01:00:58 +0000113factor: ('+'|'-'|'~') factor | power
Tim Peters84ee3232002-05-23 20:05:40 +0000114power: atom trailer* ['**' factor]
Neal Norwitz3c52c5a2005-12-18 04:12:30 +0000115atom: ('(' [yield_expr|testlist_gexp] ')' |
116 '[' [listmaker] ']' |
117 '{' [dictmaker] '}' |
118 '`' testlist1 '`' |
119 NAME | NUMBER | STRING+)
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000120listmaker: test ( list_for | (',' test)* [','] )
Raymond Hettinger354433a2004-05-19 08:20:33 +0000121testlist_gexp: test ( gen_for | (',' test)* [','] )
Guido van Rossum590baa41993-11-30 13:40:46 +0000122lambdef: 'lambda' [varargslist] ':' test
Guido van Rossum14f44511996-07-30 16:43:44 +0000123trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
124subscriptlist: subscript (',' subscript)* [',']
125subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
126sliceop: ':' [test]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127exprlist: expr (',' expr)* [',']
128testlist: test (',' test)* [',']
Guido van Rossum56f78371991-07-17 18:39:15 +0000129dictmaker: test ':' test (',' test ':' test)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130
Brett Cannon409d8f22005-03-05 06:47:57 +0000131classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
Guido van Rossuma996b911995-07-07 22:26:23 +0000132
Jeremy Hylton76901512000-03-28 23:49:17 +0000133arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
Neal Norwitzd074beb2006-02-24 23:11:14 +0000134argument: test [gen_for] | test '=' test # Really [keyword '='] test
Skip Montanaro803d6e52000-08-12 18:09:51 +0000135
136list_iter: list_for | list_if
Guido van Rossum1c917072001-10-15 15:44:05 +0000137list_for: 'for' exprlist 'in' testlist_safe [list_iter]
Thomas Woutersbb64e512006-04-12 00:06:34 +0000138list_if: 'if' old_test [list_iter]
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000139
Raymond Hettinger354433a2004-05-19 08:20:33 +0000140gen_iter: gen_for | gen_if
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000141gen_for: 'for' exprlist 'in' or_test [gen_iter]
Thomas Woutersbb64e512006-04-12 00:06:34 +0000142gen_if: 'if' old_test [gen_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +0000143
Guido van Rossum2d3b9862002-05-24 15:47:06 +0000144testlist1: test (',' test)*
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000145
146# not used in grammar, but may appear in "node" passed from Parser to Compiler
147encoding_decl: NAME
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000148
149yield_expr: 'yield' [testlist]