Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 1 | # Grammar for Python |
| 2 | |
| 3 | # Note: Changing the grammar specified in this file will most likely |
| 4 | # require corresponding changes in the parser module |
| 5 | # (../Modules/parsermodule.c). If you can't make the changes to |
| 6 | # that module yourself, please co-ordinate the required changes |
| 7 | # with someone who can; ask around on python-dev for help. Fred |
| 8 | # Drake <fdrake@acm.org> will probably be listening there. |
| 9 | |
| 10 | # NOTE WELL: You should also follow all the steps listed in PEP 306, |
| 11 | # "How to Change Python's Grammar" |
| 12 | |
| 13 | # Commands for Kees Blom's railroad program |
| 14 | #diagram:token NAME |
| 15 | #diagram:token NUMBER |
| 16 | #diagram:token STRING |
| 17 | #diagram:token NEWLINE |
| 18 | #diagram:token ENDMARKER |
| 19 | #diagram:token INDENT |
| 20 | #diagram:output\input python.bla |
| 21 | #diagram:token DEDENT |
| 22 | #diagram:output\textwidth 20.04cm\oddsidemargin 0.0cm\evensidemargin 0.0cm |
| 23 | #diagram:rules |
| 24 | |
| 25 | # Start symbols for the grammar: |
| 26 | # file_input is a module or sequence of commands read from an input file; |
| 27 | # single_input is a single interactive statement; |
| 28 | # eval_input is the input for the eval() and input() functions. |
| 29 | # NB: compound_stmt in single_input is followed by extra NEWLINE! |
| 30 | file_input: (NEWLINE | stmt)* ENDMARKER |
| 31 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 32 | eval_input: testlist NEWLINE* ENDMARKER |
| 33 | |
| 34 | decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 35 | decorators: decorator+ |
| 36 | decorated: decorators (classdef | funcdef) |
| 37 | funcdef: 'def' NAME parameters ['->' test] ':' suite |
| 38 | parameters: '(' [typedargslist] ')' |
| 39 | typedargslist: ((tfpdef ['=' test] ',')* |
| 40 | ('*' [tname] (',' tname ['=' test])* [',' '**' tname] | '**' tname) |
| 41 | | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
| 42 | tname: NAME [':' test] |
| 43 | tfpdef: tname | '(' tfplist ')' |
| 44 | tfplist: tfpdef (',' tfpdef)* [','] |
| 45 | varargslist: ((vfpdef ['=' test] ',')* |
| 46 | ('*' [vname] (',' vname ['=' test])* [',' '**' vname] | '**' vname) |
| 47 | | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
| 48 | vname: NAME |
| 49 | vfpdef: vname | '(' vfplist ')' |
| 50 | vfplist: vfpdef (',' vfpdef)* [','] |
| 51 | |
| 52 | stmt: simple_stmt | compound_stmt |
| 53 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 54 | small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | |
| 55 | import_stmt | global_stmt | exec_stmt | assert_stmt) |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 56 | expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) | |
| 57 | ('=' (yield_expr|testlist_star_expr))*) |
| 58 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 59 | augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 60 | '<<=' | '>>=' | '**=' | '//=') |
| 61 | # For normal assignments, additional restrictions enforced by the interpreter |
| 62 | print_stmt: 'print' ( [ test (',' test)* [','] ] | |
| 63 | '>>' test [ (',' test)+ [','] ] ) |
| 64 | del_stmt: 'del' exprlist |
| 65 | pass_stmt: 'pass' |
| 66 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt |
| 67 | break_stmt: 'break' |
| 68 | continue_stmt: 'continue' |
| 69 | return_stmt: 'return' [testlist] |
| 70 | yield_stmt: yield_expr |
| 71 | raise_stmt: 'raise' [test ['from' test | ',' test [',' test]]] |
| 72 | import_stmt: import_name | import_from |
| 73 | import_name: 'import' dotted_as_names |
| 74 | import_from: ('from' ('.'* dotted_name | '.'+) |
| 75 | 'import' ('*' | '(' import_as_names ')' | import_as_names)) |
| 76 | import_as_name: NAME ['as' NAME] |
| 77 | dotted_as_name: dotted_name ['as' NAME] |
| 78 | import_as_names: import_as_name (',' import_as_name)* [','] |
| 79 | dotted_as_names: dotted_as_name (',' dotted_as_name)* |
| 80 | dotted_name: NAME ('.' NAME)* |
| 81 | global_stmt: ('global' | 'nonlocal') NAME (',' NAME)* |
| 82 | exec_stmt: 'exec' expr ['in' test [',' test]] |
| 83 | assert_stmt: 'assert' test [',' test] |
| 84 | |
| 85 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated |
| 86 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 87 | while_stmt: 'while' test ':' suite ['else' ':' suite] |
| 88 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] |
| 89 | try_stmt: ('try' ':' suite |
| 90 | ((except_clause ':' suite)+ |
| 91 | ['else' ':' suite] |
| 92 | ['finally' ':' suite] | |
| 93 | 'finally' ':' suite)) |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 94 | with_stmt: 'with' with_item (',' with_item)* ':' suite |
| 95 | with_item: test ['as' expr] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 96 | with_var: 'as' expr |
| 97 | # NB compile.c makes sure that the default except clause is last |
| 98 | except_clause: 'except' [test [(',' | 'as') test]] |
| 99 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT |
| 100 | |
| 101 | # Backward compatibility cruft to support: |
| 102 | # [ x for x in lambda: True, lambda: False if x() ] |
| 103 | # even while also allowing: |
| 104 | # lambda x: 5 if x else 2 |
| 105 | # (But not a mix of the two) |
| 106 | testlist_safe: old_test [(',' old_test)+ [',']] |
| 107 | old_test: or_test | old_lambdef |
| 108 | old_lambdef: 'lambda' [varargslist] ':' old_test |
| 109 | |
| 110 | test: or_test ['if' or_test 'else' test] | lambdef |
| 111 | or_test: and_test ('or' and_test)* |
| 112 | and_test: not_test ('and' not_test)* |
| 113 | not_test: 'not' not_test | comparison |
| 114 | comparison: expr (comp_op expr)* |
| 115 | comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 116 | star_expr: '*' expr |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 117 | expr: xor_expr ('|' xor_expr)* |
| 118 | xor_expr: and_expr ('^' and_expr)* |
| 119 | and_expr: shift_expr ('&' shift_expr)* |
| 120 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 121 | arith_expr: term (('+'|'-') term)* |
| 122 | term: factor (('*'|'/'|'%'|'//') factor)* |
| 123 | factor: ('+'|'-'|'~') factor | power |
| 124 | power: atom trailer* ['**' factor] |
| 125 | atom: ('(' [yield_expr|testlist_gexp] ')' | |
| 126 | '[' [listmaker] ']' | |
| 127 | '{' [dictsetmaker] '}' | |
| 128 | '`' testlist1 '`' | |
| 129 | NAME | NUMBER | STRING+ | '.' '.' '.') |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 130 | listmaker: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) |
| 131 | testlist_gexp: test ( comp_for | (',' (test|star_expr))* [','] ) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 132 | lambdef: 'lambda' [varargslist] ':' test |
| 133 | trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
| 134 | subscriptlist: subscript (',' subscript)* [','] |
| 135 | subscript: test | [test] ':' [test] [sliceop] |
| 136 | sliceop: ':' [test] |
Benjamin Peterson | d9af52b | 2009-11-02 18:16:28 +0000 | [diff] [blame] | 137 | exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 138 | testlist: test (',' test)* [','] |
| 139 | dictsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) | |
| 140 | (test (comp_for | (',' test)* [','])) ) |
| 141 | |
| 142 | classdef: 'class' NAME ['(' [arglist] ')'] ':' suite |
| 143 | |
Benjamin Peterson | 3a2fb14 | 2008-09-13 18:37:09 +0000 | [diff] [blame] | 144 | arglist: (argument ',')* (argument [','] |
| 145 | |'*' test (',' argument)* [',' '**' test] |
| 146 | |'**' test) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 147 | argument: test [comp_for] | test '=' test # Really [keyword '='] test |
| 148 | |
| 149 | comp_iter: comp_for | comp_if |
| 150 | comp_for: 'for' exprlist 'in' testlist_safe [comp_iter] |
| 151 | comp_if: 'if' old_test [comp_iter] |
| 152 | |
| 153 | testlist1: test (',' test)* |
| 154 | |
| 155 | # not used in grammar, but may appear in "node" passed from Parser to Compiler |
| 156 | encoding_decl: NAME |
| 157 | |
| 158 | yield_expr: 'yield' [testlist] |