10. 完整的语法规范

这是完整的Python语法,它被送入解析器生成器,以生成解析Python源文件的解析器:

  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!
  11. single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
  12. file_input: (NEWLINE | stmt)* ENDMARKER
  13. eval_input: testlist NEWLINE* ENDMARKER
  14.  
  15. decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
  16. decorators: decorator+
  17. decorated: decorators (classdef | funcdef | async_funcdef)
  18.  
  19. async_funcdef: 'async' funcdef
  20. funcdef: 'def' NAME parameters ['->' test] ':' suite
  21.  
  22. parameters: '(' [typedargslist] ')'
  23. typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
  24. '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
  25. | '**' tfpdef [',']]]
  26. | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
  27. | '**' tfpdef [','])
  28. tfpdef: NAME [':' test]
  29. varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
  30. '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
  31. | '**' vfpdef [',']]]
  32. | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
  33. | '**' vfpdef [',']
  34. )
  35. vfpdef: NAME
  36.  
  37. stmt: simple_stmt | compound_stmt
  38. simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
  39. small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
  40. import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
  41. expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
  42. ('=' (yield_expr|testlist_star_expr))*)
  43. annassign: ':' test ['=' test]
  44. testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
  45. augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
  46. '<<=' | '>>=' | '**=' | '//=')
  47. # For normal and annotated assignments, additional restrictions enforced by the interpreter
  48. del_stmt: 'del' exprlist
  49. pass_stmt: 'pass'
  50. flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
  51. break_stmt: 'break'
  52. continue_stmt: 'continue'
  53. return_stmt: 'return' [testlist]
  54. yield_stmt: yield_expr
  55. raise_stmt: 'raise' [test ['from' test]]
  56. import_stmt: import_name | import_from
  57. import_name: 'import' dotted_as_names
  58. # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
  59. import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
  60. 'import' ('*' | '(' import_as_names ')' | import_as_names))
  61. import_as_name: NAME ['as' NAME]
  62. dotted_as_name: dotted_name ['as' NAME]
  63. import_as_names: import_as_name (',' import_as_name)* [',']
  64. dotted_as_names: dotted_as_name (',' dotted_as_name)*
  65. dotted_name: NAME ('.' NAME)*
  66. global_stmt: 'global' NAME (',' NAME)*
  67. nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
  68. assert_stmt: 'assert' test [',' test]
  69.  
  70. compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
  71. async_stmt: 'async' (funcdef | with_stmt | for_stmt)
  72. if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
  73. while_stmt: 'while' test ':' suite ['else' ':' suite]
  74. for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
  75. try_stmt: ('try' ':' suite
  76. ((except_clause ':' suite)+
  77. ['else' ':' suite]
  78. ['finally' ':' suite] |
  79. 'finally' ':' suite))
  80. with_stmt: 'with' with_item (',' with_item)* ':' suite
  81. with_item: test ['as' expr]
  82. # NB compile.c makes sure that the default except clause is last
  83. except_clause: 'except' [test ['as' NAME]]
  84. suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
  85.  
  86. test: or_test ['if' or_test 'else' test] | lambdef
  87. test_nocond: or_test | lambdef_nocond
  88. lambdef: 'lambda' [varargslist] ':' test
  89. lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
  90. or_test: and_test ('or' and_test)*
  91. and_test: not_test ('and' not_test)*
  92. not_test: 'not' not_test | comparison
  93. comparison: 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 :-)
  96. comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
  97. star_expr: '*' expr
  98. expr: xor_expr ('|' xor_expr)*
  99. xor_expr: and_expr ('^' and_expr)*
  100. and_expr: shift_expr ('&' shift_expr)*
  101. shift_expr: arith_expr (('<<'|'>>') arith_expr)*
  102. arith_expr: term (('+'|'-') term)*
  103. term: factor (('*'|'@'|'/'|'%'|'//') factor)*
  104. factor: ('+'|'-'|'~') factor | power
  105. power: atom_expr ['**' factor]
  106. atom_expr: ['await'] atom trailer*
  107. atom: ('(' [yield_expr|testlist_comp] ')' |
  108. '[' [testlist_comp] ']' |
  109. '{' [dictorsetmaker] '}' |
  110. NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
  111. testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
  112. trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
  113. subscriptlist: subscript (',' subscript)* [',']
  114. subscript: test | [test] ':' [test] [sliceop]
  115. sliceop: ':' [test]
  116. exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
  117. testlist: test (',' test)* [',']
  118. dictorsetmaker: ( ((test ':' test | '**' expr)
  119. (comp_for | (',' (test ':' test | '**' expr))* [','])) |
  120. ((test | star_expr)
  121. (comp_for | (',' (test | star_expr))* [','])) )
  122.  
  123. classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
  124.  
  125. arglist: 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.
  136. argument: ( test [comp_for] |
  137. test '=' test |
  138. '**' test |
  139. '*' test )
  140.  
  141. comp_iter: comp_for | comp_if
  142. sync_comp_for: 'for' exprlist 'in' or_test [comp_iter]
  143. comp_for: ['async'] sync_comp_for
  144. comp_if: 'if' test_nocond [comp_iter]
  145.  
  146. # not used in grammar, but may appear in "node" passed from Parser to Compiler
  147. encoding_decl: NAME
  148.  
  149. yield_expr: 'yield' [yield_arg]
  150. yield_arg: 'from' test | testlist