9. 完整的语法规范

这是完整的Python语法,它由解析器读入用于解析Python源文件:

  1. # Grammar for Python
  2. # Note: Changing the grammar specified in this file will most likely
  3. # require corresponding changes in the parser module
  4. # (../Modules/parsermodule.c). If you can't make the changes to
  5. # that module yourself, please co-ordinate the required changes
  6. # with someone who can; ask around on python-dev for help. Fred
  7. # Drake <fdrake@acm.org> will probably be listening there.
  8. # NOTE WELL: You should also follow all the steps listed in PEP 306,
  9. # "How to Change Python's Grammar"
  10. # Start symbols for the grammar:
  11. # single_input is a single interactive statement;
  12. # file_input is a module or sequence of commands read from an input file;
  13. # eval_input is the input for the eval() and input() functions.
  14. # NB: compound_stmt in single_input is followed by extra NEWLINE!
  15. single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
  16. file_input: (NEWLINE | stmt)* ENDMARKER
  17. eval_input: testlist NEWLINE* ENDMARKER
  18. decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
  19. decorators: decorator+
  20. decorated: decorators (classdef | funcdef)
  21. funcdef: 'def' NAME parameters ':' suite
  22. parameters: '(' [varargslist] ')'
  23. varargslist: ((fpdef ['=' test] ',')*
  24. ('*' NAME [',' '**' NAME] | '**' NAME) |
  25. fpdef ['=' test] (',' fpdef ['=' test])* [','])
  26. fpdef: NAME | '(' fplist ')'
  27. fplist: fpdef (',' fpdef)* [',']
  28. stmt: simple_stmt | compound_stmt
  29. simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
  30. small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
  31. import_stmt | global_stmt | exec_stmt | assert_stmt)
  32. expr_stmt: testlist (augassign (yield_expr|testlist) |
  33. ('=' (yield_expr|testlist))*)
  34. augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
  35. '<<=' | '>>=' | '**=' | '//=')
  36. # For normal assignments, additional restrictions enforced by the interpreter
  37. print_stmt: 'print' ( [ test (',' test)* [','] ] |
  38. '>>' test [ (',' test)+ [','] ] )
  39. del_stmt: 'del' exprlist
  40. pass_stmt: 'pass'
  41. flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
  42. break_stmt: 'break'
  43. continue_stmt: 'continue'
  44. return_stmt: 'return' [testlist]
  45. yield_stmt: yield_expr
  46. raise_stmt: 'raise' [test [',' test [',' test]]]
  47. import_stmt: import_name | import_from
  48. import_name: 'import' dotted_as_names
  49. import_from: ('from' ('.'* dotted_name | '.'+)
  50. 'import' ('*' | '(' import_as_names ')' | import_as_names))
  51. import_as_name: NAME ['as' NAME]
  52. dotted_as_name: dotted_name ['as' NAME]
  53. import_as_names: import_as_name (',' import_as_name)* [',']
  54. dotted_as_names: dotted_as_name (',' dotted_as_name)*
  55. dotted_name: NAME ('.' NAME)*
  56. global_stmt: 'global' NAME (',' NAME)*
  57. exec_stmt: 'exec' expr ['in' test [',' test]]
  58. assert_stmt: 'assert' test [',' test]
  59. compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
  60. if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
  61. while_stmt: 'while' test ':' suite ['else' ':' suite]
  62. for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
  63. try_stmt: ('try' ':' suite
  64. ((except_clause ':' suite)+
  65. ['else' ':' suite]
  66. ['finally' ':' suite] |
  67. 'finally' ':' suite))
  68. with_stmt: 'with' with_item (',' with_item)* ':' suite
  69. with_item: test ['as' expr]
  70. # NB compile.c makes sure that the default except clause is last
  71. except_clause: 'except' [test [('as' | ',') test]]
  72. suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
  73. # Backward compatibility cruft to support:
  74. # [ x for x in lambda: True, lambda: False if x() ]
  75. # even while also allowing:
  76. # lambda x: 5 if x else 2
  77. # (But not a mix of the two)
  78. testlist_safe: old_test [(',' old_test)+ [',']]
  79. old_test: or_test | old_lambdef
  80. old_lambdef: 'lambda' [varargslist] ':' old_test
  81. test: or_test ['if' or_test 'else' test] | lambdef
  82. or_test: and_test ('or' and_test)*
  83. and_test: not_test ('and' not_test)*
  84. not_test: 'not' not_test | comparison
  85. comparison: expr (comp_op expr)*
  86. comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
  87. expr: xor_expr ('|' xor_expr)*
  88. xor_expr: and_expr ('^' and_expr)*
  89. and_expr: shift_expr ('&' shift_expr)*
  90. shift_expr: arith_expr (('<<'|'>>') arith_expr)*
  91. arith_expr: term (('+'|'-') term)*
  92. term: factor (('*'|'/'|'%'|'//') factor)*
  93. factor: ('+'|'-'|'~') factor | power
  94. power: atom trailer* ['**' factor]
  95. atom: ('(' [yield_expr|testlist_comp] ')' |
  96. '[' [listmaker] ']' |
  97. '{' [dictorsetmaker] '}' |
  98. '`' testlist1 '`' |
  99. NAME | NUMBER | STRING+)
  100. listmaker: test ( list_for | (',' test)* [','] )
  101. testlist_comp: test ( comp_for | (',' test)* [','] )
  102. lambdef: 'lambda' [varargslist] ':' test
  103. trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
  104. subscriptlist: subscript (',' subscript)* [',']
  105. subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
  106. sliceop: ':' [test]
  107. exprlist: expr (',' expr)* [',']
  108. testlist: test (',' test)* [',']
  109. dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
  110. (test (comp_for | (',' test)* [','])) )
  111. classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
  112. arglist: (argument ',')* (argument [',']
  113. |'*' test (',' argument)* [',' '**' test]
  114. |'**' test)
  115. # The reason that keywords are test nodes instead of NAME is that using NAME
  116. # results in an ambiguity. ast.c makes sure it's a NAME.
  117. argument: test [comp_for] | test '=' test
  118. list_iter: list_for | list_if
  119. list_for: 'for' exprlist 'in' testlist_safe [list_iter]
  120. list_if: 'if' old_test [list_iter]
  121. comp_iter: comp_for | comp_if
  122. comp_for: 'for' exprlist 'in' or_test [comp_iter]
  123. comp_if: 'if' old_test [comp_iter]
  124. testlist1: test (',' test)*
  125. # not used in grammar, but may appear in "node" passed from Parser to Compiler
  126. encoding_decl: NAME
  127. yield_expr: 'yield' [testlist]

原文: https://wizardforcel.gitbooks.io/python-doc-27-34/content/Text/3.9.html