Guido van Rossum | 68fc349 | 1991-12-10 13:51:08 +0000 | [diff] [blame^] | 1 | # 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 Rossum | 9eb4f53 | 1991-10-24 14:54:25 +0000 | [diff] [blame] | 6 | |
| 7 | # Changes since version 6: |
| 8 | # Add logical operators '|', '^', '&' and '~' |
| 9 | # Add shift operators '<<' and '>>' |
Guido van Rossum | a76fb5b | 1991-10-20 20:10:09 +0000 | [diff] [blame] | 10 | |
| 11 | # Changes since version 5: |
| 12 | # Comparison operators '<=' '>' '<>' are now 1 token |
| 13 | # Also support '!=' and '==' as alternatives for '<>' and '=' |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 14 | |
| 15 | # Changes compared to version 4: |
Guido van Rossum | 7ac4a88 | 1991-07-27 21:29:47 +0000 | [diff] [blame] | 16 | # 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 Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 19 | # Semicolons can separate small statements |
| 20 | # 'continue' statement |
| 21 | # Dictionary constructors: {key:value, key:value, ...} |
| 22 | # More tests instead of exprs |
Guido van Rossum | 4dae216 | 1991-01-21 15:07:21 +0000 | [diff] [blame] | 23 | |
| 24 | # Changes compared to version 3: |
| 25 | # Removed 'dir' statement. |
| 26 | # Function call argument is a testlist instead of exprlist. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 27 | |
| 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | |
| 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! |
| 47 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 48 | file_input: (NEWLINE | stmt)* ENDMARKER |
| 49 | expr_input: testlist NEWLINE |
| 50 | eval_input: testlist ENDMARKER |
| 51 | |
| 52 | funcdef: 'def' NAME parameters ':' suite |
| 53 | parameters: '(' [fplist] ')' |
| 54 | fplist: fpdef (',' fpdef)* |
| 55 | fpdef: NAME | '(' fplist ')' |
| 56 | |
| 57 | stmt: simple_stmt | compound_stmt |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 58 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
Guido van Rossum | 68fc349 | 1991-12-10 13:51:08 +0000 | [diff] [blame^] | 59 | small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 60 | expr_stmt: (exprlist '=')* exprlist |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | # For assignments, additional restrictions enforced by the interpreter |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 62 | print_stmt: 'print' (test ',')* [test] |
| 63 | del_stmt: 'del' exprlist |
| 64 | pass_stmt: 'pass' |
| 65 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 66 | break_stmt: 'break' |
| 67 | continue_stmt: 'continue' |
| 68 | return_stmt: 'return' [testlist] |
| 69 | raise_stmt: 'raise' test [',' test] |
| 70 | import_stmt: 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*) |
Guido van Rossum | 68fc349 | 1991-12-10 13:51:08 +0000 | [diff] [blame^] | 71 | global_stmt: 'global' NAME (',' NAME)* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 73 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 74 | while_stmt: 'while' test ':' suite ['else' ':' suite] |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 75 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | try_stmt: 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite] |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 77 | except_clause: 'except' [test [',' test]] |
Guido van Rossum | 7ac4a88 | 1991-07-27 21:29:47 +0000 | [diff] [blame] | 78 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | |
| 80 | test: and_test ('or' and_test)* |
| 81 | and_test: not_test ('and' not_test)* |
| 82 | not_test: 'not' not_test | comparison |
| 83 | comparison: expr (comp_op expr)* |
Guido van Rossum | a76fb5b | 1991-10-20 20:10:09 +0000 | [diff] [blame] | 84 | comp_op: '<'|'>'|'='|'>='|'<='|'<>'|'!='|'=='|'in'|'not' 'in'|'is'|'is' 'not' |
Guido van Rossum | 9eb4f53 | 1991-10-24 14:54:25 +0000 | [diff] [blame] | 85 | expr: xor_expr ('|' xor_expr)* |
| 86 | xor_expr: and_expr ('^' and_expr)* |
| 87 | and_expr: shift_expr ('&' shift_expr)* |
| 88 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 89 | arith_expr: term (('+'|'-') term)* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | term: factor (('*'|'/'|'%') factor)* |
Guido van Rossum | 9eb4f53 | 1991-10-24 14:54:25 +0000 | [diff] [blame] | 91 | factor: ('+'|'-'|'~') factor | atom trailer* |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 92 | atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING |
Guido van Rossum | 4dae216 | 1991-01-21 15:07:21 +0000 | [diff] [blame] | 93 | trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 94 | subscript: test | [test] ':' [test] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | exprlist: expr (',' expr)* [','] |
| 96 | testlist: test (',' test)* [','] |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 97 | dictmaker: test ':' test (',' test ':' test)* [','] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 68fc349 | 1991-12-10 13:51:08 +0000 | [diff] [blame^] | 99 | # New class syntax should be: |
| 100 | # classdef: class NAME ['(' testlist ')'] ':' suite |
| 101 | # but merged with old syntax for compatibility it becomes: |
| 102 | classdef: 'class' NAME ['(' testlist ')' |'(' ')' ['=' baselist]] ':' suite |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | baselist: atom arguments (',' atom arguments)* |
Guido van Rossum | 68fc349 | 1991-12-10 13:51:08 +0000 | [diff] [blame^] | 104 | arguments: '(' ')' |