blob: a6169d1425204321450073e2e0deeccf23883896 [file] [log] [blame]
Guido van Rossum4dae2161991-01-21 15:07:21 +00001# Grammar for Python, version 4
2
3# Changes compared to version 3:
4# Removed 'dir' statement.
5# Function call argument is a testlist instead of exprlist.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
7# Changes compared to version 2:
8# The syntax of Boolean operations is changed to use more
9# conventional priorities: or < and < not.
10
11# Changes compared to version 1:
12# modules and scripts are unified;
13# 'quit' is gone (use ^D);
14# empty_stmt is gone, replaced by explicit NEWLINE where appropriate;
15# 'import' and 'def' aren't special any more;
16# added 'from' NAME option on import clause, and '*' to import all;
17# added class definition.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018
19# Start symbols for the grammar:
20# single_input is a single interactive statement;
21# file_input is a module or sequence of commands read from an input file;
22# expr_input is the input for the input() function;
23# eval_input is the input for the eval() function.
24
25# NB: compound_stmt in single_input is followed by extra NEWLINE!
26single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
27file_input: (NEWLINE | stmt)* ENDMARKER
28expr_input: testlist NEWLINE
29eval_input: testlist ENDMARKER
30
31funcdef: 'def' NAME parameters ':' suite
32parameters: '(' [fplist] ')'
33fplist: fpdef (',' fpdef)*
34fpdef: NAME | '(' fplist ')'
35
36stmt: simple_stmt | compound_stmt
Guido van Rossum4dae2161991-01-21 15:07:21 +000037simple_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038expr_stmt: (exprlist '=')* exprlist NEWLINE
39# For assignments, additional restrictions enforced by the interpreter
40print_stmt: 'print' (test ',')* [test] NEWLINE
41del_stmt: 'del' exprlist NEWLINE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042pass_stmt: 'pass' NEWLINE
43flow_stmt: break_stmt | return_stmt | raise_stmt
44break_stmt: 'break' NEWLINE
45return_stmt: 'return' [testlist] NEWLINE
46raise_stmt: 'raise' expr [',' expr] NEWLINE
47import_stmt: 'import' NAME (',' NAME)* NEWLINE | 'from' NAME 'import' ('*' | NAME (',' NAME)*) NEWLINE
48compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
49if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
50while_stmt: 'while' test ':' suite ['else' ':' suite]
51for_stmt: 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
52try_stmt: 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite]
53except_clause: 'except' [expr [',' expr]]
54suite: simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
55
56test: and_test ('or' and_test)*
57and_test: not_test ('and' not_test)*
58not_test: 'not' not_test | comparison
59comparison: expr (comp_op expr)*
60comp_op: '<'|'>'|'='|'>' '='|'<' '='|'<' '>'|'in'|'not' 'in'|'is'|'is' 'not'
61expr: term (('+'|'-') term)*
62term: factor (('*'|'/'|'%') factor)*
63factor: ('+'|'-') factor | atom trailer*
64atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' '}' | '`' testlist '`' | NAME | NUMBER | STRING
Guido van Rossum4dae2161991-01-21 15:07:21 +000065trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066subscript: expr | [expr] ':' [expr]
67exprlist: expr (',' expr)* [',']
68testlist: test (',' test)* [',']
69
70classdef: 'class' NAME parameters ['=' baselist] ':' suite
71baselist: atom arguments (',' atom arguments)*
72arguments: '(' [testlist] ')'