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 # single_input is a single interactive statement; 27 # file_input is a module or sequence of commands read from an input file; 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 single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE 31 file_input: (NEWLINE | stmt)* ENDMARKER 32 eval_input: testlist NEWLINE* ENDMARKER 33 34 decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE 35 decorators: decorator+ 36 funcdef: [decorators] 'def' NAME parameters ':' suite 37 parameters: '(' [varargslist] ')' 38 varargslist: ((fpdef ['=' test] ',')* 39 ('*' NAME [',' '**' NAME] | '**' NAME) | 40 fpdef ['=' test] (',' fpdef ['=' test])* [',']) 41 fpdef: NAME | '(' fplist ')' 42 fplist: fpdef (',' fpdef)* [','] 43 44 stmt: simple_stmt | compound_stmt 45 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE 46 small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | 47 import_stmt | global_stmt | exec_stmt | assert_stmt) 48 expr_stmt: testlist (augassign (yield_expr|testlist) | 49 ('=' (yield_expr|testlist))*) 50 augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | 51 '<<=' | '>>=' | '**=' | '//=') 52 # For normal assignments, additional restrictions enforced by the interpreter 53 print_stmt: 'print' ( [ test (',' test)* [','] ] | 54 '>>' test [ (',' test)+ [','] ] ) 55 del_stmt: 'del' exprlist 56 pass_stmt: 'pass' 57 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt 58 break_stmt: 'break' 59 continue_stmt: 'continue' 60 return_stmt: 'return' [testlist] 61 yield_stmt: yield_expr 62 raise_stmt: 'raise' [test [',' test [',' test]]] 63 import_stmt: import_name | import_from 64 import_name: 'import' dotted_as_names 65 import_from: ('from' ('.'* dotted_name | '.'+) 66 'import' ('*' | '(' import_as_names ')' | import_as_names)) 67 import_as_name: NAME [('as' | NAME) NAME] 68 dotted_as_name: dotted_name [('as' | NAME) NAME] 69 import_as_names: import_as_name (',' import_as_name)* [','] 70 dotted_as_names: dotted_as_name (',' dotted_as_name)* 71 dotted_name: NAME ('.' NAME)* 72 global_stmt: 'global' NAME (',' NAME)* 73 exec_stmt: 'exec' expr ['in' test [',' test]] 74 assert_stmt: 'assert' test [',' test] 75 76 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef 77 if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] 78 while_stmt: 'while' test ':' suite ['else' ':' suite] 79 for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] 80 try_stmt: ('try' ':' suite 81 ((except_clause ':' suite)+ 82 ['else' ':' suite] 83 ['finally' ':' suite] | 84 'finally' ':' suite)) 85 with_stmt: 'with' test [ with_var ] ':' suite 86 with_var: ('as' | NAME) expr 87 # NB compile.c makes sure that the default except clause is last 88 except_clause: 'except' [test [',' test]] 89 suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT 90 91 # Backward compatibility cruft to support: 92 # [ x for x in lambda: True, lambda: False if x() ] 93 # even while also allowing: 94 # lambda x: 5 if x else 2 95 # (But not a mix of the two) 96 testlist_safe: old_test [(',' old_test)+ [',']] 97 old_test: or_test | old_lambdef 98 old_lambdef: 'lambda' [varargslist] ':' old_test 99 100 test: or_test ['if' or_test 'else' test] | lambdef 101 or_test: and_test ('or' and_test)* 102 and_test: not_test ('and' not_test)* 103 not_test: 'not' not_test | comparison 104 comparison: expr (comp_op expr)* 105 comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' 106 expr: xor_expr ('|' xor_expr)* 107 xor_expr: and_expr ('^' and_expr)* 108 and_expr: shift_expr ('&' shift_expr)* 109 shift_expr: arith_expr (('<<'|'>>') arith_expr)* 110 arith_expr: term (('+'|'-') term)* 111 term: factor (('*'|'/'|'%'|'//') factor)* 112 factor: ('+'|'-'|'~') factor | power 113 power: atom trailer* ['**' factor] 114 atom: ('(' [yield_expr|testlist_gexp] ')' | 115 '[' [listmaker] ']' | 116 '{' [dictmaker] '}' | 117 '`' testlist1 '`' | 118 NAME | NUMBER | STRING+) 119 listmaker: test ( list_for | (',' test)* [','] ) 120 testlist_gexp: test ( gen_for | (',' test)* [','] ) 121 lambdef: 'lambda' [varargslist] ':' test 122 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME 123 subscriptlist: subscript (',' subscript)* [','] 124 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] 125 sliceop: ':' [test] 126 exprlist: expr (',' expr)* [','] 127 testlist: test (',' test)* [','] 128 dictmaker: test ':' test (',' test ':' test)* [','] 129 130 classdef: 'class' NAME ['(' [testlist] ')'] ':' suite 131 132 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test) 133 argument: test [gen_for] | test '=' test # Really [keyword '='] test 134 135 list_iter: list_for | list_if 136 list_for: 'for' exprlist 'in' testlist_safe [list_iter] 137 list_if: 'if' old_test [list_iter] 138 139 gen_iter: gen_for | gen_if 140 gen_for: 'for' exprlist 'in' or_test [gen_iter] 141 gen_if: 'if' old_test [gen_iter] 142 143 testlist1: test (',' test)* 144 145 # not used in grammar, but may appear in "node" passed from Parser to Compiler 146 encoding_decl: NAME 147 148 yield_expr: 'yield' [testlist]