1# Grammar for 2to3. This grammar supports Python 2.x and 3.x. 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! 30file_input: (NEWLINE | stmt)* ENDMARKER 31single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE 32eval_input: testlist NEWLINE* ENDMARKER 33 34decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE 35decorators: decorator+ 36decorated: decorators (classdef | funcdef | async_funcdef) 37async_funcdef: ASYNC funcdef 38funcdef: 'def' NAME parameters ['->' test] ':' suite 39parameters: '(' [typedargslist] ')' 40typedargslist: ((tfpdef ['=' test] ',')* 41 ('*' [tname] (',' tname ['=' test])* [',' '**' tname] | '**' tname) 42 | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) 43tname: NAME [':' test] 44tfpdef: tname | '(' tfplist ')' 45tfplist: tfpdef (',' tfpdef)* [','] 46varargslist: ((vfpdef ['=' test] ',')* 47 ('*' [vname] (',' vname ['=' test])* [',' '**' vname] | '**' vname) 48 | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) 49vname: NAME 50vfpdef: vname | '(' vfplist ')' 51vfplist: vfpdef (',' vfpdef)* [','] 52 53stmt: simple_stmt | compound_stmt 54simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE 55small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | 56 import_stmt | global_stmt | exec_stmt | assert_stmt) 57expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | 58 ('=' (yield_expr|testlist_star_expr))*) 59annassign: ':' test ['=' test] 60testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] 61augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | 62 '<<=' | '>>=' | '**=' | '//=') 63# For normal and annotated assignments, additional restrictions enforced by the interpreter 64print_stmt: 'print' ( [ test (',' test)* [','] ] | 65 '>>' test [ (',' test)+ [','] ] ) 66del_stmt: 'del' exprlist 67pass_stmt: 'pass' 68flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt 69break_stmt: 'break' 70continue_stmt: 'continue' 71return_stmt: 'return' [testlist] 72yield_stmt: yield_expr 73raise_stmt: 'raise' [test ['from' test | ',' test [',' test]]] 74import_stmt: import_name | import_from 75import_name: 'import' dotted_as_names 76import_from: ('from' ('.'* dotted_name | '.'+) 77 'import' ('*' | '(' import_as_names ')' | import_as_names)) 78import_as_name: NAME ['as' NAME] 79dotted_as_name: dotted_name ['as' NAME] 80import_as_names: import_as_name (',' import_as_name)* [','] 81dotted_as_names: dotted_as_name (',' dotted_as_name)* 82dotted_name: NAME ('.' NAME)* 83global_stmt: ('global' | 'nonlocal') NAME (',' NAME)* 84exec_stmt: 'exec' expr ['in' test [',' test]] 85assert_stmt: 'assert' test [',' test] 86 87compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt 88async_stmt: ASYNC (funcdef | with_stmt | for_stmt) 89if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] 90while_stmt: 'while' test ':' suite ['else' ':' suite] 91for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] 92try_stmt: ('try' ':' suite 93 ((except_clause ':' suite)+ 94 ['else' ':' suite] 95 ['finally' ':' suite] | 96 'finally' ':' suite)) 97with_stmt: 'with' with_item (',' with_item)* ':' suite 98with_item: test ['as' expr] 99with_var: 'as' expr 100# NB compile.c makes sure that the default except clause is last 101except_clause: 'except' [test [(',' | 'as') test]] 102suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT 103 104# Backward compatibility cruft to support: 105# [ x for x in lambda: True, lambda: False if x() ] 106# even while also allowing: 107# lambda x: 5 if x else 2 108# (But not a mix of the two) 109testlist_safe: old_test [(',' old_test)+ [',']] 110old_test: or_test | old_lambdef 111old_lambdef: 'lambda' [varargslist] ':' old_test 112 113test: or_test ['if' or_test 'else' test] | lambdef 114or_test: and_test ('or' and_test)* 115and_test: not_test ('and' not_test)* 116not_test: 'not' not_test | comparison 117comparison: expr (comp_op expr)* 118comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' 119star_expr: '*' expr 120expr: xor_expr ('|' xor_expr)* 121xor_expr: and_expr ('^' and_expr)* 122and_expr: shift_expr ('&' shift_expr)* 123shift_expr: arith_expr (('<<'|'>>') arith_expr)* 124arith_expr: term (('+'|'-') term)* 125term: factor (('*'|'@'|'/'|'%'|'//') factor)* 126factor: ('+'|'-'|'~') factor | power 127power: [AWAIT] atom trailer* ['**' factor] 128atom: ('(' [yield_expr|testlist_gexp] ')' | 129 '[' [listmaker] ']' | 130 '{' [dictsetmaker] '}' | 131 '`' testlist1 '`' | 132 NAME | NUMBER | STRING+ | '.' '.' '.') 133listmaker: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) 134testlist_gexp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) 135lambdef: 'lambda' [varargslist] ':' test 136trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME 137subscriptlist: subscript (',' subscript)* [','] 138subscript: test | [test] ':' [test] [sliceop] 139sliceop: ':' [test] 140exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] 141testlist: test (',' test)* [','] 142dictsetmaker: ( ((test ':' test | '**' expr) 143 (comp_for | (',' (test ':' test | '**' expr))* [','])) | 144 ((test | star_expr) 145 (comp_for | (',' (test | star_expr))* [','])) ) 146 147classdef: 'class' NAME ['(' [arglist] ')'] ':' suite 148 149arglist: argument (',' argument)* [','] 150 151# "test '=' test" is really "keyword '=' test", but we have no such token. 152# These need to be in a single rule to avoid grammar that is ambiguous 153# to our LL(1) parser. Even though 'test' includes '*expr' in star_expr, 154# we explicitly match '*' here, too, to give it proper precedence. 155# Illegal combinations and orderings are blocked in ast.c: 156# multiple (test comp_for) arguments are blocked; keyword unpackings 157# that precede iterable unpackings are blocked; etc. 158argument: ( test [comp_for] | 159 test '=' test | 160 '**' expr | 161 star_expr ) 162 163comp_iter: comp_for | comp_if 164comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter] 165comp_if: 'if' old_test [comp_iter] 166 167testlist1: test (',' test)* 168 169# not used in grammar, but may appear in "node" passed from Parser to Compiler 170encoding_decl: NAME 171 172yield_expr: 'yield' [yield_arg] 173yield_arg: 'from' test | testlist 174