blob: 1a98a6aa69801d041572f4d9d0a90f457d781df2 [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 Rossum590baa41993-11-30 13:40:46 +00005# 30-Nov-93:
6# Removed lambda_input, added lambdef
7
Guido van Rossum12d12c51993-10-26 17:58:25 +00008# 25-Oct-93:
9# Added lambda_input
10
Guido van Rossumdb3165e1993-10-18 17:06:59 +000011# 18-Oct-93:
12# Use testlist instead of exprlist in expr_stmt
13# Add exec statement
14
Guido van Rossum25831651993-05-19 14:50:45 +000015# 19-May-93:
16# Add access statement
17
18# 18-May-93:
19# Abolish old class header syntax
20
Guido van Rossumcf49ac51992-04-06 14:39:51 +000021# 06-Apr-92:
22# Use only '*' for varargs list
23
24# 31-Mar-92:
25# Tighten syntax for try statements
26
Guido van Rossume785fbc1992-03-04 16:41:24 +000027# 27-Feb-92:
28# Allow NEWLINE* after eval input
29
Guido van Rossum610cdc51992-01-26 18:17:23 +000030# 16-Jan-92:
31# Added '*' as alternative for '+' in varargs syntax
32# (Not sure which alternative is better yet.)
33
Guido van Rossum526e9091992-01-14 18:27:17 +000034# 11-Jan-92:
35# Variable length argument list syntax added: def f(a, b, +rest): ...
36
37# 8-Jan-92:
38# Allow only '==' for equality testing
Guido van Rossum6cf12731991-12-31 13:11:56 +000039
40# Changes since version 8:
41# Trailing commas in formal parameter lists are allowed
Guido van Rossum68fc3491991-12-10 13:51:08 +000042
43# Changes since version 7:
44# New syntax to specify base classes (but old syntax retained for now)
45# 'global' statement (valid only in functions but not enforced here)
Guido van Rossum9eb4f531991-10-24 14:54:25 +000046
47# Changes since version 6:
48# Add logical operators '|', '^', '&' and '~'
49# Add shift operators '<<' and '>>'
Guido van Rossuma76fb5b1991-10-20 20:10:09 +000050
51# Changes since version 5:
52# Comparison operators '<=' '>' '<>' are now 1 token
53# Also support '!=' and '==' as alternatives for '<>' and '='
Guido van Rossum56f78371991-07-17 18:39:15 +000054
55# Changes compared to version 4:
Guido van Rossum7ac4a881991-07-27 21:29:47 +000056# Blank lines and lines only containing a comment are now eaten
57# by the lexer, so the NEWLINE* things in suite are gone
58# (but the 2nd NEWLINE terminating single_input stays!)
Guido van Rossum56f78371991-07-17 18:39:15 +000059# Semicolons can separate small statements
60# 'continue' statement
61# Dictionary constructors: {key:value, key:value, ...}
62# More tests instead of exprs
Guido van Rossum4dae2161991-01-21 15:07:21 +000063
64# Changes compared to version 3:
65# Removed 'dir' statement.
66# Function call argument is a testlist instead of exprlist.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067
68# Changes compared to version 2:
69# The syntax of Boolean operations is changed to use more
70# conventional priorities: or < and < not.
71
72# Changes compared to version 1:
73# modules and scripts are unified;
74# 'quit' is gone (use ^D);
75# empty_stmt is gone, replaced by explicit NEWLINE where appropriate;
76# 'import' and 'def' aren't special any more;
77# added 'from' NAME option on import clause, and '*' to import all;
78# added class definition.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079
80# Start symbols for the grammar:
81# single_input is a single interactive statement;
82# file_input is a module or sequence of commands read from an input file;
Guido van Rossum590baa41993-11-30 13:40:46 +000083# eval_input is the input for the eval() and input() functions.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084
85# NB: compound_stmt in single_input is followed by extra NEWLINE!
86single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
87file_input: (NEWLINE | stmt)* ENDMARKER
Guido van Rossume785fbc1992-03-04 16:41:24 +000088eval_input: 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
Guido van Rossum57531fe1993-11-30 14:57:42 +0000126test: and_test ('or' and_test)* | lambdef
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127and_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 Rossum57531fe1993-11-30 14:57:42 +0000138atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
Guido van Rossum590baa41993-11-30 13:40:46 +0000139lambdef: 'lambda' [varargslist] ':' test
Guido van Rossum4dae2161991-01-21 15:07:21 +0000140trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
Guido van Rossum56f78371991-07-17 18:39:15 +0000141subscript: test | [test] ':' [test]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142exprlist: expr (',' expr)* [',']
143testlist: test (',' test)* [',']
Guido van Rossum56f78371991-07-17 18:39:15 +0000144dictmaker: test ':' test (',' test ':' test)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145
Guido van Rossum25831651993-05-19 14:50:45 +0000146classdef: class NAME ['(' testlist ')'] ':' suite