]>
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 | # Commands for Kees Blom's railroad program\r | |
14 | #diagram:token NAME\r | |
15 | #diagram:token NUMBER\r | |
16 | #diagram:token STRING\r | |
17 | #diagram:token NEWLINE\r | |
18 | #diagram:token ENDMARKER\r | |
19 | #diagram:token INDENT\r | |
20 | #diagram:output\input python.bla\r | |
21 | #diagram:token DEDENT\r | |
22 | #diagram:output\textwidth 20.04cm\oddsidemargin 0.0cm\evensidemargin 0.0cm\r | |
23 | #diagram:rules\r | |
24 | \r | |
25 | # Start symbols for the grammar:\r | |
26 | # file_input is a module or sequence of commands read from an input file;\r | |
27 | # single_input is a single interactive statement;\r | |
28 | # eval_input is the input for the eval() and input() functions.\r | |
29 | # NB: compound_stmt in single_input is followed by extra NEWLINE!\r | |
30 | file_input: (NEWLINE | stmt)* ENDMARKER\r | |
31 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE\r | |
32 | eval_input: testlist NEWLINE* ENDMARKER\r | |
33 | \r | |
34 | decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE\r | |
35 | decorators: decorator+\r | |
36 | decorated: decorators (classdef | funcdef)\r | |
37 | funcdef: 'def' NAME parameters ['->' test] ':' suite\r | |
38 | parameters: '(' [typedargslist] ')'\r | |
39 | typedargslist: ((tfpdef ['=' test] ',')*\r | |
40 | ('*' [tname] (',' tname ['=' test])* [',' '**' tname] | '**' tname)\r | |
41 | | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])\r | |
42 | tname: NAME [':' test]\r | |
43 | tfpdef: tname | '(' tfplist ')'\r | |
44 | tfplist: tfpdef (',' tfpdef)* [',']\r | |
45 | varargslist: ((vfpdef ['=' test] ',')*\r | |
46 | ('*' [vname] (',' vname ['=' test])* [',' '**' vname] | '**' vname)\r | |
47 | | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])\r | |
48 | vname: NAME\r | |
49 | vfpdef: vname | '(' vfplist ')'\r | |
50 | vfplist: vfpdef (',' vfpdef)* [',']\r | |
51 | \r | |
52 | stmt: simple_stmt | compound_stmt\r | |
53 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE\r | |
54 | small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |\r | |
55 | import_stmt | global_stmt | exec_stmt | assert_stmt)\r | |
56 | expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |\r | |
57 | ('=' (yield_expr|testlist_star_expr))*)\r | |
58 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']\r | |
59 | augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |\r | |
60 | '<<=' | '>>=' | '**=' | '//=')\r | |
61 | # For normal assignments, additional restrictions enforced by the interpreter\r | |
62 | print_stmt: 'print' ( [ test (',' test)* [','] ] |\r | |
63 | '>>' test [ (',' test)+ [','] ] )\r | |
64 | del_stmt: 'del' exprlist\r | |
65 | pass_stmt: 'pass'\r | |
66 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt\r | |
67 | break_stmt: 'break'\r | |
68 | continue_stmt: 'continue'\r | |
69 | return_stmt: 'return' [testlist]\r | |
70 | yield_stmt: yield_expr\r | |
71 | raise_stmt: 'raise' [test ['from' test | ',' test [',' test]]]\r | |
72 | import_stmt: import_name | import_from\r | |
73 | import_name: 'import' dotted_as_names\r | |
74 | import_from: ('from' ('.'* dotted_name | '.'+)\r | |
75 | 'import' ('*' | '(' import_as_names ')' | import_as_names))\r | |
76 | import_as_name: NAME ['as' NAME]\r | |
77 | dotted_as_name: dotted_name ['as' NAME]\r | |
78 | import_as_names: import_as_name (',' import_as_name)* [',']\r | |
79 | dotted_as_names: dotted_as_name (',' dotted_as_name)*\r | |
80 | dotted_name: NAME ('.' NAME)*\r | |
81 | global_stmt: ('global' | 'nonlocal') NAME (',' NAME)*\r | |
82 | exec_stmt: 'exec' expr ['in' test [',' test]]\r | |
83 | assert_stmt: 'assert' test [',' test]\r | |
84 | \r | |
85 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated\r | |
86 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]\r | |
87 | while_stmt: 'while' test ':' suite ['else' ':' suite]\r | |
88 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]\r | |
89 | try_stmt: ('try' ':' suite\r | |
90 | ((except_clause ':' suite)+\r | |
91 | ['else' ':' suite]\r | |
92 | ['finally' ':' suite] |\r | |
93 | 'finally' ':' suite))\r | |
94 | with_stmt: 'with' with_item (',' with_item)* ':' suite\r | |
95 | with_item: test ['as' expr]\r | |
96 | with_var: 'as' expr\r | |
97 | # NB compile.c makes sure that the default except clause is last\r | |
98 | except_clause: 'except' [test [(',' | 'as') test]]\r | |
99 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT\r | |
100 | \r | |
101 | # Backward compatibility cruft to support:\r | |
102 | # [ x for x in lambda: True, lambda: False if x() ]\r | |
103 | # even while also allowing:\r | |
104 | # lambda x: 5 if x else 2\r | |
105 | # (But not a mix of the two)\r | |
106 | testlist_safe: old_test [(',' old_test)+ [',']]\r | |
107 | old_test: or_test | old_lambdef\r | |
108 | old_lambdef: 'lambda' [varargslist] ':' old_test\r | |
109 | \r | |
110 | test: or_test ['if' or_test 'else' test] | lambdef\r | |
111 | or_test: and_test ('or' and_test)*\r | |
112 | and_test: not_test ('and' not_test)*\r | |
113 | not_test: 'not' not_test | comparison\r | |
114 | comparison: expr (comp_op expr)*\r | |
115 | comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'\r | |
116 | star_expr: '*' expr\r | |
117 | expr: xor_expr ('|' xor_expr)*\r | |
118 | xor_expr: and_expr ('^' and_expr)*\r | |
119 | and_expr: shift_expr ('&' shift_expr)*\r | |
120 | shift_expr: arith_expr (('<<'|'>>') arith_expr)*\r | |
121 | arith_expr: term (('+'|'-') term)*\r | |
122 | term: factor (('*'|'/'|'%'|'//') factor)*\r | |
123 | factor: ('+'|'-'|'~') factor | power\r | |
124 | power: atom trailer* ['**' factor]\r | |
125 | atom: ('(' [yield_expr|testlist_gexp] ')' |\r | |
126 | '[' [listmaker] ']' |\r | |
127 | '{' [dictsetmaker] '}' |\r | |
128 | '`' testlist1 '`' |\r | |
129 | NAME | NUMBER | STRING+ | '.' '.' '.')\r | |
130 | listmaker: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )\r | |
131 | testlist_gexp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )\r | |
132 | lambdef: 'lambda' [varargslist] ':' test\r | |
133 | trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME\r | |
134 | subscriptlist: subscript (',' subscript)* [',']\r | |
135 | subscript: test | [test] ':' [test] [sliceop]\r | |
136 | sliceop: ':' [test]\r | |
137 | exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']\r | |
138 | testlist: test (',' test)* [',']\r | |
139 | dictsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |\r | |
140 | (test (comp_for | (',' test)* [','])) )\r | |
141 | \r | |
142 | classdef: 'class' NAME ['(' [arglist] ')'] ':' suite\r | |
143 | \r | |
144 | arglist: (argument ',')* (argument [',']\r | |
145 | |'*' test (',' argument)* [',' '**' test] \r | |
146 | |'**' test)\r | |
147 | argument: test [comp_for] | test '=' test # Really [keyword '='] test\r | |
148 | \r | |
149 | comp_iter: comp_for | comp_if\r | |
150 | comp_for: 'for' exprlist 'in' testlist_safe [comp_iter]\r | |
151 | comp_if: 'if' old_test [comp_iter]\r | |
152 | \r | |
153 | testlist1: test (',' test)*\r | |
154 | \r | |
155 | # not used in grammar, but may appear in "node" passed from Parser to Compiler\r | |
156 | encoding_decl: NAME\r | |
157 | \r | |
158 | yield_expr: 'yield' [testlist]\r |