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