OILS / opy / py27.grammar View on Github | oilshell.org

142 lines, 127 significant
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!
18single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
19file_input: (NEWLINE | stmt)* ENDMARKER
20eval_input: testlist NEWLINE* ENDMARKER
21
22decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
23decorators: decorator+
24decorated: decorators (classdef | funcdef)
25funcdef: 'def' NAME parameters ':' suite
26parameters: '(' [varargslist] ')'
27varargslist: ((fpdef ['=' test] ',')*
28 ('*' NAME [',' '**' NAME] | '**' NAME) |
29 fpdef ['=' test] (',' fpdef ['=' test])* [','])
30fpdef: NAME | '(' fplist ')'
31fplist: fpdef (',' fpdef)* [',']
32
33stmt: simple_stmt | compound_stmt
34simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
35small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
36 import_stmt | global_stmt | exec_stmt | assert_stmt)
37expr_stmt: testlist (augassign (yield_expr|testlist) |
38 ('=' (yield_expr|testlist))*)
39augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
40 '<<=' | '>>=' | '**=' | '//=')
41# For normal assignments, additional restrictions enforced by the interpreter
42print_stmt: 'print' ( [ test (',' test)* [','] ] |
43 '>>' test [ (',' test)+ [','] ] )
44del_stmt: 'del' exprlist
45pass_stmt: 'pass'
46flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
47break_stmt: 'break'
48continue_stmt: 'continue'
49return_stmt: 'return' [testlist]
50yield_stmt: yield_expr
51raise_stmt: 'raise' [test [',' test [',' test]]]
52import_stmt: import_name | import_from
53import_name: 'import' dotted_as_names
54import_from: ('from' ('.'* dotted_name | '.'+)
55 'import' ('*' | '(' import_as_names ')' | import_as_names))
56import_as_name: NAME ['as' NAME]
57dotted_as_name: dotted_name ['as' NAME]
58import_as_names: import_as_name (',' import_as_name)* [',']
59dotted_as_names: dotted_as_name (',' dotted_as_name)*
60dotted_name: NAME ('.' NAME)*
61global_stmt: 'global' NAME (',' NAME)*
62exec_stmt: 'exec' expr ['in' test [',' test]]
63assert_stmt: 'assert' test [',' test]
64
65compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
66if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
67while_stmt: 'while' test ':' suite ['else' ':' suite]
68for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
69try_stmt: ('try' ':' suite
70 ((except_clause ':' suite)+
71 ['else' ':' suite]
72 ['finally' ':' suite] |
73 'finally' ':' suite))
74with_stmt: 'with' with_item (',' with_item)* ':' suite
75with_item: test ['as' expr]
76# NB compile.c makes sure that the default except clause is last
77except_clause: 'except' [test [('as' | ',') test]]
78suite: 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)
85testlist_safe: old_test [(',' old_test)+ [',']]
86old_test: or_test | old_lambdef
87old_lambdef: 'lambda' [varargslist] ':' old_test
88
89test: or_test ['if' or_test 'else' test] | lambdef
90or_test: and_test ('or' and_test)*
91and_test: not_test ('and' not_test)*
92not_test: 'not' not_test | comparison
93comparison: expr (comp_op expr)*
94comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
95expr: xor_expr ('|' xor_expr)*
96xor_expr: and_expr ('^' and_expr)*
97and_expr: shift_expr ('&' shift_expr)*
98shift_expr: arith_expr (('<<'|'>>') arith_expr)*
99arith_expr: term (('+'|'-') term)*
100term: factor (('*'|'/'|'%'|'//') factor)*
101factor: ('+'|'-'|'~') factor | power
102power: atom trailer* ['**' factor]
103atom: ('(' [yield_expr|testlist_comp] ')' |
104 '[' [listmaker] ']' |
105 '{' [dictorsetmaker] '}' |
106 '`' testlist1 '`' |
107 NAME | NUMBER | STRING+)
108listmaker: test ( list_for | (',' test)* [','] )
109testlist_comp: test ( comp_for | (',' test)* [','] )
110lambdef: 'lambda' [varargslist] ':' test
111trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
112subscriptlist: subscript (',' subscript)* [',']
113subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
114sliceop: ':' [test]
115exprlist: expr (',' expr)* [',']
116testlist: test (',' test)* [',']
117dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
118 (test (comp_for | (',' test)* [','])) )
119
120classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
121
122arglist: (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.
127argument: test [comp_for] | test '=' test
128
129list_iter: list_for | list_if
130list_for: 'for' exprlist 'in' testlist_safe [list_iter]
131list_if: 'if' old_test [list_iter]
132
133comp_iter: comp_for | comp_if
134comp_for: 'for' exprlist 'in' or_test [comp_iter]
135comp_if: 'if' old_test [comp_iter]
136
137testlist1: test (',' test)*
138
139# not used in grammar, but may appear in "node" passed from Parser to Compiler
140encoding_decl: NAME
141
142yield_expr: 'yield' [testlist]