blob: a93d3e224fd71ba70420684ed5de5a4b6902186c [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 Rossum610cdc51992-01-26 18:17:23 +00005# 16-Jan-92:
6# Added '*' as alternative for '+' in varargs syntax
7# (Not sure which alternative is better yet.)
8
Guido van Rossum526e9091992-01-14 18:27:17 +00009# 11-Jan-92:
10# Variable length argument list syntax added: def f(a, b, +rest): ...
11
12# 8-Jan-92:
13# Allow only '==' for equality testing
Guido van Rossum6cf12731991-12-31 13:11:56 +000014
15# Changes since version 8:
16# Trailing commas in formal parameter lists are allowed
Guido van Rossum68fc3491991-12-10 13:51:08 +000017
18# Changes since version 7:
19# New syntax to specify base classes (but old syntax retained for now)
20# 'global' statement (valid only in functions but not enforced here)
Guido van Rossum9eb4f531991-10-24 14:54:25 +000021
22# Changes since version 6:
23# Add logical operators '|', '^', '&' and '~'
24# Add shift operators '<<' and '>>'
Guido van Rossuma76fb5b1991-10-20 20:10:09 +000025
26# Changes since version 5:
27# Comparison operators '<=' '>' '<>' are now 1 token
28# Also support '!=' and '==' as alternatives for '<>' and '='
Guido van Rossum56f78371991-07-17 18:39:15 +000029
30# Changes compared to version 4:
Guido van Rossum7ac4a881991-07-27 21:29:47 +000031# Blank lines and lines only containing a comment are now eaten
32# by the lexer, so the NEWLINE* things in suite are gone
33# (but the 2nd NEWLINE terminating single_input stays!)
Guido van Rossum56f78371991-07-17 18:39:15 +000034# Semicolons can separate small statements
35# 'continue' statement
36# Dictionary constructors: {key:value, key:value, ...}
37# More tests instead of exprs
Guido van Rossum4dae2161991-01-21 15:07:21 +000038
39# Changes compared to version 3:
40# Removed 'dir' statement.
41# Function call argument is a testlist instead of exprlist.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042
43# Changes compared to version 2:
44# The syntax of Boolean operations is changed to use more
45# conventional priorities: or < and < not.
46
47# Changes compared to version 1:
48# modules and scripts are unified;
49# 'quit' is gone (use ^D);
50# empty_stmt is gone, replaced by explicit NEWLINE where appropriate;
51# 'import' and 'def' aren't special any more;
52# added 'from' NAME option on import clause, and '*' to import all;
53# added class definition.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054
55# Start symbols for the grammar:
56# single_input is a single interactive statement;
57# file_input is a module or sequence of commands read from an input file;
58# expr_input is the input for the input() function;
59# eval_input is the input for the eval() function.
60
61# NB: compound_stmt in single_input is followed by extra NEWLINE!
62single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
63file_input: (NEWLINE | stmt)* ENDMARKER
64expr_input: testlist NEWLINE
65eval_input: testlist ENDMARKER
66
67funcdef: 'def' NAME parameters ':' suite
Guido van Rossum526e9091992-01-14 18:27:17 +000068parameters: '(' [varargslist] ')'
Guido van Rossum610cdc51992-01-26 18:17:23 +000069varargslist: (fpdef ',')* ('+'|'*') NAME | fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070fpdef: NAME | '(' fplist ')'
Guido van Rossum526e9091992-01-14 18:27:17 +000071fplist: fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072
73stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000074simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossum68fc3491991-12-10 13:51:08 +000075small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt
Guido van Rossum09cea471992-01-01 14:51:57 +000076expr_stmt: (exprlist '=')* exprlist
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077# For assignments, additional restrictions enforced by the interpreter
Guido van Rossum56f78371991-07-17 18:39:15 +000078print_stmt: 'print' (test ',')* [test]
79del_stmt: 'del' exprlist
80pass_stmt: 'pass'
81flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
82break_stmt: 'break'
83continue_stmt: 'continue'
84return_stmt: 'return' [testlist]
85raise_stmt: 'raise' test [',' test]
86import_stmt: 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum68fc3491991-12-10 13:51:08 +000087global_stmt: 'global' NAME (',' NAME)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
89if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
90while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000091for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092try_stmt: 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000093except_clause: 'except' [test [',' test]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +000094suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095
96test: and_test ('or' and_test)*
97and_test: not_test ('and' not_test)*
98not_test: 'not' not_test | comparison
99comparison: expr (comp_op expr)*
Guido van Rossum6cf12731991-12-31 13:11:56 +0000100comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000101expr: xor_expr ('|' xor_expr)*
102xor_expr: and_expr ('^' and_expr)*
103and_expr: shift_expr ('&' shift_expr)*
104shift_expr: arith_expr (('<<'|'>>') arith_expr)*
105arith_expr: term (('+'|'-') term)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106term: factor (('*'|'/'|'%') factor)*
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000107factor: ('+'|'-'|'~') factor | atom trailer*
Guido van Rossum56f78371991-07-17 18:39:15 +0000108atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
Guido van Rossum4dae2161991-01-21 15:07:21 +0000109trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
Guido van Rossum56f78371991-07-17 18:39:15 +0000110subscript: test | [test] ':' [test]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111exprlist: expr (',' expr)* [',']
112testlist: test (',' test)* [',']
Guido van Rossum56f78371991-07-17 18:39:15 +0000113dictmaker: test ':' test (',' test ':' test)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114
Guido van Rossum68fc3491991-12-10 13:51:08 +0000115# New class syntax should be:
116# classdef: class NAME ['(' testlist ')'] ':' suite
117# but merged with old syntax for compatibility it becomes:
118classdef: 'class' NAME ['(' testlist ')' |'(' ')' ['=' baselist]] ':' suite
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119baselist: atom arguments (',' atom arguments)*
Guido van Rossum68fc3491991-12-10 13:51:08 +0000120arguments: '(' ')'