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