Guido van Rossum | 526e909 | 1992-01-14 18:27:17 +0000 | [diff] [blame] | 1 | # Grammar for Python |
Guido van Rossum | 6cf1273 | 1991-12-31 13:11:56 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 526e909 | 1992-01-14 18:27:17 +0000 | [diff] [blame] | 3 | # Change log: |
| 4 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame^] | 5 | # 19-May-93: |
| 6 | # Add access statement |
| 7 | |
| 8 | # 18-May-93: |
| 9 | # Abolish old class header syntax |
| 10 | |
Guido van Rossum | cf49ac5 | 1992-04-06 14:39:51 +0000 | [diff] [blame] | 11 | # 06-Apr-92: |
| 12 | # Use only '*' for varargs list |
| 13 | |
| 14 | # 31-Mar-92: |
| 15 | # Tighten syntax for try statements |
| 16 | |
Guido van Rossum | e785fbc | 1992-03-04 16:41:24 +0000 | [diff] [blame] | 17 | # 27-Feb-92: |
| 18 | # Allow NEWLINE* after eval input |
| 19 | |
Guido van Rossum | 610cdc5 | 1992-01-26 18:17:23 +0000 | [diff] [blame] | 20 | # 16-Jan-92: |
| 21 | # Added '*' as alternative for '+' in varargs syntax |
| 22 | # (Not sure which alternative is better yet.) |
| 23 | |
Guido van Rossum | 526e909 | 1992-01-14 18:27:17 +0000 | [diff] [blame] | 24 | # 11-Jan-92: |
| 25 | # Variable length argument list syntax added: def f(a, b, +rest): ... |
| 26 | |
| 27 | # 8-Jan-92: |
| 28 | # Allow only '==' for equality testing |
Guido van Rossum | 6cf1273 | 1991-12-31 13:11:56 +0000 | [diff] [blame] | 29 | |
| 30 | # Changes since version 8: |
| 31 | # Trailing commas in formal parameter lists are allowed |
Guido van Rossum | 68fc349 | 1991-12-10 13:51:08 +0000 | [diff] [blame] | 32 | |
| 33 | # Changes since version 7: |
| 34 | # New syntax to specify base classes (but old syntax retained for now) |
| 35 | # 'global' statement (valid only in functions but not enforced here) |
Guido van Rossum | 9eb4f53 | 1991-10-24 14:54:25 +0000 | [diff] [blame] | 36 | |
| 37 | # Changes since version 6: |
| 38 | # Add logical operators '|', '^', '&' and '~' |
| 39 | # Add shift operators '<<' and '>>' |
Guido van Rossum | a76fb5b | 1991-10-20 20:10:09 +0000 | [diff] [blame] | 40 | |
| 41 | # Changes since version 5: |
| 42 | # Comparison operators '<=' '>' '<>' are now 1 token |
| 43 | # Also support '!=' and '==' as alternatives for '<>' and '=' |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 44 | |
| 45 | # Changes compared to version 4: |
Guido van Rossum | 7ac4a88 | 1991-07-27 21:29:47 +0000 | [diff] [blame] | 46 | # Blank lines and lines only containing a comment are now eaten |
| 47 | # by the lexer, so the NEWLINE* things in suite are gone |
| 48 | # (but the 2nd NEWLINE terminating single_input stays!) |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 49 | # Semicolons can separate small statements |
| 50 | # 'continue' statement |
| 51 | # Dictionary constructors: {key:value, key:value, ...} |
| 52 | # More tests instead of exprs |
Guido van Rossum | 4dae216 | 1991-01-21 15:07:21 +0000 | [diff] [blame] | 53 | |
| 54 | # Changes compared to version 3: |
| 55 | # Removed 'dir' statement. |
| 56 | # Function call argument is a testlist instead of exprlist. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | |
| 58 | # Changes compared to version 2: |
| 59 | # The syntax of Boolean operations is changed to use more |
| 60 | # conventional priorities: or < and < not. |
| 61 | |
| 62 | # Changes compared to version 1: |
| 63 | # modules and scripts are unified; |
| 64 | # 'quit' is gone (use ^D); |
| 65 | # empty_stmt is gone, replaced by explicit NEWLINE where appropriate; |
| 66 | # 'import' and 'def' aren't special any more; |
| 67 | # added 'from' NAME option on import clause, and '*' to import all; |
| 68 | # added class definition. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 69 | |
| 70 | # Start symbols for the grammar: |
| 71 | # single_input is a single interactive statement; |
| 72 | # file_input is a module or sequence of commands read from an input file; |
| 73 | # expr_input is the input for the input() function; |
| 74 | # eval_input is the input for the eval() function. |
| 75 | |
| 76 | # NB: compound_stmt in single_input is followed by extra NEWLINE! |
| 77 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 78 | file_input: (NEWLINE | stmt)* ENDMARKER |
| 79 | expr_input: testlist NEWLINE |
Guido van Rossum | e785fbc | 1992-03-04 16:41:24 +0000 | [diff] [blame] | 80 | eval_input: testlist NEWLINE* ENDMARKER |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 81 | |
| 82 | funcdef: 'def' NAME parameters ':' suite |
Guido van Rossum | 526e909 | 1992-01-14 18:27:17 +0000 | [diff] [blame] | 83 | parameters: '(' [varargslist] ')' |
Guido van Rossum | 02334d2 | 1992-04-06 12:36:19 +0000 | [diff] [blame] | 84 | varargslist: (fpdef ',')* '*' NAME | fpdef (',' fpdef)* [','] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | fpdef: NAME | '(' fplist ')' |
Guido van Rossum | 526e909 | 1992-01-14 18:27:17 +0000 | [diff] [blame] | 86 | fplist: fpdef (',' fpdef)* [','] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | |
| 88 | stmt: simple_stmt | compound_stmt |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 89 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame^] | 90 | small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | access_stmt |
Guido van Rossum | 09cea47 | 1992-01-01 14:51:57 +0000 | [diff] [blame] | 91 | expr_stmt: (exprlist '=')* exprlist |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | # For assignments, additional restrictions enforced by the interpreter |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 93 | print_stmt: 'print' (test ',')* [test] |
| 94 | del_stmt: 'del' exprlist |
| 95 | pass_stmt: 'pass' |
| 96 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 97 | break_stmt: 'break' |
| 98 | continue_stmt: 'continue' |
| 99 | return_stmt: 'return' [testlist] |
| 100 | raise_stmt: 'raise' test [',' test] |
| 101 | import_stmt: 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*) |
Guido van Rossum | 68fc349 | 1991-12-10 13:51:08 +0000 | [diff] [blame] | 102 | global_stmt: 'global' NAME (',' NAME)* |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame^] | 103 | access_stmt: 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)* |
| 104 | accesstype: NAME+ |
| 105 | # accesstype should be ('public' | 'protected' | 'private') ['read'] ['write'] |
| 106 | # but can't be because that would create undesirable reserved words! |
| 107 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 109 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 110 | while_stmt: 'while' test ':' suite ['else' ':' suite] |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 111 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] |
Guido van Rossum | af82141 | 1992-03-31 18:49:18 +0000 | [diff] [blame] | 112 | try_stmt: 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' ':' suite |
| 113 | # NB compile.c makes sure that the default except clause is last |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 114 | except_clause: 'except' [test [',' test]] |
Guido van Rossum | 7ac4a88 | 1991-07-27 21:29:47 +0000 | [diff] [blame] | 115 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | |
| 117 | test: and_test ('or' and_test)* |
| 118 | and_test: not_test ('and' not_test)* |
| 119 | not_test: 'not' not_test | comparison |
| 120 | comparison: expr (comp_op expr)* |
Guido van Rossum | 6cf1273 | 1991-12-31 13:11:56 +0000 | [diff] [blame] | 121 | comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
Guido van Rossum | 9eb4f53 | 1991-10-24 14:54:25 +0000 | [diff] [blame] | 122 | expr: xor_expr ('|' xor_expr)* |
| 123 | xor_expr: and_expr ('^' and_expr)* |
| 124 | and_expr: shift_expr ('&' shift_expr)* |
| 125 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 126 | arith_expr: term (('+'|'-') term)* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 127 | term: factor (('*'|'/'|'%') factor)* |
Guido van Rossum | 9eb4f53 | 1991-10-24 14:54:25 +0000 | [diff] [blame] | 128 | factor: ('+'|'-'|'~') factor | atom trailer* |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 129 | atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING |
Guido van Rossum | 4dae216 | 1991-01-21 15:07:21 +0000 | [diff] [blame] | 130 | trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 131 | subscript: test | [test] ':' [test] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | exprlist: expr (',' expr)* [','] |
| 133 | testlist: test (',' test)* [','] |
Guido van Rossum | 56f7837 | 1991-07-17 18:39:15 +0000 | [diff] [blame] | 134 | dictmaker: test ':' test (',' test ':' test)* [','] |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame^] | 136 | classdef: class NAME ['(' testlist ')'] ':' suite |