Skip to content

Commit bcab40c

Browse files
committed
Python Grammar file from Python 3.4
1 parent 8e1e0b2 commit bcab40c

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

parser/Grammar

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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 <[email protected]> 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() 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 ['->' test] ':' suite
26+
parameters: '(' [typedargslist] ')'
27+
typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [','
28+
['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef]]
29+
| '*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
30+
tfpdef: NAME [':' test]
31+
varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [','
32+
['*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef]]
33+
| '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef)
34+
vfpdef: NAME
35+
36+
stmt: simple_stmt | compound_stmt
37+
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
38+
small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
39+
import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
40+
expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
41+
('=' (yield_expr|testlist_star_expr))*)
42+
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
43+
augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
44+
'<<=' | '>>=' | '**=' | '//=')
45+
# For normal assignments, additional restrictions enforced by the interpreter
46+
del_stmt: 'del' exprlist
47+
pass_stmt: 'pass'
48+
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
49+
break_stmt: 'break'
50+
continue_stmt: 'continue'
51+
return_stmt: 'return' [testlist]
52+
yield_stmt: yield_expr
53+
raise_stmt: 'raise' [test ['from' test]]
54+
import_stmt: import_name | import_from
55+
import_name: 'import' dotted_as_names
56+
# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
57+
import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
58+
'import' ('*' | '(' import_as_names ')' | import_as_names))
59+
import_as_name: NAME ['as' NAME]
60+
dotted_as_name: dotted_name ['as' NAME]
61+
import_as_names: import_as_name (',' import_as_name)* [',']
62+
dotted_as_names: dotted_as_name (',' dotted_as_name)*
63+
dotted_name: NAME ('.' NAME)*
64+
global_stmt: 'global' NAME (',' NAME)*
65+
nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
66+
assert_stmt: 'assert' test [',' test]
67+
68+
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
69+
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
70+
while_stmt: 'while' test ':' suite ['else' ':' suite]
71+
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
72+
try_stmt: ('try' ':' suite
73+
((except_clause ':' suite)+
74+
['else' ':' suite]
75+
['finally' ':' suite] |
76+
'finally' ':' suite))
77+
with_stmt: 'with' with_item (',' with_item)* ':' suite
78+
with_item: test ['as' expr]
79+
# NB compile.c makes sure that the default except clause is last
80+
except_clause: 'except' [test ['as' NAME]]
81+
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
82+
83+
test: or_test ['if' or_test 'else' test] | lambdef
84+
test_nocond: or_test | lambdef_nocond
85+
lambdef: 'lambda' [varargslist] ':' test
86+
lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
87+
or_test: and_test ('or' and_test)*
88+
and_test: not_test ('and' not_test)*
89+
not_test: 'not' not_test | comparison
90+
comparison: expr (comp_op expr)*
91+
# <> isn't actually a valid comparison operator in Python. It's here for the
92+
# sake of a __future__ import described in PEP 401
93+
comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
94+
star_expr: '*' expr
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+
'[' [testlist_comp] ']' |
105+
'{' [dictorsetmaker] '}' |
106+
NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
107+
testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
108+
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
109+
subscriptlist: subscript (',' subscript)* [',']
110+
subscript: test | [test] ':' [test] [sliceop]
111+
sliceop: ':' [test]
112+
exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
113+
testlist: test (',' test)* [',']
114+
dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
115+
(test (comp_for | (',' test)* [','])) )
116+
117+
classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
118+
119+
arglist: (argument ',')* (argument [',']
120+
|'*' test (',' argument)* [',' '**' test]
121+
|'**' test)
122+
# The reason that keywords are test nodes instead of NAME is that using NAME
123+
# results in an ambiguity. ast.c makes sure it's a NAME.
124+
argument: test [comp_for] | test '=' test # Really [keyword '='] test
125+
comp_iter: comp_for | comp_if
126+
comp_for: 'for' exprlist 'in' or_test [comp_iter]
127+
comp_if: 'if' test_nocond [comp_iter]
128+
129+
# not used in grammar, but may appear in "node" passed from Parser to Compiler
130+
encoding_decl: NAME
131+
132+
yield_expr: 'yield' [yield_arg]
133+
yield_arg: 'from' test | testlist

0 commit comments

Comments
 (0)