1# Grammar for Python 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# single_input is a single interactive statement; 8# file_input is a module or sequence of commands read from an input file; 9# eval_input is the input for the eval() functions. 10# NB: compound_stmt in single_input is followed by extra NEWLINE! 11single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE 12file_input: (NEWLINE | stmt)* ENDMARKER 13eval_input: testlist NEWLINE* ENDMARKER 14 15decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE 16decorators: decorator+ 17decorated: decorators (classdef | funcdef | async_funcdef) 18 19async_funcdef: 'async' funcdef 20funcdef: 'def' NAME parameters ['->' test] ':' suite 21 22parameters: '(' [typedargslist] ')' 23typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ 24 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] 25 | '**' tfpdef [',']]] 26 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] 27 | '**' tfpdef [',']) 28tfpdef: NAME [':' test] 29varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ 30 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] 31 | '**' vfpdef [',']]] 32 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] 33 | '**' vfpdef [','] 34) 35vfpdef: NAME 36 37stmt: simple_stmt | compound_stmt 38simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE 39small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt | 40 import_stmt | global_stmt | nonlocal_stmt | assert_stmt) 41expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | 42 ('=' (yield_expr|testlist_star_expr))*) 43annassign: ':' test ['=' test] 44testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] 45augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | 46 '<<=' | '>>=' | '**=' | '//=') 47# For normal and annotated assignments, additional restrictions enforced by the interpreter 48del_stmt: 'del' exprlist 49pass_stmt: 'pass' 50flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt 51break_stmt: 'break' 52continue_stmt: 'continue' 53return_stmt: 'return' [testlist] 54yield_stmt: yield_expr 55raise_stmt: 'raise' [test ['from' test]] 56import_stmt: import_name | import_from 57import_name: 'import' dotted_as_names 58# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS 59import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+) 60 'import' ('*' | '(' import_as_names ')' | import_as_names)) 61import_as_name: NAME ['as' NAME] 62dotted_as_name: dotted_name ['as' NAME] 63import_as_names: import_as_name (',' import_as_name)* [','] 64dotted_as_names: dotted_as_name (',' dotted_as_name)* 65dotted_name: NAME ('.' NAME)* 66global_stmt: 'global' NAME (',' NAME)* 67nonlocal_stmt: 'nonlocal' NAME (',' NAME)* 68assert_stmt: 'assert' test [',' test] 69 70compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt 71async_stmt: 'async' (funcdef | with_stmt | for_stmt) 72if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] 73while_stmt: 'while' test ':' suite ['else' ':' suite] 74for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] 75try_stmt: ('try' ':' suite 76 ((except_clause ':' suite)+ 77 ['else' ':' suite] 78 ['finally' ':' suite] | 79 'finally' ':' suite)) 80with_stmt: 'with' with_item (',' with_item)* ':' suite 81with_item: test ['as' expr] 82# NB compile.c makes sure that the default except clause is last 83except_clause: 'except' [test ['as' NAME]] 84suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT 85 86test: or_test ['if' or_test 'else' test] | lambdef 87test_nocond: or_test | lambdef_nocond 88lambdef: 'lambda' [varargslist] ':' test 89lambdef_nocond: 'lambda' [varargslist] ':' test_nocond 90or_test: and_test ('or' and_test)* 91and_test: not_test ('and' not_test)* 92not_test: 'not' not_test | comparison 93comparison: expr (comp_op expr)* 94# <> isn't actually a valid comparison operator in Python. It's here for the 95# sake of a __future__ import described in PEP 401 (which really works :-) 96comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' 97star_expr: '*' expr 98expr: xor_expr ('|' xor_expr)* 99xor_expr: and_expr ('^' and_expr)* 100and_expr: shift_expr ('&' shift_expr)* 101shift_expr: arith_expr (('<<'|'>>') arith_expr)* 102arith_expr: term (('+'|'-') term)* 103term: factor (('*'|'@'|'/'|'%'|'//') factor)* 104factor: ('+'|'-'|'~') factor | power 105power: atom_expr ['**' factor] 106atom_expr: ['await'] atom trailer* 107atom: ('(' [yield_expr|testlist_comp] ')' | 108 '[' [testlist_comp] ']' | 109 '{' [dictorsetmaker] '}' | 110 NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False') 111testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) 112trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME 113subscriptlist: subscript (',' subscript)* [','] 114subscript: test | [test] ':' [test] [sliceop] 115sliceop: ':' [test] 116exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] 117testlist: test (',' test)* [','] 118dictorsetmaker: ( ((test ':' test | '**' expr) 119 (comp_for | (',' (test ':' test | '**' expr))* [','])) | 120 ((test | star_expr) 121 (comp_for | (',' (test | star_expr))* [','])) ) 122 123classdef: 'class' NAME ['(' [arglist] ')'] ':' suite 124 125arglist: argument (',' argument)* [','] 126 127# The reason that keywords are test nodes instead of NAME is that using NAME 128# results in an ambiguity. ast.c makes sure it's a NAME. 129# "test '=' test" is really "keyword '=' test", but we have no such token. 130# These need to be in a single rule to avoid grammar that is ambiguous 131# to our LL(1) parser. Even though 'test' includes '*expr' in star_expr, 132# we explicitly match '*' here, too, to give it proper precedence. 133# Illegal combinations and orderings are blocked in ast.c: 134# multiple (test comp_for) arguments are blocked; keyword unpackings 135# that precede iterable unpackings are blocked; etc. 136argument: ( test [comp_for] | 137 test '=' test | 138 '**' test | 139 '*' test ) 140 141comp_iter: comp_for | comp_if 142sync_comp_for: 'for' exprlist 'in' or_test [comp_iter] 143comp_for: ['async'] sync_comp_for 144comp_if: 'if' test_nocond [comp_iter] 145 146# not used in grammar, but may appear in "node" passed from Parser to Compiler 147encoding_decl: NAME 148 149yield_expr: 'yield' [yield_arg] 150yield_arg: 'from' test | testlist 151