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