blob: 22a48f7a0f7099d2c5b05b604f8d110c584a61a3 [file] [log] [blame]
Guido van Rossum526e9091992-01-14 18:27:17 +00001# Grammar for Python
Guido van Rossum6cf12731991-12-31 13:11:56 +00002
Guido van Rossum526e9091992-01-14 18:27:17 +00003# Change log:
4
Guido van Rossum12d12c51993-10-26 17:58:25 +00005# 25-Oct-93:
6# Added lambda_input
7
Guido van Rossumdb3165e1993-10-18 17:06:59 +00008# 18-Oct-93:
9# Use testlist instead of exprlist in expr_stmt
10# Add exec statement
11
Guido van Rossum25831651993-05-19 14:50:45 +000012# 19-May-93:
13# Add access statement
14
15# 18-May-93:
16# Abolish old class header syntax
17
Guido van Rossumcf49ac51992-04-06 14:39:51 +000018# 06-Apr-92:
19# Use only '*' for varargs list
20
21# 31-Mar-92:
22# Tighten syntax for try statements
23
Guido van Rossume785fbc1992-03-04 16:41:24 +000024# 27-Feb-92:
25# Allow NEWLINE* after eval input
26
Guido van Rossum610cdc51992-01-26 18:17:23 +000027# 16-Jan-92:
28# Added '*' as alternative for '+' in varargs syntax
29# (Not sure which alternative is better yet.)
30
Guido van Rossum526e9091992-01-14 18:27:17 +000031# 11-Jan-92:
32# Variable length argument list syntax added: def f(a, b, +rest): ...
33
34# 8-Jan-92:
35# Allow only '==' for equality testing
Guido van Rossum6cf12731991-12-31 13:11:56 +000036
37# Changes since version 8:
38# Trailing commas in formal parameter lists are allowed
Guido van Rossum68fc3491991-12-10 13:51:08 +000039
40# Changes since version 7:
41# New syntax to specify base classes (but old syntax retained for now)
42# 'global' statement (valid only in functions but not enforced here)
Guido van Rossum9eb4f531991-10-24 14:54:25 +000043
44# Changes since version 6:
45# Add logical operators '|', '^', '&' and '~'
46# Add shift operators '<<' and '>>'
Guido van Rossuma76fb5b1991-10-20 20:10:09 +000047
48# Changes since version 5:
49# Comparison operators '<=' '>' '<>' are now 1 token
50# Also support '!=' and '==' as alternatives for '<>' and '='
Guido van Rossum56f78371991-07-17 18:39:15 +000051
52# Changes compared to version 4:
Guido van Rossum7ac4a881991-07-27 21:29:47 +000053# Blank lines and lines only containing a comment are now eaten
54# by the lexer, so the NEWLINE* things in suite are gone
55# (but the 2nd NEWLINE terminating single_input stays!)
Guido van Rossum56f78371991-07-17 18:39:15 +000056# Semicolons can separate small statements
57# 'continue' statement
58# Dictionary constructors: {key:value, key:value, ...}
59# More tests instead of exprs
Guido van Rossum4dae2161991-01-21 15:07:21 +000060
61# Changes compared to version 3:
62# Removed 'dir' statement.
63# Function call argument is a testlist instead of exprlist.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064
65# Changes compared to version 2:
66# The syntax of Boolean operations is changed to use more
67# conventional priorities: or < and < not.
68
69# Changes compared to version 1:
70# modules and scripts are unified;
71# 'quit' is gone (use ^D);
72# empty_stmt is gone, replaced by explicit NEWLINE where appropriate;
73# 'import' and 'def' aren't special any more;
74# added 'from' NAME option on import clause, and '*' to import all;
75# added class definition.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076
77# Start symbols for the grammar:
78# single_input is a single interactive statement;
79# file_input is a module or sequence of commands read from an input file;
80# expr_input is the input for the input() function;
Guido van Rossum12d12c51993-10-26 17:58:25 +000081# eval_input is the input for the eval() function;
82# lambda_input is the input for the proposed lambda() function.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083
84# NB: compound_stmt in single_input is followed by extra NEWLINE!
85single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
86file_input: (NEWLINE | stmt)* ENDMARKER
Guido van Rossume785fbc1992-03-04 16:41:24 +000087eval_input: testlist NEWLINE* ENDMARKER
Guido van Rossum12d12c51993-10-26 17:58:25 +000088lambda_input: varargslist ':' testlist NEWLINE* ENDMARKER
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089
90funcdef: 'def' NAME parameters ':' suite
Guido van Rossum526e9091992-01-14 18:27:17 +000091parameters: '(' [varargslist] ')'
Guido van Rossum02334d21992-04-06 12:36:19 +000092varargslist: (fpdef ',')* '*' NAME | fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093fpdef: NAME | '(' fplist ')'
Guido van Rossum526e9091992-01-14 18:27:17 +000094fplist: fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095
96stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000097simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossumdb3165e1993-10-18 17:06:59 +000098small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
99expr_stmt: (testlist '=')* testlist
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100# For assignments, additional restrictions enforced by the interpreter
Guido van Rossum56f78371991-07-17 18:39:15 +0000101print_stmt: 'print' (test ',')* [test]
102del_stmt: 'del' exprlist
103pass_stmt: 'pass'
104flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
105break_stmt: 'break'
106continue_stmt: 'continue'
107return_stmt: 'return' [testlist]
108raise_stmt: 'raise' test [',' test]
109import_stmt: 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum68fc3491991-12-10 13:51:08 +0000110global_stmt: 'global' NAME (',' NAME)*
Guido van Rossumb3f72581993-05-21 19:56:10 +0000111access_stmt: 'access' ('*' | NAME (',' NAME)*) ':' accesstype (',' accesstype)*
Guido van Rossum25831651993-05-19 14:50:45 +0000112accesstype: NAME+
113# accesstype should be ('public' | 'protected' | 'private') ['read'] ['write']
114# but can't be because that would create undesirable reserved words!
Guido van Rossum12d12c51993-10-26 17:58:25 +0000115exec_stmt: 'exec' expr ['in' test [',' test]]
Guido van Rossum25831651993-05-19 14:50:45 +0000116
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
118if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
119while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +0000120for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Guido van Rossumaf821411992-03-31 18:49:18 +0000121try_stmt: 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' ':' suite
122# NB compile.c makes sure that the default except clause is last
Guido van Rossum56f78371991-07-17 18:39:15 +0000123except_clause: 'except' [test [',' test]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +0000124suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125
126test: and_test ('or' and_test)*
127and_test: not_test ('and' not_test)*
128not_test: 'not' not_test | comparison
129comparison: expr (comp_op expr)*
Guido van Rossum6cf12731991-12-31 13:11:56 +0000130comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000131expr: xor_expr ('|' xor_expr)*
132xor_expr: and_expr ('^' and_expr)*
133and_expr: shift_expr ('&' shift_expr)*
134shift_expr: arith_expr (('<<'|'>>') arith_expr)*
135arith_expr: term (('+'|'-') term)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136term: factor (('*'|'/'|'%') factor)*
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000137factor: ('+'|'-'|'~') factor | atom trailer*
Guido van Rossum56f78371991-07-17 18:39:15 +0000138atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
Guido van Rossum4dae2161991-01-21 15:07:21 +0000139trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
Guido van Rossum56f78371991-07-17 18:39:15 +0000140subscript: test | [test] ':' [test]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141exprlist: expr (',' expr)* [',']
142testlist: test (',' test)* [',']
Guido van Rossum56f78371991-07-17 18:39:15 +0000143dictmaker: test ':' test (',' test ':' test)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144
Guido van Rossum25831651993-05-19 14:50:45 +0000145classdef: class NAME ['(' testlist ')'] ':' suite