blob: 3462edd7c266a5c298610b8b7ea2f16cff560772 [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
5# 11-Jan-92:
6# Variable length argument list syntax added: def f(a, b, +rest): ...
7
8# 8-Jan-92:
9# Allow only '==' for equality testing
Guido van Rossum6cf12731991-12-31 13:11:56 +000010
11# Changes since version 8:
12# Trailing commas in formal parameter lists are allowed
Guido van Rossum68fc3491991-12-10 13:51:08 +000013
14# Changes since version 7:
15# New syntax to specify base classes (but old syntax retained for now)
16# 'global' statement (valid only in functions but not enforced here)
Guido van Rossum9eb4f531991-10-24 14:54:25 +000017
18# Changes since version 6:
19# Add logical operators '|', '^', '&' and '~'
20# Add shift operators '<<' and '>>'
Guido van Rossuma76fb5b1991-10-20 20:10:09 +000021
22# Changes since version 5:
23# Comparison operators '<=' '>' '<>' are now 1 token
24# Also support '!=' and '==' as alternatives for '<>' and '='
Guido van Rossum56f78371991-07-17 18:39:15 +000025
26# Changes compared to version 4:
Guido van Rossum7ac4a881991-07-27 21:29:47 +000027# Blank lines and lines only containing a comment are now eaten
28# by the lexer, so the NEWLINE* things in suite are gone
29# (but the 2nd NEWLINE terminating single_input stays!)
Guido van Rossum56f78371991-07-17 18:39:15 +000030# Semicolons can separate small statements
31# 'continue' statement
32# Dictionary constructors: {key:value, key:value, ...}
33# More tests instead of exprs
Guido van Rossum4dae2161991-01-21 15:07:21 +000034
35# Changes compared to version 3:
36# Removed 'dir' statement.
37# Function call argument is a testlist instead of exprlist.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038
39# Changes compared to version 2:
40# The syntax of Boolean operations is changed to use more
41# conventional priorities: or < and < not.
42
43# Changes compared to version 1:
44# modules and scripts are unified;
45# 'quit' is gone (use ^D);
46# empty_stmt is gone, replaced by explicit NEWLINE where appropriate;
47# 'import' and 'def' aren't special any more;
48# added 'from' NAME option on import clause, and '*' to import all;
49# added class definition.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050
51# Start symbols for the grammar:
52# single_input is a single interactive statement;
53# file_input is a module or sequence of commands read from an input file;
54# expr_input is the input for the input() function;
55# eval_input is the input for the eval() function.
56
57# NB: compound_stmt in single_input is followed by extra NEWLINE!
58single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
59file_input: (NEWLINE | stmt)* ENDMARKER
60expr_input: testlist NEWLINE
61eval_input: testlist ENDMARKER
62
63funcdef: 'def' NAME parameters ':' suite
Guido van Rossum526e9091992-01-14 18:27:17 +000064parameters: '(' [varargslist] ')'
65varargslist: (fpdef ',')* '+' NAME | fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066fpdef: NAME | '(' fplist ')'
Guido van Rossum526e9091992-01-14 18:27:17 +000067fplist: fpdef (',' fpdef)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068
69stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000070simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossum68fc3491991-12-10 13:51:08 +000071small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt
Guido van Rossum09cea471992-01-01 14:51:57 +000072expr_stmt: (exprlist '=')* exprlist
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073# For assignments, additional restrictions enforced by the interpreter
Guido van Rossum56f78371991-07-17 18:39:15 +000074print_stmt: 'print' (test ',')* [test]
75del_stmt: 'del' exprlist
76pass_stmt: 'pass'
77flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
78break_stmt: 'break'
79continue_stmt: 'continue'
80return_stmt: 'return' [testlist]
81raise_stmt: 'raise' test [',' test]
82import_stmt: 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum68fc3491991-12-10 13:51:08 +000083global_stmt: 'global' NAME (',' NAME)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
85if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
86while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000087for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088try_stmt: 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000089except_clause: 'except' [test [',' test]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +000090suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091
92test: and_test ('or' and_test)*
93and_test: not_test ('and' not_test)*
94not_test: 'not' not_test | comparison
95comparison: expr (comp_op expr)*
Guido van Rossum6cf12731991-12-31 13:11:56 +000096comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Guido van Rossum9eb4f531991-10-24 14:54:25 +000097expr: xor_expr ('|' xor_expr)*
98xor_expr: and_expr ('^' and_expr)*
99and_expr: shift_expr ('&' shift_expr)*
100shift_expr: arith_expr (('<<'|'>>') arith_expr)*
101arith_expr: term (('+'|'-') term)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102term: factor (('*'|'/'|'%') factor)*
Guido van Rossum9eb4f531991-10-24 14:54:25 +0000103factor: ('+'|'-'|'~') factor | atom trailer*
Guido van Rossum56f78371991-07-17 18:39:15 +0000104atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
Guido van Rossum4dae2161991-01-21 15:07:21 +0000105trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
Guido van Rossum56f78371991-07-17 18:39:15 +0000106subscript: test | [test] ':' [test]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107exprlist: expr (',' expr)* [',']
108testlist: test (',' test)* [',']
Guido van Rossum56f78371991-07-17 18:39:15 +0000109dictmaker: test ':' test (',' test ':' test)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110
Guido van Rossum68fc3491991-12-10 13:51:08 +0000111# New class syntax should be:
112# classdef: class NAME ['(' testlist ')'] ':' suite
113# but merged with old syntax for compatibility it becomes:
114classdef: 'class' NAME ['(' testlist ')' |'(' ')' ['=' baselist]] ':' suite
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115baselist: atom arguments (',' atom arguments)*
Guido van Rossum68fc3491991-12-10 13:51:08 +0000116arguments: '(' ')'