Fixed bug in time-to-midnight calculation.
[python.git] / Grammar / Grammar
blob1553b34738a2206b4e6ce407c4d89bad0efb6df3
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 # Commands for Kees Blom's railroad program
11 #diagram:token NAME
12 #diagram:token NUMBER
13 #diagram:token STRING
14 #diagram:token NEWLINE
15 #diagram:token ENDMARKER
16 #diagram:token INDENT
17 #diagram:output\input python.bla
18 #diagram:token DEDENT
19 #diagram:output\textwidth 20.04cm\oddsidemargin  0.0cm\evensidemargin 0.0cm
20 #diagram:rules
22 # Start symbols for the grammar:
23 #       single_input is a single interactive statement;
24 #       file_input is a module or sequence of commands read from an input file;
25 #       eval_input is the input for the eval() and input() functions.
26 # NB: compound_stmt in single_input is followed by extra NEWLINE!
27 single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
28 file_input: (NEWLINE | stmt)* ENDMARKER
29 eval_input: testlist NEWLINE* ENDMARKER
31 decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
32 decorators: decorator+
33 funcdef: [decorators] 'def' NAME parameters ':' suite
34 parameters: '(' [varargslist] ')'
35 varargslist: ((fpdef ['=' test] ',')*
36               ('*' NAME [',' '**' NAME] | '**' NAME) |
37               fpdef ['=' test] (',' fpdef ['=' test])* [','])
38 fpdef: NAME | '(' fplist ')'
39 fplist: fpdef (',' fpdef)* [',']
41 stmt: simple_stmt | compound_stmt
42 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
43 small_stmt: (expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt |
44              import_stmt | global_stmt | exec_stmt | assert_stmt)
45 expr_stmt: testlist (augassign (yield_expr|testlist) |
46                      ('=' (yield_expr|testlist))*)
47 augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
48             '<<=' | '>>=' | '**=' | '//=')
49 # For normal assignments, additional restrictions enforced by the interpreter
50 print_stmt: 'print' ( [ test (',' test)* [','] ] |
51                       '>>' test [ (',' test)+ [','] ] )
52 del_stmt: 'del' exprlist
53 pass_stmt: 'pass'
54 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
55 break_stmt: 'break'
56 continue_stmt: 'continue'
57 return_stmt: 'return' [testlist]
58 yield_stmt: yield_expr
59 raise_stmt: 'raise' [test [',' test [',' test]]]
60 import_stmt: import_name | import_from
61 import_name: 'import' dotted_as_names
62 import_from: ('from' ('.')* dotted_name
63               'import' ('*' | '(' import_as_names ')' | import_as_names))
64 import_as_name: NAME [NAME NAME]
65 dotted_as_name: dotted_name [NAME NAME]
66 import_as_names: import_as_name (',' import_as_name)* [',']
67 dotted_as_names: dotted_as_name (',' dotted_as_name)*
68 dotted_name: NAME ('.' NAME)*
69 global_stmt: 'global' NAME (',' NAME)*
70 exec_stmt: 'exec' expr ['in' test [',' test]]
71 assert_stmt: 'assert' test [',' test]
73 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
74 if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
75 while_stmt: 'while' test ':' suite ['else' ':' suite]
76 for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
77 try_stmt: ('try' ':' suite
78            ((except_clause ':' suite)+
79             ['else' ':' suite]
80             ['finally' ':' suite] |
81            'finally' ':' suite))
82 # NB compile.c makes sure that the default except clause is last
83 except_clause: 'except' [test [',' test]]
84 suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
86 test: and_test ('or' and_test)* | lambdef
87 and_test: not_test ('and' not_test)*
88 not_test: 'not' not_test | comparison
89 comparison: expr (comp_op expr)*
90 comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
91 expr: xor_expr ('|' xor_expr)*
92 xor_expr: and_expr ('^' and_expr)*
93 and_expr: shift_expr ('&' shift_expr)*
94 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
95 arith_expr: term (('+'|'-') term)*
96 term: factor (('*'|'/'|'%'|'//') factor)*
97 factor: ('+'|'-'|'~') factor | power
98 power: atom trailer* ['**' factor]
99 atom: ('(' [yield_expr|testlist_gexp] ')' |
100        '[' [listmaker] ']' |
101        '{' [dictmaker] '}' |
102        '`' testlist1 '`' |
103        NAME | NUMBER | STRING+)
104 listmaker: test ( list_for | (',' test)* [','] )
105 testlist_gexp: test ( gen_for | (',' test)* [','] )
106 lambdef: 'lambda' [varargslist] ':' test
107 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
108 subscriptlist: subscript (',' subscript)* [',']
109 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
110 sliceop: ':' [test]
111 exprlist: expr (',' expr)* [',']
112 testlist: test (',' test)* [',']
113 testlist_safe: test [(',' test)+ [',']]
114 dictmaker: test ':' test (',' test ':' test)* [',']
116 classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
118 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
119 argument: test [gen_for] | test '=' test ['(' gen_for ')']  # Really [keyword '='] test
121 list_iter: list_for | list_if
122 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
123 list_if: 'if' test [list_iter]
125 gen_iter: gen_for | gen_if
126 gen_for: 'for' exprlist 'in' test [gen_iter]
127 gen_if: 'if' test [gen_iter]
129 testlist1: test (',' test)*
131 # not used in grammar, but may appear in "node" passed from Parser to Compiler
132 encoding_decl: NAME
134 yield_expr: 'yield' [testlist]