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