1992-01-14 14:27:17 -04:00
|
|
|
# Grammar for Python
|
1991-12-31 09:11:56 -04:00
|
|
|
|
2000-08-23 12:45:28 -03:00
|
|
|
# Note: Changing the grammar specified in this file will most likely
|
|
|
|
# require corresponding changes in the parser module
|
|
|
|
# (../Modules/parsermodule.c). If you can't make the changes to
|
|
|
|
# that module yourself, please co-ordinate the required changes
|
|
|
|
# with someone who can; ask around on python-dev for help. Fred
|
|
|
|
# Drake <fdrake@acm.org> will probably be listening there.
|
|
|
|
|
2006-04-12 02:16:30 -03:00
|
|
|
# NOTE WELL: You should also follow all the steps listed in PEP 306,
|
|
|
|
# "How to Change Python's Grammar"
|
|
|
|
|
1994-09-29 07:05:45 -03:00
|
|
|
# Commands for Kees Blom's railroad program
|
1994-08-17 10:19:13 -03:00
|
|
|
#diagram:token NAME
|
|
|
|
#diagram:token NUMBER
|
|
|
|
#diagram:token STRING
|
|
|
|
#diagram:token NEWLINE
|
|
|
|
#diagram:token ENDMARKER
|
|
|
|
#diagram:token INDENT
|
|
|
|
#diagram:output\input python.bla
|
|
|
|
#diagram:token DEDENT
|
|
|
|
#diagram:output\textwidth 20.04cm\oddsidemargin 0.0cm\evensidemargin 0.0cm
|
|
|
|
#diagram:rules
|
1994-09-29 07:05:45 -03:00
|
|
|
|
|
|
|
# Start symbols for the grammar:
|
|
|
|
# single_input is a single interactive statement;
|
|
|
|
# file_input is a module or sequence of commands read from an input file;
|
|
|
|
# eval_input is the input for the eval() and input() functions.
|
|
|
|
# NB: compound_stmt in single_input is followed by extra NEWLINE!
|
1990-10-14 09:07:46 -03:00
|
|
|
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
|
|
|
|
file_input: (NEWLINE | stmt)* ENDMARKER
|
1992-03-04 12:41:24 -04:00
|
|
|
eval_input: testlist NEWLINE* ENDMARKER
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2004-08-17 14:29:16 -03:00
|
|
|
decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
|
|
|
|
decorators: decorator+
|
2004-08-02 03:10:11 -03:00
|
|
|
funcdef: [decorators] 'def' NAME parameters ':' suite
|
1992-01-14 14:27:17 -04:00
|
|
|
parameters: '(' [varargslist] ')'
|
2005-12-18 00:12:30 -04:00
|
|
|
varargslist: ((fpdef ['=' test] ',')*
|
|
|
|
('*' NAME [',' '**' NAME] | '**' NAME) |
|
|
|
|
fpdef ['=' test] (',' fpdef ['=' test])* [','])
|
1990-10-14 09:07:46 -03:00
|
|
|
fpdef: NAME | '(' fplist ')'
|
1992-01-14 14:27:17 -04:00
|
|
|
fplist: fpdef (',' fpdef)* [',']
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
stmt: simple_stmt | compound_stmt
|
1991-07-17 15:39:15 -03:00
|
|
|
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
|
2005-12-18 00:12:30 -04:00
|
|
|
small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
|
|
|
|
import_stmt | global_stmt | exec_stmt | assert_stmt)
|
|
|
|
expr_stmt: testlist (augassign (yield_expr|testlist) |
|
|
|
|
('=' (yield_expr|testlist))*)
|
|
|
|
augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
|
|
|
|
'<<=' | '>>=' | '**=' | '//=')
|
2000-08-24 17:11:32 -03:00
|
|
|
# For normal assignments, additional restrictions enforced by the interpreter
|
2005-12-18 00:12:30 -04:00
|
|
|
print_stmt: 'print' ( [ test (',' test)* [','] ] |
|
|
|
|
'>>' test [ (',' test)+ [','] ] )
|
1991-07-17 15:39:15 -03:00
|
|
|
del_stmt: 'del' exprlist
|
|
|
|
pass_stmt: 'pass'
|
2001-06-18 19:08:13 -03:00
|
|
|
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
|
1991-07-17 15:39:15 -03:00
|
|
|
break_stmt: 'break'
|
|
|
|
continue_stmt: 'continue'
|
|
|
|
return_stmt: 'return' [testlist]
|
2005-08-01 21:46:46 -03:00
|
|
|
yield_stmt: yield_expr
|
1998-04-09 18:39:57 -03:00
|
|
|
raise_stmt: 'raise' [test [',' test [',' test]]]
|
2004-08-31 07:07:13 -03:00
|
|
|
import_stmt: import_name | import_from
|
|
|
|
import_name: 'import' dotted_as_names
|
2006-05-25 08:25:51 -03:00
|
|
|
import_from: ('from' ('.'* dotted_name | '.'+)
|
2005-12-18 00:12:30 -04:00
|
|
|
'import' ('*' | '(' import_as_names ')' | import_as_names))
|
2006-09-06 03:28:06 -03:00
|
|
|
import_as_name: NAME ['as' NAME]
|
|
|
|
dotted_as_name: dotted_name ['as' NAME]
|
2004-08-31 07:07:13 -03:00
|
|
|
import_as_names: import_as_name (',' import_as_name)* [',']
|
|
|
|
dotted_as_names: dotted_as_name (',' dotted_as_name)*
|
1995-01-07 06:25:36 -04:00
|
|
|
dotted_name: NAME ('.' NAME)*
|
1991-12-10 09:51:08 -04:00
|
|
|
global_stmt: 'global' NAME (',' NAME)*
|
1993-10-26 14:58:25 -03:00
|
|
|
exec_stmt: 'exec' expr ['in' test [',' test]]
|
1997-04-15 21:34:46 -03:00
|
|
|
assert_stmt: 'assert' test [',' test]
|
1993-05-19 11:50:45 -03:00
|
|
|
|
2006-02-27 18:32:47 -04:00
|
|
|
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef
|
1994-09-29 07:05:45 -03:00
|
|
|
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
|
1990-10-14 09:07:46 -03:00
|
|
|
while_stmt: 'while' test ':' suite ['else' ':' suite]
|
1991-07-17 15:39:15 -03:00
|
|
|
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
|
2005-12-18 00:12:30 -04:00
|
|
|
try_stmt: ('try' ':' suite
|
|
|
|
((except_clause ':' suite)+
|
|
|
|
['else' ':' suite]
|
|
|
|
['finally' ':' suite] |
|
|
|
|
'finally' ':' suite))
|
2006-02-27 18:32:47 -04:00
|
|
|
with_stmt: 'with' test [ with_var ] ':' suite
|
2006-09-06 03:28:06 -03:00
|
|
|
with_var: 'as' expr
|
1992-03-31 14:49:18 -04:00
|
|
|
# NB compile.c makes sure that the default except clause is last
|
2007-05-18 20:11:24 -03:00
|
|
|
except_clause: 'except' [test [('as' | ',') test]]
|
1991-07-27 18:29:47 -03:00
|
|
|
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2006-02-26 20:24:13 -04:00
|
|
|
# Backward compatibility cruft to support:
|
|
|
|
# [ x for x in lambda: True, lambda: False if x() ]
|
|
|
|
# even while also allowing:
|
|
|
|
# lambda x: 5 if x else 2
|
|
|
|
# (But not a mix of the two)
|
|
|
|
testlist_safe: old_test [(',' old_test)+ [',']]
|
|
|
|
old_test: or_test | old_lambdef
|
|
|
|
old_lambdef: 'lambda' [varargslist] ':' old_test
|
|
|
|
|
|
|
|
test: or_test ['if' or_test 'else' test] | lambdef
|
|
|
|
or_test: and_test ('or' and_test)*
|
1990-10-14 09:07:46 -03:00
|
|
|
and_test: not_test ('and' not_test)*
|
|
|
|
not_test: 'not' not_test | comparison
|
|
|
|
comparison: expr (comp_op expr)*
|
1991-12-31 09:11:56 -04:00
|
|
|
comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
|
1991-10-24 11:54:25 -03:00
|
|
|
expr: xor_expr ('|' xor_expr)*
|
|
|
|
xor_expr: and_expr ('^' and_expr)*
|
|
|
|
and_expr: shift_expr ('&' shift_expr)*
|
|
|
|
shift_expr: arith_expr (('<<'|'>>') arith_expr)*
|
|
|
|
arith_expr: term (('+'|'-') term)*
|
2001-08-08 02:00:18 -03:00
|
|
|
term: factor (('*'|'/'|'%'|'//') factor)*
|
1996-01-11 21:00:58 -04:00
|
|
|
factor: ('+'|'-'|'~') factor | power
|
2002-05-23 17:05:40 -03:00
|
|
|
power: atom trailer* ['**' factor]
|
2005-12-18 00:12:30 -04:00
|
|
|
atom: ('(' [yield_expr|testlist_gexp] ')' |
|
|
|
|
'[' [listmaker] ']' |
|
|
|
|
'{' [dictmaker] '}' |
|
|
|
|
'`' testlist1 '`' |
|
|
|
|
NAME | NUMBER | STRING+)
|
2000-08-21 23:43:07 -03:00
|
|
|
listmaker: test ( list_for | (',' test)* [','] )
|
2004-05-19 05:20:33 -03:00
|
|
|
testlist_gexp: test ( gen_for | (',' test)* [','] )
|
1993-11-30 09:40:46 -04:00
|
|
|
lambdef: 'lambda' [varargslist] ':' test
|
1996-07-30 13:43:44 -03:00
|
|
|
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
|
|
|
|
subscriptlist: subscript (',' subscript)* [',']
|
|
|
|
subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
|
|
|
|
sliceop: ':' [test]
|
1990-10-14 09:07:46 -03:00
|
|
|
exprlist: expr (',' expr)* [',']
|
|
|
|
testlist: test (',' test)* [',']
|
1991-07-17 15:39:15 -03:00
|
|
|
dictmaker: test ':' test (',' test ':' test)* [',']
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2005-03-05 02:47:57 -04:00
|
|
|
classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
|
1995-07-07 19:26:23 -03:00
|
|
|
|
2000-03-28 19:49:17 -04:00
|
|
|
arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
|
2006-02-24 19:11:14 -04:00
|
|
|
argument: test [gen_for] | test '=' test # Really [keyword '='] test
|
2000-08-12 15:09:51 -03:00
|
|
|
|
|
|
|
list_iter: list_for | list_if
|
2001-10-15 12:44:05 -03:00
|
|
|
list_for: 'for' exprlist 'in' testlist_safe [list_iter]
|
2006-04-11 21:06:34 -03:00
|
|
|
list_if: 'if' old_test [list_iter]
|
2002-05-24 12:47:06 -03:00
|
|
|
|
2004-05-19 05:20:33 -03:00
|
|
|
gen_iter: gen_for | gen_if
|
2006-02-26 20:24:13 -04:00
|
|
|
gen_for: 'for' exprlist 'in' or_test [gen_iter]
|
2006-04-11 21:06:34 -03:00
|
|
|
gen_if: 'if' old_test [gen_iter]
|
2004-05-19 05:20:33 -03:00
|
|
|
|
2002-05-24 12:47:06 -03:00
|
|
|
testlist1: test (',' test)*
|
2002-08-04 14:29:52 -03:00
|
|
|
|
|
|
|
# not used in grammar, but may appear in "node" passed from Parser to Compiler
|
|
|
|
encoding_decl: NAME
|
2005-08-01 21:46:46 -03:00
|
|
|
|
|
|
|
yield_expr: 'yield' [testlist]
|