10. 完整的语法规范

这是完整的 Python 语法规范,直接提取自用于生成 CPython 解析器的语法 (参见 Grammar/python.gram)。 这里显示的版本省略了有关代码生成和错误恢复的细节。

该标记法是 EBNFPEG 的混合体。 特别地,& 后跟一个符号、形符或带括号的分组来表示正向前视(即要求匹配但不会消耗掉),而 ! 表示负向前视(即 _不要求_ 匹配)。 我们使用 | 分隔符来表示 PEG 的“有序选择” (在传统 PEG 语法中写作 /)。 请参阅 PEP 617 了解有关该语法规则的更多细节。

  1. # PEG grammar for Python
  2. file: [statements] ENDMARKER
  3. interactive: statement_newline
  4. eval: expressions NEWLINE* ENDMARKER
  5. func_type: '(' [type_expressions] ')' '->' expression NEWLINE* ENDMARKER
  6. fstring: star_expressions
  7. # type_expressions allow */** but ignore them
  8. type_expressions:
  9. | ','.expression+ ',' '*' expression ',' '**' expression
  10. | ','.expression+ ',' '*' expression
  11. | ','.expression+ ',' '**' expression
  12. | '*' expression ',' '**' expression
  13. | '*' expression
  14. | '**' expression
  15. | ','.expression+
  16. statements: statement+
  17. statement: compound_stmt | simple_stmts
  18. statement_newline:
  19. | compound_stmt NEWLINE
  20. | simple_stmts
  21. | NEWLINE
  22. | ENDMARKER
  23. simple_stmts:
  24. | simple_stmt !';' NEWLINE # Not needed, there for speedup
  25. | ';'.simple_stmt+ [';'] NEWLINE
  26. # NOTE: assignment MUST precede expression, else parsing a simple assignment
  27. # will throw a SyntaxError.
  28. simple_stmt:
  29. | assignment
  30. | star_expressions
  31. | return_stmt
  32. | import_stmt
  33. | raise_stmt
  34. | 'pass'
  35. | del_stmt
  36. | yield_stmt
  37. | assert_stmt
  38. | 'break'
  39. | 'continue'
  40. | global_stmt
  41. | nonlocal_stmt
  42. compound_stmt:
  43. | function_def
  44. | if_stmt
  45. | class_def
  46. | with_stmt
  47. | for_stmt
  48. | try_stmt
  49. | while_stmt
  50. | match_stmt
  51. # NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
  52. assignment:
  53. | NAME ':' expression ['=' annotated_rhs ]
  54. | ('(' single_target ')'
  55. | single_subscript_attribute_target) ':' expression ['=' annotated_rhs ]
  56. | (star_targets '=' )+ (yield_expr | star_expressions) !'=' [TYPE_COMMENT]
  57. | single_target augassign ~ (yield_expr | star_expressions)
  58. augassign:
  59. | '+='
  60. | '-='
  61. | '*='
  62. | '@='
  63. | '/='
  64. | '%='
  65. | '&='
  66. | '|='
  67. | '^='
  68. | '<<='
  69. | '>>='
  70. | '**='
  71. | '//='
  72. global_stmt: 'global' ','.NAME+
  73. nonlocal_stmt: 'nonlocal' ','.NAME+
  74. yield_stmt: yield_expr
  75. assert_stmt: 'assert' expression [',' expression ]
  76. del_stmt:
  77. | 'del' del_targets &(';' | NEWLINE)
  78. import_stmt: import_name | import_from
  79. import_name: 'import' dotted_as_names
  80. # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
  81. import_from:
  82. | 'from' ('.' | '...')* dotted_name 'import' import_from_targets
  83. | 'from' ('.' | '...')+ 'import' import_from_targets
  84. import_from_targets:
  85. | '(' import_from_as_names [','] ')'
  86. | import_from_as_names !','
  87. | '*'
  88. import_from_as_names:
  89. | ','.import_from_as_name+
  90. import_from_as_name:
  91. | NAME ['as' NAME ]
  92. dotted_as_names:
  93. | ','.dotted_as_name+
  94. dotted_as_name:
  95. | dotted_name ['as' NAME ]
  96. dotted_name:
  97. | dotted_name '.' NAME
  98. | NAME
  99. if_stmt:
  100. | 'if' named_expression ':' block elif_stmt
  101. | 'if' named_expression ':' block [else_block]
  102. elif_stmt:
  103. | 'elif' named_expression ':' block elif_stmt
  104. | 'elif' named_expression ':' block [else_block]
  105. else_block:
  106. | 'else' ':' block
  107. while_stmt:
  108. | 'while' named_expression ':' block [else_block]
  109. for_stmt:
  110. | 'for' star_targets 'in' ~ star_expressions ':' [TYPE_COMMENT] block [else_block]
  111. | ASYNC 'for' star_targets 'in' ~ star_expressions ':' [TYPE_COMMENT] block [else_block]
  112. with_stmt:
  113. | 'with' '(' ','.with_item+ ','? ')' ':' block
  114. | 'with' ','.with_item+ ':' [TYPE_COMMENT] block
  115. | ASYNC 'with' '(' ','.with_item+ ','? ')' ':' block
  116. | ASYNC 'with' ','.with_item+ ':' [TYPE_COMMENT] block
  117. with_item:
  118. | expression 'as' star_target &(',' | ')' | ':')
  119. | expression
  120. try_stmt:
  121. | 'try' ':' block finally_block
  122. | 'try' ':' block except_block+ [else_block] [finally_block]
  123. except_block:
  124. | 'except' expression ['as' NAME ] ':' block
  125. | 'except' ':' block
  126. finally_block:
  127. | 'finally' ':' block
  128. match_stmt:
  129. | "match" subject_expr ':' NEWLINE INDENT case_block+ DEDENT
  130. subject_expr:
  131. | star_named_expression ',' star_named_expressions?
  132. | named_expression
  133. case_block:
  134. | "case" patterns guard? ':' block
  135. guard: 'if' named_expression
  136. patterns:
  137. | open_sequence_pattern
  138. | pattern
  139. pattern:
  140. | as_pattern
  141. | or_pattern
  142. as_pattern:
  143. | or_pattern 'as' pattern_capture_target
  144. or_pattern:
  145. | '|'.closed_pattern+
  146. closed_pattern:
  147. | literal_pattern
  148. | capture_pattern
  149. | wildcard_pattern
  150. | value_pattern
  151. | group_pattern
  152. | sequence_pattern
  153. | mapping_pattern
  154. | class_pattern
  155. # Literal patterns are used for equality and identity constraints
  156. literal_pattern:
  157. | signed_number !('+' | '-')
  158. | complex_number
  159. | strings
  160. | 'None'
  161. | 'True'
  162. | 'False'
  163. # Literal expressions are used to restrict permitted mapping pattern keys
  164. literal_expr:
  165. | signed_number !('+' | '-')
  166. | complex_number
  167. | strings
  168. | 'None'
  169. | 'True'
  170. | 'False'
  171. complex_number:
  172. | signed_real_number '+' imaginary_number
  173. | signed_real_number '-' imaginary_number
  174. signed_number:
  175. | NUMBER
  176. | '-' NUMBER
  177. signed_real_number:
  178. | real_number
  179. | '-' real_number
  180. real_number:
  181. | NUMBER
  182. imaginary_number:
  183. | NUMBER
  184. capture_pattern:
  185. | pattern_capture_target
  186. pattern_capture_target:
  187. | !"_" NAME !('.' | '(' | '=')
  188. wildcard_pattern:
  189. | "_"
  190. value_pattern:
  191. | attr !('.' | '(' | '=')
  192. attr:
  193. | name_or_attr '.' NAME
  194. name_or_attr:
  195. | attr
  196. | NAME
  197. group_pattern:
  198. | '(' pattern ')'
  199. sequence_pattern:
  200. | '[' maybe_sequence_pattern? ']'
  201. | '(' open_sequence_pattern? ')'
  202. open_sequence_pattern:
  203. | maybe_star_pattern ',' maybe_sequence_pattern?
  204. maybe_sequence_pattern:
  205. | ','.maybe_star_pattern+ ','?
  206. maybe_star_pattern:
  207. | star_pattern
  208. | pattern
  209. star_pattern:
  210. | '*' pattern_capture_target
  211. | '*' wildcard_pattern
  212. mapping_pattern:
  213. | '{' '}'
  214. | '{' double_star_pattern ','? '}'
  215. | '{' items_pattern ',' double_star_pattern ','? '}'
  216. | '{' items_pattern ','? '}'
  217. items_pattern:
  218. | ','.key_value_pattern+
  219. key_value_pattern:
  220. | (literal_expr | attr) ':' pattern
  221. double_star_pattern:
  222. | '**' pattern_capture_target
  223. class_pattern:
  224. | name_or_attr '(' ')'
  225. | name_or_attr '(' positional_patterns ','? ')'
  226. | name_or_attr '(' keyword_patterns ','? ')'
  227. | name_or_attr '(' positional_patterns ',' keyword_patterns ','? ')'
  228. positional_patterns:
  229. | ','.pattern+
  230. keyword_patterns:
  231. | ','.keyword_pattern+
  232. keyword_pattern:
  233. | NAME '=' pattern
  234. return_stmt:
  235. | 'return' [star_expressions]
  236. raise_stmt:
  237. | 'raise' expression ['from' expression ]
  238. | 'raise'
  239. function_def:
  240. | decorators function_def_raw
  241. | function_def_raw
  242. function_def_raw:
  243. | 'def' NAME '(' [params] ')' ['->' expression ] ':' [func_type_comment] block
  244. | ASYNC 'def' NAME '(' [params] ')' ['->' expression ] ':' [func_type_comment] block
  245. func_type_comment:
  246. | NEWLINE TYPE_COMMENT &(NEWLINE INDENT) # Must be followed by indented block
  247. | TYPE_COMMENT
  248. params:
  249. | parameters
  250. parameters:
  251. | slash_no_default param_no_default* param_with_default* [star_etc]
  252. | slash_with_default param_with_default* [star_etc]
  253. | param_no_default+ param_with_default* [star_etc]
  254. | param_with_default+ [star_etc]
  255. | star_etc
  256. # Some duplication here because we can't write (',' | &')'),
  257. # which is because we don't support empty alternatives (yet).
  258. #
  259. slash_no_default:
  260. | param_no_default+ '/' ','
  261. | param_no_default+ '/' &')'
  262. slash_with_default:
  263. | param_no_default* param_with_default+ '/' ','
  264. | param_no_default* param_with_default+ '/' &')'
  265. star_etc:
  266. | '*' param_no_default param_maybe_default* [kwds]
  267. | '*' ',' param_maybe_default+ [kwds]
  268. | kwds
  269. kwds: '**' param_no_default
  270. # One parameter. This *includes* a following comma and type comment.
  271. #
  272. # There are three styles:
  273. # - No default
  274. # - With default
  275. # - Maybe with default
  276. #
  277. # There are two alternative forms of each, to deal with type comments:
  278. # - Ends in a comma followed by an optional type comment
  279. # - No comma, optional type comment, must be followed by close paren
  280. # The latter form is for a final parameter without trailing comma.
  281. #
  282. param_no_default:
  283. | param ',' TYPE_COMMENT?
  284. | param TYPE_COMMENT? &')'
  285. param_with_default:
  286. | param default ',' TYPE_COMMENT?
  287. | param default TYPE_COMMENT? &')'
  288. param_maybe_default:
  289. | param default? ',' TYPE_COMMENT?
  290. | param default? TYPE_COMMENT? &')'
  291. param: NAME annotation?
  292. annotation: ':' expression
  293. default: '=' expression
  294. decorators: ('@' named_expression NEWLINE )+
  295. class_def:
  296. | decorators class_def_raw
  297. | class_def_raw
  298. class_def_raw:
  299. | 'class' NAME ['(' [arguments] ')' ] ':' block
  300. block:
  301. | NEWLINE INDENT statements DEDENT
  302. | simple_stmts
  303. star_expressions:
  304. | star_expression (',' star_expression )+ [',']
  305. | star_expression ','
  306. | star_expression
  307. star_expression:
  308. | '*' bitwise_or
  309. | expression
  310. star_named_expressions: ','.star_named_expression+ [',']
  311. star_named_expression:
  312. | '*' bitwise_or
  313. | named_expression
  314. assigment_expression:
  315. | NAME ':=' ~ expression
  316. named_expression:
  317. | assigment_expression
  318. | expression !':='
  319. annotated_rhs: yield_expr | star_expressions
  320. expressions:
  321. | expression (',' expression )+ [',']
  322. | expression ','
  323. | expression
  324. expression:
  325. | disjunction 'if' disjunction 'else' expression
  326. | disjunction
  327. | lambdef
  328. lambdef:
  329. | 'lambda' [lambda_params] ':' expression
  330. lambda_params:
  331. | lambda_parameters
  332. # lambda_parameters etc. duplicates parameters but without annotations
  333. # or type comments, and if there's no comma after a parameter, we expect
  334. # a colon, not a close parenthesis. (For more, see parameters above.)
  335. #
  336. lambda_parameters:
  337. | lambda_slash_no_default lambda_param_no_default* lambda_param_with_default* [lambda_star_etc]
  338. | lambda_slash_with_default lambda_param_with_default* [lambda_star_etc]
  339. | lambda_param_no_default+ lambda_param_with_default* [lambda_star_etc]
  340. | lambda_param_with_default+ [lambda_star_etc]
  341. | lambda_star_etc
  342. lambda_slash_no_default:
  343. | lambda_param_no_default+ '/' ','
  344. | lambda_param_no_default+ '/' &':'
  345. lambda_slash_with_default:
  346. | lambda_param_no_default* lambda_param_with_default+ '/' ','
  347. | lambda_param_no_default* lambda_param_with_default+ '/' &':'
  348. lambda_star_etc:
  349. | '*' lambda_param_no_default lambda_param_maybe_default* [lambda_kwds]
  350. | '*' ',' lambda_param_maybe_default+ [lambda_kwds]
  351. | lambda_kwds
  352. lambda_kwds: '**' lambda_param_no_default
  353. lambda_param_no_default:
  354. | lambda_param ','
  355. | lambda_param &':'
  356. lambda_param_with_default:
  357. | lambda_param default ','
  358. | lambda_param default &':'
  359. lambda_param_maybe_default:
  360. | lambda_param default? ','
  361. | lambda_param default? &':'
  362. lambda_param: NAME
  363. disjunction:
  364. | conjunction ('or' conjunction )+
  365. | conjunction
  366. conjunction:
  367. | inversion ('and' inversion )+
  368. | inversion
  369. inversion:
  370. | 'not' inversion
  371. | comparison
  372. comparison:
  373. | bitwise_or compare_op_bitwise_or_pair+
  374. | bitwise_or
  375. compare_op_bitwise_or_pair:
  376. | eq_bitwise_or
  377. | noteq_bitwise_or
  378. | lte_bitwise_or
  379. | lt_bitwise_or
  380. | gte_bitwise_or
  381. | gt_bitwise_or
  382. | notin_bitwise_or
  383. | in_bitwise_or
  384. | isnot_bitwise_or
  385. | is_bitwise_or
  386. eq_bitwise_or: '==' bitwise_or
  387. noteq_bitwise_or:
  388. | ('!=' ) bitwise_or
  389. lte_bitwise_or: '<=' bitwise_or
  390. lt_bitwise_or: '<' bitwise_or
  391. gte_bitwise_or: '>=' bitwise_or
  392. gt_bitwise_or: '>' bitwise_or
  393. notin_bitwise_or: 'not' 'in' bitwise_or
  394. in_bitwise_or: 'in' bitwise_or
  395. isnot_bitwise_or: 'is' 'not' bitwise_or
  396. is_bitwise_or: 'is' bitwise_or
  397. bitwise_or:
  398. | bitwise_or '|' bitwise_xor
  399. | bitwise_xor
  400. bitwise_xor:
  401. | bitwise_xor '^' bitwise_and
  402. | bitwise_and
  403. bitwise_and:
  404. | bitwise_and '&' shift_expr
  405. | shift_expr
  406. shift_expr:
  407. | shift_expr '<<' sum
  408. | shift_expr '>>' sum
  409. | sum
  410. sum:
  411. | sum '+' term
  412. | sum '-' term
  413. | term
  414. term:
  415. | term '*' factor
  416. | term '/' factor
  417. | term '//' factor
  418. | term '%' factor
  419. | term '@' factor
  420. | factor
  421. factor:
  422. | '+' factor
  423. | '-' factor
  424. | '~' factor
  425. | power
  426. power:
  427. | await_primary '**' factor
  428. | await_primary
  429. await_primary:
  430. | AWAIT primary
  431. | primary
  432. primary:
  433. | invalid_primary # must be before 'primay genexp' because of invalid_genexp
  434. | primary '.' NAME
  435. | primary genexp
  436. | primary '(' [arguments] ')'
  437. | primary '[' slices ']'
  438. | atom
  439. slices:
  440. | slice !','
  441. | ','.slice+ [',']
  442. slice:
  443. | [expression] ':' [expression] [':' [expression] ]
  444. | named_expression
  445. atom:
  446. | NAME
  447. | 'True'
  448. | 'False'
  449. | 'None'
  450. | strings
  451. | NUMBER
  452. | (tuple | group | genexp)
  453. | (list | listcomp)
  454. | (dict | set | dictcomp | setcomp)
  455. | '...'
  456. strings: STRING+
  457. list:
  458. | '[' [star_named_expressions] ']'
  459. listcomp:
  460. | '[' named_expression for_if_clauses ']'
  461. tuple:
  462. | '(' [star_named_expression ',' [star_named_expressions] ] ')'
  463. group:
  464. | '(' (yield_expr | named_expression) ')'
  465. genexp:
  466. | '(' ( assigment_expression | expression !':=') for_if_clauses ')'
  467. set: '{' star_named_expressions '}'
  468. setcomp:
  469. | '{' named_expression for_if_clauses '}'
  470. dict:
  471. | '{' [double_starred_kvpairs] '}'
  472. | '{' invalid_double_starred_kvpairs '}'
  473. dictcomp:
  474. | '{' kvpair for_if_clauses '}'
  475. double_starred_kvpairs: ','.double_starred_kvpair+ [',']
  476. double_starred_kvpair:
  477. | '**' bitwise_or
  478. | kvpair
  479. kvpair: expression ':' expression
  480. for_if_clauses:
  481. | for_if_clause+
  482. for_if_clause:
  483. | ASYNC 'for' star_targets 'in' ~ disjunction ('if' disjunction )*
  484. | 'for' star_targets 'in' ~ disjunction ('if' disjunction )*
  485. yield_expr:
  486. | 'yield' 'from' expression
  487. | 'yield' [star_expressions]
  488. arguments:
  489. | args [','] &')'
  490. args:
  491. | ','.(starred_expression | ( assigment_expression | expression !':=') !'=')+ [',' kwargs ]
  492. | kwargs
  493. kwargs:
  494. | ','.kwarg_or_starred+ ',' ','.kwarg_or_double_starred+
  495. | ','.kwarg_or_starred+
  496. | ','.kwarg_or_double_starred+
  497. starred_expression:
  498. | '*' expression
  499. kwarg_or_starred:
  500. | NAME '=' expression
  501. | starred_expression
  502. kwarg_or_double_starred:
  503. | NAME '=' expression
  504. | '**' expression
  505. # NOTE: star_targets may contain *bitwise_or, targets may not.
  506. star_targets:
  507. | star_target !','
  508. | star_target (',' star_target )* [',']
  509. star_targets_list_seq: ','.star_target+ [',']
  510. star_targets_tuple_seq:
  511. | star_target (',' star_target )+ [',']
  512. | star_target ','
  513. star_target:
  514. | '*' (!'*' star_target)
  515. | target_with_star_atom
  516. target_with_star_atom:
  517. | t_primary '.' NAME !t_lookahead
  518. | t_primary '[' slices ']' !t_lookahead
  519. | star_atom
  520. star_atom:
  521. | NAME
  522. | '(' target_with_star_atom ')'
  523. | '(' [star_targets_tuple_seq] ')'
  524. | '[' [star_targets_list_seq] ']'
  525. single_target:
  526. | single_subscript_attribute_target
  527. | NAME
  528. | '(' single_target ')'
  529. single_subscript_attribute_target:
  530. | t_primary '.' NAME !t_lookahead
  531. | t_primary '[' slices ']' !t_lookahead
  532. del_targets: ','.del_target+ [',']
  533. del_target:
  534. | t_primary '.' NAME !t_lookahead
  535. | t_primary '[' slices ']' !t_lookahead
  536. | del_t_atom
  537. del_t_atom:
  538. | NAME
  539. | '(' del_target ')'
  540. | '(' [del_targets] ')'
  541. | '[' [del_targets] ']'
  542. t_primary:
  543. | t_primary '.' NAME &t_lookahead
  544. | t_primary '[' slices ']' &t_lookahead
  545. | t_primary genexp &t_lookahead
  546. | t_primary '(' [arguments] ')' &t_lookahead
  547. | atom &t_lookahead
  548. t_lookahead: '(' | '[' | '.'