blob: 24dc70e3d8863651c6b78848fdf60c25a7fbed47 [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 Rossume785fbc1992-03-04 16:41:24 +00005# 27-Feb-92:
6# Allow NEWLINE* after eval input
7
Guido van Rossum610cdc51992-01-26 18:17:23 +00008# 16-Jan-92:
9# Added '*' as alternative for '+' in varargs syntax
10# (Not sure which alternative is better yet.)
11
Guido van Rossum526e9091992-01-14 18:27:17 +000012# 11-Jan-92:
13# Variable length argument list syntax added: def f(a, b, +rest): ...
14
15# 8-Jan-92:
16# Allow only '==' for equality testing
Guido van Rossum6cf12731991-12-31 13:11:56 +000017
18# Changes since version 8:
19# Trailing commas in formal parameter lists are allowed
Guido van Rossum68fc3491991-12-10 13:51:08 +000020
21# Changes since version 7:
22# New syntax to specify base classes (but old syntax retained for now)
23# 'global' statement (valid only in functions but not enforced here)
Guido van Rossum9eb4f531991-10-24 14:54:25 +000024
25# Changes since version 6:
26# Add logical operators '|', '^', '&' and '~'
27# Add shift operators '<<' and '>>'
Guido van Rossuma76fb5b1991-10-20 20:10:09 +000028
29# Changes since version 5:
30# Comparison operators '<=' '>' '<>' are now 1 token
31# Also support '!=' and '==' as alternatives for '<>' and '='
Guido van Rossum56f78371991-07-17 18:39:15 +000032
33# Changes compared to version 4:
Guido van Rossum7ac4a881991-07-27 21:29:47 +000034# Blank lines and lines only containing a comment are now eaten
35# by the lexer, so the NEWLINE* things in suite are gone
36# (but the 2nd NEWLINE terminating single_input stays!)
Guido van Rossum56f78371991-07-17 18:39:15 +000037# Semicolons can separate small statements
38# 'continue' statement
39# Dictionary constructors: {key:value, key:value, ...}
40# More tests instead of exprs
Guido van Rossum4dae2161991-01-21 15:07:21 +000041
42# Changes compared to version 3:
43# Removed 'dir' statement.
44# Function call argument is a testlist instead of exprlist.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
46# Changes compared to version 2:
47# The syntax of Boolean operations is changed to use more
48# conventional priorities: or < and < not.
49
50# Changes compared to version 1:
51# modules and scripts are unified;
52# 'quit' is gone (use ^D);
53# empty_stmt is gone, replaced by explicit NEWLINE where appropriate;
54# 'import' and 'def' aren't special any more;
55# added 'from' NAME option on import clause, and '*' to import all;
56# added class definition.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057
58# Start symbols for the grammar:
59# single_input is a single interactive statement;
60# file_input is a module or sequence of commands read from an input file;
61# expr_input is the input for the input() function;
62# eval_input is the input for the eval() function.
63
64# NB: compound_stmt in single_input is followed by extra NEWLINE!
65single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
66file_input: (NEWLINE | stmt)* ENDMARKER
67expr_input: testlist NEWLINE
Guido van Rossume785fbc1992-03-04 16:41:24 +000068eval_input: testlist NEWLINE* ENDMARKER
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069
70funcdef: 'def' NAME parameters ':' suite
Guido van Rossum526e9091992-01-14 18:27:17 +000071parameters: '(' [varargslist] ')'
Guido van Rossum610cdc51992-01-26 18:17:23 +000072varargslist: (fpdef ',')* ('+'|'*') NAME | fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073fpdef: NAME | '(' fplist ')'
Guido van Rossum526e9091992-01-14 18:27:17 +000074fplist: fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075
76stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000077simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossum68fc3491991-12-10 13:51:08 +000078small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt
Guido van Rossum09cea471992-01-01 14:51:57 +000079expr_stmt: (exprlist '=')* exprlist
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080# For assignments, additional restrictions enforced by the interpreter
Guido van Rossum56f78371991-07-17 18:39:15 +000081print_stmt: 'print' (test ',')* [test]
82del_stmt: 'del' exprlist
83pass_stmt: 'pass'
84flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
85break_stmt: 'break'
86continue_stmt: 'continue'
87return_stmt: 'return' [testlist]
88raise_stmt: 'raise' test [',' test]
89import_stmt: 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum68fc3491991-12-10 13:51:08 +000090global_stmt: 'global' NAME (',' NAME)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
92if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
93while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000094for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Guido van Rossumaf821411992-03-31 18:49:18 +000095try_stmt: 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' ':' suite
96# NB compile.c makes sure that the default except clause is last
Guido van Rossum56f78371991-07-17 18:39:15 +000097except_clause: 'except' [test [',' test]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +000098suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099
100test: and_test ('or' and_test)*
101and_test: not_test ('and' not_test)*
102not_test: 'not' not_test | comparison
103comparison: expr (comp_op expr)*
Guido van Rossum6cf12731991-12-31 13:11:56 +0000104comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000105expr: xor_expr ('|' xor_expr)*
106xor_expr: and_expr ('^' and_expr)*
107and_expr: shift_expr ('&' shift_expr)*
108shift_expr: arith_expr (('<<'|'>>') arith_expr)*
109arith_expr: term (('+'|'-') term)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110term: factor (('*'|'/'|'%') factor)*
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000111factor: ('+'|'-'|'~') factor | atom trailer*
Guido van Rossum56f78371991-07-17 18:39:15 +0000112atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
Guido van Rossum4dae2161991-01-21 15:07:21 +0000113trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
Guido van Rossum56f78371991-07-17 18:39:15 +0000114subscript: test | [test] ':' [test]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115exprlist: expr (',' expr)* [',']
116testlist: test (',' test)* [',']
Guido van Rossum56f78371991-07-17 18:39:15 +0000117dictmaker: test ':' test (',' test ':' test)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118
Guido van Rossum68fc3491991-12-10 13:51:08 +0000119# New class syntax should be:
120# classdef: class NAME ['(' testlist ')'] ':' suite
121# but merged with old syntax for compatibility it becomes:
122classdef: 'class' NAME ['(' testlist ')' |'(' ')' ['=' baselist]] ':' suite
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123baselist: atom arguments (',' atom arguments)*
Guido van Rossum68fc3491991-12-10 13:51:08 +0000124arguments: '(' ')'