Grammar.txt 8.5 KB

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