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.
|
|
|
|
|
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
|
|
|
|
|
|
|
funcdef: 'def' NAME parameters ':' suite
|
1992-01-14 14:27:17 -04:00
|
|
|
parameters: '(' [varargslist] ')'
|
2000-03-28 19:49:17 -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
|
2000-08-24 17:11:32 -03: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 testlist | ('=' testlist)*)
|
2001-08-08 02:00:18 -03:00
|
|
|
augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=' | '//='
|
2000-08-24 17:11:32 -03:00
|
|
|
# For normal assignments, additional restrictions enforced by the interpreter
|
2000-08-21 12:34:33 -03: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]
|
2001-06-18 19:08:13 -03:00
|
|
|
yield_stmt: 'yield' testlist
|
1998-04-09 18:39:57 -03:00
|
|
|
raise_stmt: 'raise' [test [',' test [',' test]]]
|
2000-08-17 19:55:00 -03:00
|
|
|
import_stmt: 'import' dotted_as_name (',' dotted_as_name)* | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
|
|
|
|
import_as_name: NAME [NAME NAME]
|
|
|
|
dotted_as_name: dotted_name [NAME 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
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
compound_stmt: if_stmt | while_stmt | for_stmt | try_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]
|
1994-08-17 10:19:13 -03:00
|
|
|
try_stmt: ('try' ':' suite (except_clause ':' suite)+ #diagram:break
|
|
|
|
['else' ':' suite] | 'try' ':' suite 'finally' ':' suite)
|
1992-03-31 14:49:18 -04:00
|
|
|
# NB compile.c makes sure that the default except clause is last
|
1991-07-17 15:39:15 -03:00
|
|
|
except_clause: 'except' [test [',' test]]
|
1991-07-27 18:29:47 -03:00
|
|
|
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1993-11-30 10:57:42 -04:00
|
|
|
test: and_test ('or' and_test)* | lambdef
|
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]
|
2002-05-24 12:47:06 -03:00
|
|
|
atom: '(' [testlist] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}' | '`' testlist1 '`' | NAME | NUMBER | STRING+
|
2000-08-21 23:43:07 -03:00
|
|
|
listmaker: test ( list_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)* [',']
|
2001-10-15 12:44:05 -03:00
|
|
|
testlist_safe: test [(',' test)+ [',']]
|
1991-07-17 15:39:15 -03:00
|
|
|
dictmaker: test ':' test (',' test ':' test)* [',']
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1993-12-20 08:53:10 -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)
|
1995-07-07 19:26:23 -03:00
|
|
|
argument: [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]
|
2000-08-12 15:09:51 -03:00
|
|
|
list_if: 'if' test [list_iter]
|
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
|