blob: cb4bc2718e890b98f575424ce658a769b0f5e224 [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 Rossum25831651993-05-19 14:50:45 +00005# 19-May-93:
6# Add access statement
7
8# 18-May-93:
9# Abolish old class header syntax
10
Guido van Rossumcf49ac51992-04-06 14:39:51 +000011# 06-Apr-92:
12# Use only '*' for varargs list
13
14# 31-Mar-92:
15# Tighten syntax for try statements
16
Guido van Rossume785fbc1992-03-04 16:41:24 +000017# 27-Feb-92:
18# Allow NEWLINE* after eval input
19
Guido van Rossum610cdc51992-01-26 18:17:23 +000020# 16-Jan-92:
21# Added '*' as alternative for '+' in varargs syntax
22# (Not sure which alternative is better yet.)
23
Guido van Rossum526e9091992-01-14 18:27:17 +000024# 11-Jan-92:
25# Variable length argument list syntax added: def f(a, b, +rest): ...
26
27# 8-Jan-92:
28# Allow only '==' for equality testing
Guido van Rossum6cf12731991-12-31 13:11:56 +000029
30# Changes since version 8:
31# Trailing commas in formal parameter lists are allowed
Guido van Rossum68fc3491991-12-10 13:51:08 +000032
33# Changes since version 7:
34# New syntax to specify base classes (but old syntax retained for now)
35# 'global' statement (valid only in functions but not enforced here)
Guido van Rossum9eb4f531991-10-24 14:54:25 +000036
37# Changes since version 6:
38# Add logical operators '|', '^', '&' and '~'
39# Add shift operators '<<' and '>>'
Guido van Rossuma76fb5b1991-10-20 20:10:09 +000040
41# Changes since version 5:
42# Comparison operators '<=' '>' '<>' are now 1 token
43# Also support '!=' and '==' as alternatives for '<>' and '='
Guido van Rossum56f78371991-07-17 18:39:15 +000044
45# Changes compared to version 4:
Guido van Rossum7ac4a881991-07-27 21:29:47 +000046# Blank lines and lines only containing a comment are now eaten
47# by the lexer, so the NEWLINE* things in suite are gone
48# (but the 2nd NEWLINE terminating single_input stays!)
Guido van Rossum56f78371991-07-17 18:39:15 +000049# Semicolons can separate small statements
50# 'continue' statement
51# Dictionary constructors: {key:value, key:value, ...}
52# More tests instead of exprs
Guido van Rossum4dae2161991-01-21 15:07:21 +000053
54# Changes compared to version 3:
55# Removed 'dir' statement.
56# Function call argument is a testlist instead of exprlist.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057
58# Changes compared to version 2:
59# The syntax of Boolean operations is changed to use more
60# conventional priorities: or < and < not.
61
62# Changes compared to version 1:
63# modules and scripts are unified;
64# 'quit' is gone (use ^D);
65# empty_stmt is gone, replaced by explicit NEWLINE where appropriate;
66# 'import' and 'def' aren't special any more;
67# added 'from' NAME option on import clause, and '*' to import all;
68# added class definition.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069
70# Start symbols for the grammar:
71# single_input is a single interactive statement;
72# file_input is a module or sequence of commands read from an input file;
73# expr_input is the input for the input() function;
74# eval_input is the input for the eval() function.
75
76# NB: compound_stmt in single_input is followed by extra NEWLINE!
77single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
78file_input: (NEWLINE | stmt)* ENDMARKER
79expr_input: testlist NEWLINE
Guido van Rossume785fbc1992-03-04 16:41:24 +000080eval_input: testlist NEWLINE* ENDMARKER
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081
82funcdef: 'def' NAME parameters ':' suite
Guido van Rossum526e9091992-01-14 18:27:17 +000083parameters: '(' [varargslist] ')'
Guido van Rossum02334d21992-04-06 12:36:19 +000084varargslist: (fpdef ',')* '*' NAME | fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085fpdef: NAME | '(' fplist ')'
Guido van Rossum526e9091992-01-14 18:27:17 +000086fplist: fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087
88stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000089simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossum25831651993-05-19 14:50:45 +000090small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | access_stmt
Guido van Rossum09cea471992-01-01 14:51:57 +000091expr_stmt: (exprlist '=')* exprlist
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092# For assignments, additional restrictions enforced by the interpreter
Guido van Rossum56f78371991-07-17 18:39:15 +000093print_stmt: 'print' (test ',')* [test]
94del_stmt: 'del' exprlist
95pass_stmt: 'pass'
96flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
97break_stmt: 'break'
98continue_stmt: 'continue'
99return_stmt: 'return' [testlist]
100raise_stmt: 'raise' test [',' test]
101import_stmt: 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum68fc3491991-12-10 13:51:08 +0000102global_stmt: 'global' NAME (',' NAME)*
Guido van Rossum25831651993-05-19 14:50:45 +0000103access_stmt: 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)*
104accesstype: NAME+
105# accesstype should be ('public' | 'protected' | 'private') ['read'] ['write']
106# but can't be because that would create undesirable reserved words!
107
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
109if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
110while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +0000111for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Guido van Rossumaf821411992-03-31 18:49:18 +0000112try_stmt: 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' ':' suite
113# NB compile.c makes sure that the default except clause is last
Guido van Rossum56f78371991-07-17 18:39:15 +0000114except_clause: 'except' [test [',' test]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +0000115suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116
117test: and_test ('or' and_test)*
118and_test: not_test ('and' not_test)*
119not_test: 'not' not_test | comparison
120comparison: expr (comp_op expr)*
Guido van Rossum6cf12731991-12-31 13:11:56 +0000121comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000122expr: xor_expr ('|' xor_expr)*
123xor_expr: and_expr ('^' and_expr)*
124and_expr: shift_expr ('&' shift_expr)*
125shift_expr: arith_expr (('<<'|'>>') arith_expr)*
126arith_expr: term (('+'|'-') term)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127term: factor (('*'|'/'|'%') factor)*
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000128factor: ('+'|'-'|'~') factor | atom trailer*
Guido van Rossum56f78371991-07-17 18:39:15 +0000129atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
Guido van Rossum4dae2161991-01-21 15:07:21 +0000130trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
Guido van Rossum56f78371991-07-17 18:39:15 +0000131subscript: test | [test] ':' [test]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132exprlist: expr (',' expr)* [',']
133testlist: test (',' test)* [',']
Guido van Rossum56f78371991-07-17 18:39:15 +0000134dictmaker: test ':' test (',' test ':' test)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135
Guido van Rossum25831651993-05-19 14:50:45 +0000136classdef: class NAME ['(' testlist ')'] ':' suite