blob: 494ebf026f78458ac9e4c8bf3fad0c7c5d1d979a [file] [log] [blame]
Guido van Rossum68fc3491991-12-10 13:51:08 +00001# Grammar for Python, version 8
2
3# Changes since version 7:
4# New syntax to specify base classes (but old syntax retained for now)
5# 'global' statement (valid only in functions but not enforced here)
Guido van Rossum9eb4f531991-10-24 14:54:25 +00006
7# Changes since version 6:
8# Add logical operators '|', '^', '&' and '~'
9# Add shift operators '<<' and '>>'
Guido van Rossuma76fb5b1991-10-20 20:10:09 +000010
11# Changes since version 5:
12# Comparison operators '<=' '>' '<>' are now 1 token
13# Also support '!=' and '==' as alternatives for '<>' and '='
Guido van Rossum56f78371991-07-17 18:39:15 +000014
15# Changes compared to version 4:
Guido van Rossum7ac4a881991-07-27 21:29:47 +000016# Blank lines and lines only containing a comment are now eaten
17# by the lexer, so the NEWLINE* things in suite are gone
18# (but the 2nd NEWLINE terminating single_input stays!)
Guido van Rossum56f78371991-07-17 18:39:15 +000019# Semicolons can separate small statements
20# 'continue' statement
21# Dictionary constructors: {key:value, key:value, ...}
22# More tests instead of exprs
Guido van Rossum4dae2161991-01-21 15:07:21 +000023
24# Changes compared to version 3:
25# Removed 'dir' statement.
26# Function call argument is a testlist instead of exprlist.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027
28# Changes compared to version 2:
29# The syntax of Boolean operations is changed to use more
30# conventional priorities: or < and < not.
31
32# Changes compared to version 1:
33# modules and scripts are unified;
34# 'quit' is gone (use ^D);
35# empty_stmt is gone, replaced by explicit NEWLINE where appropriate;
36# 'import' and 'def' aren't special any more;
37# added 'from' NAME option on import clause, and '*' to import all;
38# added class definition.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039
40# Start symbols for the grammar:
41# single_input is a single interactive statement;
42# file_input is a module or sequence of commands read from an input file;
43# expr_input is the input for the input() function;
44# eval_input is the input for the eval() function.
45
46# NB: compound_stmt in single_input is followed by extra NEWLINE!
47single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
48file_input: (NEWLINE | stmt)* ENDMARKER
49expr_input: testlist NEWLINE
50eval_input: testlist ENDMARKER
51
52funcdef: 'def' NAME parameters ':' suite
53parameters: '(' [fplist] ')'
54fplist: fpdef (',' fpdef)*
55fpdef: NAME | '(' fplist ')'
56
57stmt: simple_stmt | compound_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000058simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
Guido van Rossum68fc3491991-12-10 13:51:08 +000059small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt
Guido van Rossum56f78371991-07-17 18:39:15 +000060expr_stmt: (exprlist '=')* exprlist
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061# For assignments, additional restrictions enforced by the interpreter
Guido van Rossum56f78371991-07-17 18:39:15 +000062print_stmt: 'print' (test ',')* [test]
63del_stmt: 'del' exprlist
64pass_stmt: 'pass'
65flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
66break_stmt: 'break'
67continue_stmt: 'continue'
68return_stmt: 'return' [testlist]
69raise_stmt: 'raise' test [',' test]
70import_stmt: 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum68fc3491991-12-10 13:51:08 +000071global_stmt: 'global' NAME (',' NAME)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
73if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
74while_stmt: 'while' test ':' suite ['else' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000075for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076try_stmt: 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite]
Guido van Rossum56f78371991-07-17 18:39:15 +000077except_clause: 'except' [test [',' test]]
Guido van Rossum7ac4a881991-07-27 21:29:47 +000078suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079
80test: and_test ('or' and_test)*
81and_test: not_test ('and' not_test)*
82not_test: 'not' not_test | comparison
83comparison: expr (comp_op expr)*
Guido van Rossuma76fb5b1991-10-20 20:10:09 +000084comp_op: '<'|'>'|'='|'>='|'<='|'<>'|'!='|'=='|'in'|'not' 'in'|'is'|'is' 'not'
Guido van Rossum9eb4f531991-10-24 14:54:25 +000085expr: xor_expr ('|' xor_expr)*
86xor_expr: and_expr ('^' and_expr)*
87and_expr: shift_expr ('&' shift_expr)*
88shift_expr: arith_expr (('<<'|'>>') arith_expr)*
89arith_expr: term (('+'|'-') term)*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090term: factor (('*'|'/'|'%') factor)*
Guido van Rossum9eb4f531991-10-24 14:54:25 +000091factor: ('+'|'-'|'~') factor | atom trailer*
Guido van Rossum56f78371991-07-17 18:39:15 +000092atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
Guido van Rossum4dae2161991-01-21 15:07:21 +000093trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
Guido van Rossum56f78371991-07-17 18:39:15 +000094subscript: test | [test] ':' [test]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095exprlist: expr (',' expr)* [',']
96testlist: test (',' test)* [',']
Guido van Rossum56f78371991-07-17 18:39:15 +000097dictmaker: test ':' test (',' test ':' test)* [',']
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098
Guido van Rossum68fc3491991-12-10 13:51:08 +000099# New class syntax should be:
100# classdef: class NAME ['(' testlist ')'] ':' suite
101# but merged with old syntax for compatibility it becomes:
102classdef: 'class' NAME ['(' testlist ')' |'(' ')' ['=' baselist]] ':' suite
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103baselist: atom arguments (',' atom arguments)*
Guido van Rossum68fc3491991-12-10 13:51:08 +0000104arguments: '(' ')'