blob: a7ddad3cf322c5fad91c24218db7bcea92c917e0 [file] [log] [blame]
Benjamin Petersona846d0a2012-01-15 21:28:00 -05001# Grammar for 2to3. This grammar supports Python 2.x and 3.x.
Martin v. Löwisef04c442008-03-19 05:04:44 +00002
Łukasz Langab51f5de2018-03-13 00:44:49 -07003# NOTE WELL: You should also follow all the steps listed at
4# https://devguide.python.org/grammar/
Martin v. Löwisef04c442008-03-19 05:04:44 +00005
6# Start symbols for the grammar:
7# file_input is a module or sequence of commands read from an input file;
8# single_input is a single interactive statement;
9# eval_input is the input for the eval() and input() functions.
10# NB: compound_stmt in single_input is followed by extra NEWLINE!
11file_input: (NEWLINE | stmt)* ENDMARKER
12single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
13eval_input: testlist NEWLINE* ENDMARKER
14
15decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
16decorators: decorator+
Yury Selivanov75445082015-05-11 22:57:16 -040017decorated: decorators (classdef | funcdef | async_funcdef)
Jelle Zijlstraf64aae42018-03-18 09:54:33 -070018async_funcdef: ASYNC funcdef
Martin v. Löwisef04c442008-03-19 05:04:44 +000019funcdef: 'def' NAME parameters ['->' test] ':' suite
20parameters: '(' [typedargslist] ')'
21typedargslist: ((tfpdef ['=' test] ',')*
Łukasz Langab51f5de2018-03-13 00:44:49 -070022 ('*' [tname] (',' tname ['=' test])* [',' ['**' tname [',']]] | '**' tname [','])
Martin v. Löwisef04c442008-03-19 05:04:44 +000023 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
24tname: NAME [':' test]
25tfpdef: tname | '(' tfplist ')'
26tfplist: tfpdef (',' tfpdef)* [',']
27varargslist: ((vfpdef ['=' test] ',')*
Łukasz Langab51f5de2018-03-13 00:44:49 -070028 ('*' [vname] (',' vname ['=' test])* [',' ['**' vname [',']]] | '**' vname [','])
Martin v. Löwisef04c442008-03-19 05:04:44 +000029 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
30vname: NAME
31vfpdef: vname | '(' vfplist ')'
32vfplist: vfpdef (',' vfpdef)* [',']
33
34stmt: simple_stmt | compound_stmt
35simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
36small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
37 import_stmt | global_stmt | exec_stmt | assert_stmt)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070038expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Benjamin Petersond9af52b2009-11-02 18:16:28 +000039 ('=' (yield_expr|testlist_star_expr))*)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070040annassign: ':' test ['=' test]
Benjamin Petersond9af52b2009-11-02 18:16:28 +000041testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
Benjamin Peterson4ab92c82014-04-10 00:12:47 -040042augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
Martin v. Löwisef04c442008-03-19 05:04:44 +000043 '<<=' | '>>=' | '**=' | '//=')
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070044# For normal and annotated assignments, additional restrictions enforced by the interpreter
Martin v. Löwisef04c442008-03-19 05:04:44 +000045print_stmt: 'print' ( [ test (',' test)* [','] ] |
46 '>>' test [ (',' test)+ [','] ] )
47del_stmt: 'del' exprlist
48pass_stmt: 'pass'
49flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
50break_stmt: 'break'
51continue_stmt: 'continue'
52return_stmt: 'return' [testlist]
53yield_stmt: yield_expr
54raise_stmt: 'raise' [test ['from' test | ',' test [',' test]]]
55import_stmt: import_name | import_from
56import_name: 'import' dotted_as_names
57import_from: ('from' ('.'* dotted_name | '.'+)
58 'import' ('*' | '(' import_as_names ')' | import_as_names))
59import_as_name: NAME ['as' NAME]
60dotted_as_name: dotted_name ['as' NAME]
61import_as_names: import_as_name (',' import_as_name)* [',']
62dotted_as_names: dotted_as_name (',' dotted_as_name)*
63dotted_name: NAME ('.' NAME)*
64global_stmt: ('global' | 'nonlocal') NAME (',' NAME)*
65exec_stmt: 'exec' expr ['in' test [',' test]]
66assert_stmt: 'assert' test [',' test]
67
Yury Selivanov75445082015-05-11 22:57:16 -040068compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
Jelle Zijlstraf64aae42018-03-18 09:54:33 -070069async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
Martin v. Löwisef04c442008-03-19 05:04:44 +000070if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
71while_stmt: 'while' test ':' suite ['else' ':' suite]
72for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
73try_stmt: ('try' ':' suite
74 ((except_clause ':' suite)+
75 ['else' ':' suite]
76 ['finally' ':' suite] |
77 'finally' ':' suite))
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000078with_stmt: 'with' with_item (',' with_item)* ':' suite
79with_item: test ['as' expr]
Martin v. Löwisef04c442008-03-19 05:04:44 +000080with_var: 'as' expr
81# NB compile.c makes sure that the default except clause is last
82except_clause: 'except' [test [(',' | 'as') test]]
83suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
84
85# Backward compatibility cruft to support:
86# [ x for x in lambda: True, lambda: False if x() ]
87# even while also allowing:
88# lambda x: 5 if x else 2
89# (But not a mix of the two)
90testlist_safe: old_test [(',' old_test)+ [',']]
91old_test: or_test | old_lambdef
92old_lambdef: 'lambda' [varargslist] ':' old_test
93
94test: or_test ['if' or_test 'else' test] | lambdef
95or_test: and_test ('or' and_test)*
96and_test: not_test ('and' not_test)*
97not_test: 'not' not_test | comparison
98comparison: expr (comp_op expr)*
99comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
Benjamin Petersond9af52b2009-11-02 18:16:28 +0000100star_expr: '*' expr
Martin v. Löwisef04c442008-03-19 05:04:44 +0000101expr: xor_expr ('|' xor_expr)*
102xor_expr: and_expr ('^' and_expr)*
103and_expr: shift_expr ('&' shift_expr)*
104shift_expr: arith_expr (('<<'|'>>') arith_expr)*
105arith_expr: term (('+'|'-') term)*
Benjamin Peterson4ab92c82014-04-10 00:12:47 -0400106term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Martin v. Löwisef04c442008-03-19 05:04:44 +0000107factor: ('+'|'-'|'~') factor | power
Jelle Zijlstraf64aae42018-03-18 09:54:33 -0700108power: [AWAIT] atom trailer* ['**' factor]
Martin v. Löwisef04c442008-03-19 05:04:44 +0000109atom: ('(' [yield_expr|testlist_gexp] ')' |
110 '[' [listmaker] ']' |
111 '{' [dictsetmaker] '}' |
112 '`' testlist1 '`' |
113 NAME | NUMBER | STRING+ | '.' '.' '.')
Serhiy Storchaka4b8a7f52018-07-31 09:34:30 +0300114listmaker: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
115testlist_gexp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
Martin v. Löwisef04c442008-03-19 05:04:44 +0000116lambdef: 'lambda' [varargslist] ':' test
117trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
118subscriptlist: subscript (',' subscript)* [',']
119subscript: test | [test] ':' [test] [sliceop]
120sliceop: ':' [test]
Benjamin Petersond9af52b2009-11-02 18:16:28 +0000121exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
Martin v. Löwisef04c442008-03-19 05:04:44 +0000122testlist: test (',' test)* [',']
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)28325742016-09-09 18:18:52 -0700123dictsetmaker: ( ((test ':' test | '**' expr)
124 (comp_for | (',' (test ':' test | '**' expr))* [','])) |
125 ((test | star_expr)
126 (comp_for | (',' (test | star_expr))* [','])) )
Martin v. Löwisef04c442008-03-19 05:04:44 +0000127
128classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
129
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)28325742016-09-09 18:18:52 -0700130arglist: argument (',' argument)* [',']
131
132# "test '=' test" is really "keyword '=' test", but we have no such token.
133# These need to be in a single rule to avoid grammar that is ambiguous
134# to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,
135# we explicitly match '*' here, too, to give it proper precedence.
136# Illegal combinations and orderings are blocked in ast.c:
Martin Panterd2a584b2016-10-10 00:24:34 +0000137# multiple (test comp_for) arguments are blocked; keyword unpackings
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)28325742016-09-09 18:18:52 -0700138# that precede iterable unpackings are blocked; etc.
139argument: ( test [comp_for] |
140 test '=' test |
141 '**' expr |
142 star_expr )
Martin v. Löwisef04c442008-03-19 05:04:44 +0000143
144comp_iter: comp_for | comp_if
Serhiy Storchaka4b8a7f52018-07-31 09:34:30 +0300145comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter]
Martin v. Löwisef04c442008-03-19 05:04:44 +0000146comp_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
Benjamin Peterson0654be12014-04-10 00:23:18 -0400153yield_expr: 'yield' [yield_arg]
154yield_arg: 'from' test | testlist