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