Grammar.txt 6.4 KB

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