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