]>
Commit | Line | Data |
---|---|---|
53e6db90 DC |
1 | # Grammar for Python |
2 | ||
3 | # NOTE WELL: You should also follow all the steps listed at | |
4 | # https://docs.python.org/devguide/grammar.html | |
5 | ||
6 | # Start symbols for the grammar: | |
7 | # single_input is a single interactive statement; | |
8 | # file_input is a module or sequence of commands read from an input file; | |
9 | # eval_input is the input for the eval() functions. | |
10 | # NB: compound_stmt in single_input is followed by extra NEWLINE! | |
11 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE | |
12 | file_input: stmt* ENDMARKER | |
13 | eval_input: testlist NEWLINE* ENDMARKER | |
14 | decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE | |
15 | decorators: decorator+ | |
16 | decorated: decorators (classdef | funcdef | async_funcdef) | |
17 | ||
18 | # NOTE: Francisco Souza/Reinoud Elhorst, using ASYNC/'await' keywords instead of | |
19 | # skipping python3.5+ compatibility, in favour of 3.7 solution | |
20 | async_funcdef: 'async' funcdef | |
21 | funcdef: 'def' NAME parameters ['->' test] ':' suite | |
22 | ||
23 | parameters: '(' [typedargslist] ')' | |
24 | typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ | |
25 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] | |
26 | | '**' tfpdef [',']]] | |
27 | | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] | |
28 | | '**' tfpdef [',']) | |
29 | tfpdef: NAME [':' test] | |
30 | varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ | |
31 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] | |
32 | | '**' vfpdef [',']]] | |
33 | | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] | |
34 | | '**' vfpdef [','] | |
35 | ) | |
36 | vfpdef: NAME | |
37 | ||
38 | stmt: simple_stmt | compound_stmt | NEWLINE | |
39 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE | |
40 | small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt | | |
41 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt) | |
42 | expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | | |
43 | ('=' (yield_expr|testlist_star_expr))*) | |
44 | annassign: ':' test ['=' test] | |
45 | testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] | |
46 | augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | | |
47 | '<<=' | '>>=' | '**=' | '//=') | |
48 | # For normal and annotated assignments, additional restrictions enforced by the interpreter | |
49 | del_stmt: 'del' exprlist | |
50 | pass_stmt: 'pass' | |
51 | flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt | |
52 | break_stmt: 'break' | |
53 | continue_stmt: 'continue' | |
54 | return_stmt: 'return' [testlist] | |
55 | yield_stmt: yield_expr | |
56 | raise_stmt: 'raise' [test ['from' test]] | |
57 | import_stmt: import_name | import_from | |
58 | import_name: 'import' dotted_as_names | |
59 | # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS | |
60 | import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+) | |
61 | 'import' ('*' | '(' import_as_names ')' | import_as_names)) | |
62 | import_as_name: NAME ['as' NAME] | |
63 | dotted_as_name: dotted_name ['as' NAME] | |
64 | import_as_names: import_as_name (',' import_as_name)* [','] | |
65 | dotted_as_names: dotted_as_name (',' dotted_as_name)* | |
66 | dotted_name: NAME ('.' NAME)* | |
67 | global_stmt: 'global' NAME (',' NAME)* | |
68 | nonlocal_stmt: 'nonlocal' NAME (',' NAME)* | |
69 | assert_stmt: 'assert' test [',' test] | |
70 | ||
71 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt | |
72 | async_stmt: 'async' (funcdef | with_stmt | for_stmt) | |
73 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] | |
74 | while_stmt: 'while' test ':' suite ['else' ':' suite] | |
75 | for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] | |
76 | try_stmt: ('try' ':' suite | |
77 | ((except_clause ':' suite)+ | |
78 | ['else' ':' suite] | |
79 | ['finally' ':' suite] | | |
80 | 'finally' ':' suite)) | |
81 | with_stmt: 'with' with_item (',' with_item)* ':' suite | |
82 | with_item: test ['as' expr] | |
83 | # NB compile.c makes sure that the default except clause is last | |
84 | except_clause: 'except' [test ['as' NAME]] | |
85 | suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT | |
86 | ||
87 | test: or_test ['if' or_test 'else' test] | lambdef | |
88 | test_nocond: or_test | lambdef_nocond | |
89 | lambdef: 'lambda' [varargslist] ':' test | |
90 | lambdef_nocond: 'lambda' [varargslist] ':' test_nocond | |
91 | or_test: and_test ('or' and_test)* | |
92 | and_test: not_test ('and' not_test)* | |
93 | not_test: 'not' not_test | comparison | |
94 | comparison: expr (comp_op expr)* | |
95 | # <> isn't actually a valid comparison operator in Python. It's here for the | |
96 | # sake of a __future__ import described in PEP 401 (which really works :-) | |
97 | comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' | |
98 | star_expr: '*' expr | |
99 | expr: xor_expr ('|' xor_expr)* | |
100 | xor_expr: and_expr ('^' and_expr)* | |
101 | and_expr: shift_expr ('&' shift_expr)* | |
102 | shift_expr: arith_expr (('<<'|'>>') arith_expr)* | |
103 | arith_expr: term (('+'|'-') term)* | |
104 | term: factor (('*'|'@'|'/'|'%'|'//') factor)* | |
105 | factor: ('+'|'-'|'~') factor | power | |
106 | power: atom_expr ['**' factor] | |
107 | atom_expr: ['await'] atom trailer* | |
108 | atom: ('(' [yield_expr|testlist_comp] ')' | | |
109 | '[' [testlist_comp] ']' | | |
110 | '{' [dictorsetmaker] '}' | | |
111 | NAME | NUMBER | strings | '...' | 'None' | 'True' | 'False') | |
112 | testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) | |
113 | trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME | |
114 | subscriptlist: subscript (',' subscript)* [','] | |
115 | subscript: test | [test] ':' [test] [sliceop] | |
116 | sliceop: ':' [test] | |
117 | exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] | |
118 | testlist: test (',' test)* [','] | |
119 | dictorsetmaker: ( ((test ':' test | '**' expr) | |
120 | (comp_for | (',' (test ':' test | '**' expr))* [','])) | | |
121 | ((test | star_expr) | |
122 | (comp_for | (',' (test | star_expr))* [','])) ) | |
123 | ||
124 | classdef: 'class' NAME ['(' [arglist] ')'] ':' suite | |
125 | ||
126 | arglist: argument (',' argument)* [','] | |
127 | ||
128 | # The reason that keywords are test nodes instead of NAME is that using NAME | |
129 | # results in an ambiguity. ast.c makes sure it's a NAME. | |
130 | # "test '=' test" is really "keyword '=' test", but we have no such token. | |
131 | # These need to be in a single rule to avoid grammar that is ambiguous | |
132 | # to our LL(1) parser. Even though 'test' includes '*expr' in star_expr, | |
133 | # we explicitly match '*' here, too, to give it proper precedence. | |
134 | # Illegal combinations and orderings are blocked in ast.c: | |
135 | # multiple (test comp_for) arguments are blocked; keyword unpackings | |
136 | # that precede iterable unpackings are blocked; etc. | |
137 | argument: ( test [comp_for] | | |
138 | test '=' test | | |
139 | '**' test | | |
140 | '*' test ) | |
141 | ||
142 | comp_iter: comp_for | comp_if | |
143 | sync_comp_for: 'for' exprlist 'in' or_test [comp_iter] | |
144 | comp_for: ['async'] sync_comp_for | |
145 | comp_if: 'if' test_nocond [comp_iter] | |
146 | ||
147 | # not used in grammar, but may appear in "node" passed from Parser to Compiler | |
148 | encoding_decl: NAME | |
149 | ||
150 | yield_expr: 'yield' [yield_arg] | |
151 | yield_arg: 'from' test | testlist | |
152 | ||
153 | strings: (STRING | fstring)+ | |
154 | fstring: FSTRING_START fstring_content* FSTRING_END | |
155 | fstring_content: FSTRING_STRING | fstring_expr | |
156 | fstring_conversion: '!' NAME | |
157 | fstring_expr: '{' (testlist_comp | yield_expr) [ fstring_conversion ] [ fstring_format_spec ] '}' | |
158 | fstring_format_spec: ':' fstring_content* |