|
| 1 | +# 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! |
| 24 | +single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 25 | +file_input: (NEWLINE | stmt)* ENDMARKER |
| 26 | +expr_input: testlist NEWLINE |
| 27 | +eval_input: testlist ENDMARKER |
| 28 | + |
| 29 | +funcdef: 'def' NAME parameters ':' suite |
| 30 | +parameters: '(' [fplist] ')' |
| 31 | +fplist: fpdef (',' fpdef)* |
| 32 | +fpdef: NAME | '(' fplist ')' |
| 33 | + |
| 34 | +stmt: simple_stmt | compound_stmt |
| 35 | +simple_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | dir_stmt | flow_stmt | import_stmt |
| 36 | +expr_stmt: (exprlist '=')* exprlist NEWLINE |
| 37 | +# For assignments, additional restrictions enforced by the interpreter |
| 38 | +print_stmt: 'print' (test ',')* [test] NEWLINE |
| 39 | +del_stmt: 'del' exprlist NEWLINE |
| 40 | +dir_stmt: 'dir' [expr] NEWLINE |
| 41 | +pass_stmt: 'pass' NEWLINE |
| 42 | +flow_stmt: break_stmt | return_stmt | raise_stmt |
| 43 | +break_stmt: 'break' NEWLINE |
| 44 | +return_stmt: 'return' [testlist] NEWLINE |
| 45 | +raise_stmt: 'raise' expr [',' expr] NEWLINE |
| 46 | +import_stmt: 'import' NAME (',' NAME)* NEWLINE | 'from' NAME 'import' ('*' | NAME (',' NAME)*) NEWLINE |
| 47 | +compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 48 | +if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 49 | +while_stmt: 'while' test ':' suite ['else' ':' suite] |
| 50 | +for_stmt: 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
| 51 | +try_stmt: 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite] |
| 52 | +except_clause: 'except' [expr [',' expr]] |
| 53 | +suite: simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
| 54 | + |
| 55 | +test: and_test ('or' and_test)* |
| 56 | +and_test: not_test ('and' not_test)* |
| 57 | +not_test: 'not' not_test | comparison |
| 58 | +comparison: expr (comp_op expr)* |
| 59 | +comp_op: '<'|'>'|'='|'>' '='|'<' '='|'<' '>'|'in'|'not' 'in'|'is'|'is' 'not' |
| 60 | +expr: term (('+'|'-') term)* |
| 61 | +term: factor (('*'|'/'|'%') factor)* |
| 62 | +factor: ('+'|'-') factor | atom trailer* |
| 63 | +atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' '}' | '`' testlist '`' | NAME | NUMBER | STRING |
| 64 | +trailer: '(' [exprlist] ')' | '[' subscript ']' | '.' NAME |
| 65 | +subscript: expr | [expr] ':' [expr] |
| 66 | +exprlist: expr (',' expr)* [','] |
| 67 | +testlist: test (',' test)* [','] |
| 68 | + |
| 69 | +classdef: 'class' NAME parameters ['=' baselist] ':' suite |
| 70 | +baselist: atom arguments (',' atom arguments)* |
| 71 | +arguments: '(' [testlist] ')' |
0 commit comments