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