blob: 2e0b20c46d936305937e7217cafbb754e89fec99 [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 Rossumda5d5181994-08-01 11:00:20 +00005# 3-May-94:
6# Added else clause to try-except
7
8# 17-Apr-94:
9# Added string literal concatenation
10
11# 13-Apr-94:
12# Added default values for function/lambda argument lists
13
Guido van Rossum590baa41993-11-30 13:40:46 +000014# 30-Nov-93:
15# Removed lambda_input, added lambdef
16
Guido van Rossum12d12c51993-10-26 17:58:25 +000017# 25-Oct-93:
18# Added lambda_input
19
Guido van Rossumdb3165e1993-10-18 17:06:59 +000020# 18-Oct-93:
21# Use testlist instead of exprlist in expr_stmt
22# Add exec statement
23
Guido van Rossum25831651993-05-19 14:50:45 +000024# 19-May-93:
25# Add access statement
26
27# 18-May-93:
28# Abolish old class header syntax
29
Guido van Rossumcf49ac51992-04-06 14:39:51 +000030# 06-Apr-92:
31# Use only '*' for varargs list
32
33# 31-Mar-92:
34# Tighten syntax for try statements
35
Guido van Rossume785fbc1992-03-04 16:41:24 +000036# 27-Feb-92:
37# Allow NEWLINE* after eval input
38
Guido van Rossum610cdc51992-01-26 18:17:23 +000039# 16-Jan-92:
40# Added '*' as alternative for '+' in varargs syntax
41# (Not sure which alternative is better yet.)
42
Guido van Rossum526e9091992-01-14 18:27:17 +000043# 11-Jan-92:
44# Variable length argument list syntax added: def f(a, b, +rest): ...
45
46# 8-Jan-92:
47# Allow only '==' for equality testing
Guido van Rossum6cf12731991-12-31 13:11:56 +000048
49# Changes since version 8:
50# Trailing commas in formal parameter lists are allowed
Guido van Rossum68fc3491991-12-10 13:51:08 +000051
52# Changes since version 7:
53# New syntax to specify base classes (but old syntax retained for now)
54# 'global' statement (valid only in functions but not enforced here)
Guido van Rossum9eb4f531991-10-24 14:54:25 +000055
56# Changes since version 6:
57# Add logical operators '|', '^', '&' and '~'
58# Add shift operators '<<' and '>>'
Guido van Rossuma76fb5b1991-10-20 20:10:09 +000059
60# Changes since version 5:
61# Comparison operators '<=' '>' '<>' are now 1 token
62# Also support '!=' and '==' as alternatives for '<>' and '='
Guido van Rossum56f78371991-07-17 18:39:15 +000063
64# Changes compared to version 4:
Guido van Rossum7ac4a881991-07-27 21:29:47 +000065# Blank lines and lines only containing a comment are now eaten
66# by the lexer, so the NEWLINE* things in suite are gone
67# (but the 2nd NEWLINE terminating single_input stays!)
Guido van Rossum56f78371991-07-17 18:39:15 +000068# Semicolons can separate small statements
69# 'continue' statement
70# Dictionary constructors: {key:value, key:value, ...}
71# More tests instead of exprs
Guido van Rossum4dae2161991-01-21 15:07:21 +000072
73# Changes compared to version 3:
74# Removed 'dir' statement.
75# Function call argument is a testlist instead of exprlist.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076
77# Changes compared to version 2:
78# The syntax of Boolean operations is changed to use more
79# conventional priorities: or < and < not.
80
81# Changes compared to version 1:
82# modules and scripts are unified;
83# 'quit' is gone (use ^D);
84# empty_stmt is gone, replaced by explicit NEWLINE where appropriate;
85# 'import' and 'def' aren't special any more;
86# added 'from' NAME option on import clause, and '*' to import all;
87# added class definition.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088
89# Start symbols for the grammar:
90# single_input is a single interactive statement;
91# file_input is a module or sequence of commands read from an input file;
Guido van Rossum590baa41993-11-30 13:40:46 +000092# eval_input is the input for the eval() and input() functions.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093
94# NB: compound_stmt in single_input is followed by extra NEWLINE!
95single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
96file_input: (NEWLINE | stmt)* ENDMARKER
Guido van Rossume785fbc1992-03-04 16:41:24 +000097eval_input: testlist NEWLINE* ENDMARKER
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098
99funcdef: 'def' NAME parameters ':' suite
Guido van Rossum526e9091992-01-14 18:27:17 +0000100parameters: '(' [varargslist] ')'
Guido van Rossumda5d5181994-08-01 11:00:20 +0000101varargslist: (fpdef ['=' test] ',')* '*' NAME | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102fpdef: NAME | '(' fplist ')'
Guido van Rossum526e9091992-01-14 18:27:17 +0000103fplist: fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104
105stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +0000106simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossumdb3165e1993-10-18 17:06:59 +0000107small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
108expr_stmt: (testlist '=')* testlist
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109# For assignments, additional restrictions enforced by the interpreter
Guido van Rossum56f78371991-07-17 18:39:15 +0000110print_stmt: 'print' (test ',')* [test]
111del_stmt: 'del' exprlist
112pass_stmt: 'pass'
113flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
114break_stmt: 'break'
115continue_stmt: 'continue'
116return_stmt: 'return' [testlist]
117raise_stmt: 'raise' test [',' test]
118import_stmt: 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum68fc3491991-12-10 13:51:08 +0000119global_stmt: 'global' NAME (',' NAME)*
Guido van Rossumb3f72581993-05-21 19:56:10 +0000120access_stmt: 'access' ('*' | NAME (',' NAME)*) ':' accesstype (',' accesstype)*
Guido van Rossum25831651993-05-19 14:50:45 +0000121accesstype: NAME+
122# accesstype should be ('public' | 'protected' | 'private') ['read'] ['write']
123# but can't be because that would create undesirable reserved words!
Guido van Rossum12d12c51993-10-26 17:58:25 +0000124exec_stmt: 'exec' expr ['in' test [',' test]]
Guido van Rossum25831651993-05-19 14:50:45 +0000125
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
127if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
128while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +0000129for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Guido van Rossumda5d5181994-08-01 11:00:20 +0000130try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] | 'try' ':' suite 'finally' ':' suite
Guido van Rossumaf821411992-03-31 18:49:18 +0000131# NB compile.c makes sure that the default except clause is last
Guido van Rossum56f78371991-07-17 18:39:15 +0000132except_clause: 'except' [test [',' test]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +0000133suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134
Guido van Rossum57531fe1993-11-30 14:57:42 +0000135test: and_test ('or' and_test)* | lambdef
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136and_test: not_test ('and' not_test)*
137not_test: 'not' not_test | comparison
138comparison: expr (comp_op expr)*
Guido van Rossum6cf12731991-12-31 13:11:56 +0000139comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000140expr: xor_expr ('|' xor_expr)*
141xor_expr: and_expr ('^' and_expr)*
142and_expr: shift_expr ('&' shift_expr)*
143shift_expr: arith_expr (('<<'|'>>') arith_expr)*
144arith_expr: term (('+'|'-') term)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145term: factor (('*'|'/'|'%') factor)*
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000146factor: ('+'|'-'|'~') factor | atom trailer*
Guido van Rossumda5d5181994-08-01 11:00:20 +0000147atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
Guido van Rossum590baa41993-11-30 13:40:46 +0000148lambdef: 'lambda' [varargslist] ':' test
Guido van Rossum4dae2161991-01-21 15:07:21 +0000149trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
Guido van Rossum56f78371991-07-17 18:39:15 +0000150subscript: test | [test] ':' [test]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151exprlist: expr (',' expr)* [',']
152testlist: test (',' test)* [',']
Guido van Rossum56f78371991-07-17 18:39:15 +0000153dictmaker: test ':' test (',' test ':' test)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154
Guido van Rossum248a50c1993-12-20 12:53:10 +0000155classdef: 'class' NAME ['(' testlist ')'] ':' suite