Require implementations for warnings.showwarning() support the 'line' argument.
[python.git] / Grammar / Grammar
blob865d44c19d1ffd8bfa901138e4998dee3fe0c24c
1 # Grammar for Python
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.
10 # NOTE WELL: You should also follow all the steps listed in PEP 306,
11 # "How to Change Python's Grammar"
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
25 # Start symbols for the grammar:
26 #       single_input is a single interactive statement;
27 #       file_input is a module or sequence of commands read from an input file;
28 #       eval_input is the input for the eval() and input() functions.
29 # NB: compound_stmt in single_input is followed by extra NEWLINE!
30 single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
31 file_input: (NEWLINE | stmt)* ENDMARKER
32 eval_input: testlist NEWLINE* ENDMARKER
34 decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
35 decorators: decorator+
36 decorated: decorators (classdef | funcdef)
37 funcdef: 'def' NAME parameters ':' suite
38 parameters: '(' [varargslist] ')'
39 varargslist: ((fpdef ['=' test] ',')*
40               ('*' NAME [',' '**' NAME] | '**' NAME) |
41               fpdef ['=' test] (',' fpdef ['=' test])* [','])
42 fpdef: NAME | '(' fplist ')'
43 fplist: fpdef (',' fpdef)* [',']
45 stmt: simple_stmt | compound_stmt
46 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
47 small_stmt: (expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt |
48              import_stmt | global_stmt | exec_stmt | assert_stmt)
49 expr_stmt: testlist (augassign (yield_expr|testlist) |
50                      ('=' (yield_expr|testlist))*)
51 augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
52             '<<=' | '>>=' | '**=' | '//=')
53 # For normal assignments, additional restrictions enforced by the interpreter
54 print_stmt: 'print' ( [ test (',' test)* [','] ] |
55                       '>>' test [ (',' test)+ [','] ] )
56 del_stmt: 'del' exprlist
57 pass_stmt: 'pass'
58 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
59 break_stmt: 'break'
60 continue_stmt: 'continue'
61 return_stmt: 'return' [testlist]
62 yield_stmt: yield_expr
63 raise_stmt: 'raise' [test [',' test [',' test]]]
64 import_stmt: import_name | import_from
65 import_name: 'import' dotted_as_names
66 import_from: ('from' ('.'* dotted_name | '.'+)
67               'import' ('*' | '(' import_as_names ')' | import_as_names))
68 import_as_name: NAME ['as' NAME]
69 dotted_as_name: dotted_name ['as' NAME]
70 import_as_names: import_as_name (',' import_as_name)* [',']
71 dotted_as_names: dotted_as_name (',' dotted_as_name)*
72 dotted_name: NAME ('.' NAME)*
73 global_stmt: 'global' NAME (',' NAME)*
74 exec_stmt: 'exec' expr ['in' test [',' test]]
75 assert_stmt: 'assert' test [',' test]
77 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
78 if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
79 while_stmt: 'while' test ':' suite ['else' ':' suite]
80 for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
81 try_stmt: ('try' ':' suite
82            ((except_clause ':' suite)+
83             ['else' ':' suite]
84             ['finally' ':' suite] |
85            'finally' ':' suite))
86 with_stmt: 'with' test [ with_var ] ':' suite
87 with_var: 'as' expr
88 # NB compile.c makes sure that the default except clause is last
89 except_clause: 'except' [test [('as' | ',') test]]
90 suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
92 # Backward compatibility cruft to support:
93 # [ x for x in lambda: True, lambda: False if x() ]
94 # even while also allowing:
95 # lambda x: 5 if x else 2
96 # (But not a mix of the two)
97 testlist_safe: old_test [(',' old_test)+ [',']]
98 old_test: or_test | old_lambdef
99 old_lambdef: 'lambda' [varargslist] ':' old_test
101 test: or_test ['if' or_test 'else' test] | lambdef
102 or_test: and_test ('or' and_test)*
103 and_test: not_test ('and' not_test)*
104 not_test: 'not' not_test | comparison
105 comparison: expr (comp_op expr)*
106 comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
107 expr: xor_expr ('|' xor_expr)*
108 xor_expr: and_expr ('^' and_expr)*
109 and_expr: shift_expr ('&' shift_expr)*
110 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
111 arith_expr: term (('+'|'-') term)*
112 term: factor (('*'|'/'|'%'|'//') factor)*
113 factor: ('+'|'-'|'~') factor | power
114 power: atom trailer* ['**' factor]
115 atom: ('(' [yield_expr|testlist_gexp] ')' |
116        '[' [listmaker] ']' |
117        '{' [dictmaker] '}' |
118        '`' testlist1 '`' |
119        NAME | NUMBER | STRING+)
120 listmaker: test ( list_for | (',' test)* [','] )
121 testlist_gexp: test ( gen_for | (',' test)* [','] )
122 lambdef: 'lambda' [varargslist] ':' test
123 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
124 subscriptlist: subscript (',' subscript)* [',']
125 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
126 sliceop: ':' [test]
127 exprlist: expr (',' expr)* [',']
128 testlist: test (',' test)* [',']
129 dictmaker: test ':' test (',' test ':' test)* [',']
131 classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
133 arglist: (argument ',')* (argument [',']
134                          |'*' test (',' argument)* [',' '**' test] 
135                          |'**' test)
136 argument: test [gen_for] | test '=' test  # Really [keyword '='] test
138 list_iter: list_for | list_if
139 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
140 list_if: 'if' old_test [list_iter]
142 gen_iter: gen_for | gen_if
143 gen_for: 'for' exprlist 'in' or_test [gen_iter]
144 gen_if: 'if' old_test [gen_iter]
146 testlist1: test (',' test)*
148 # not used in grammar, but may appear in "node" passed from Parser to Compiler
149 encoding_decl: NAME
151 yield_expr: 'yield' [testlist]