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