1- # Grammar for Python, version 4
1+ # Grammar for Python, version 5
2+
3+ # Changes compared to version 4:
4+ # Semicolons can separate small statements
5+ # 'continue' statement
6+ # Dictionary constructors: {key:value, key:value, ...}
7+ # More tests instead of exprs
28
39# Changes compared to version 3:
410# Removed 'dir' statement.
@@ -34,23 +40,25 @@ fplist: fpdef (',' fpdef)*
3440fpdef: NAME | '(' fplist ')'
3541
3642stmt: simple_stmt | compound_stmt
37- simple_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt
38- expr_stmt: (exprlist '=')* exprlist NEWLINE
43+ simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
44+ small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt
45+ expr_stmt: (exprlist '=')* exprlist
3946# For assignments, additional restrictions enforced by the interpreter
40- print_stmt: 'print' (test ',')* [test] NEWLINE
41- del_stmt: 'del' exprlist NEWLINE
42- pass_stmt: 'pass' NEWLINE
43- flow_stmt: break_stmt | return_stmt | raise_stmt
44- break_stmt: 'break' NEWLINE
45- return_stmt: 'return' [testlist] NEWLINE
46- raise_stmt: 'raise' expr [',' expr] NEWLINE
47- import_stmt: 'import' NAME (',' NAME)* NEWLINE | 'from' NAME 'import' ('*' | NAME (',' NAME)*) NEWLINE
47+ print_stmt: 'print' (test ',')* [test]
48+ del_stmt: 'del' exprlist
49+ pass_stmt: 'pass'
50+ flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
51+ break_stmt: 'break'
52+ continue_stmt: 'continue'
53+ return_stmt: 'return' [testlist]
54+ raise_stmt: 'raise' test [',' test]
55+ import_stmt: 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
4856compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
4957if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
5058while_stmt: 'while' test ':' suite ['else' ':' suite]
51- for_stmt: 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
59+ for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
5260try_stmt: 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite]
53- except_clause: 'except' [expr [',' expr ]]
61+ except_clause: 'except' [test [',' test ]]
5462suite: simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
5563
5664test: and_test ('or' and_test)*
@@ -61,11 +69,12 @@ comp_op: '<'|'>'|'='|'>' '='|'<' '='|'<' '>'|'in'|'not' 'in'|'is'|'is' 'not'
6169expr: term (('+'|'-') term)*
6270term: factor (('*'|'/'|'%') factor)*
6371factor: ('+'|'-') factor | atom trailer*
64- atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' '}' | '`' testlist '`' | NAME | NUMBER | STRING
72+ atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
6573trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
66- subscript: expr | [expr ] ':' [expr ]
74+ subscript: test | [test ] ':' [test ]
6775exprlist: expr (',' expr)* [',']
6876testlist: test (',' test)* [',']
77+ dictmaker: test ':' test (',' test ':' test)* [',']
6978
7079classdef: 'class' NAME parameters ['=' baselist] ':' suite
7180baselist: atom arguments (',' atom arguments)*
0 commit comments