3 # Commands for Kees Blom's railroad program
8 #diagram:token ENDMARKER
10 #diagram:output\input python.bla
12 #diagram:output\textwidth 20.04cm\oddsidemargin 0.0cm\evensidemargin 0.0cm
15 # Start symbols for the grammar:
16 # single_input is a single interactive statement;
17 # file_input is a module or sequence of commands read from an input file;
18 # eval_input is the input for the eval() and input() functions.
19 # NB: compound_stmt in single_input is followed by extra NEWLINE!
20 single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
21 file_input: (NEWLINE | stmt)* ENDMARKER
22 eval_input: testlist NEWLINE* ENDMARKER
24 funcdef: 'def' NAME parameters ':' suite
25 parameters: '(' [varargslist] ')'
26 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME] | ('**'|'*' '*') NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
27 fpdef: NAME | '(' fplist ')'
28 fplist: fpdef (',' fpdef)* [',']
30 stmt: simple_stmt | compound_stmt
31 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
32 #small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
33 small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | exec_stmt | assert_stmt
34 expr_stmt: testlist ('=' testlist)*
35 # For assignments, additional restrictions enforced by the interpreter
36 print_stmt: 'print' (test ',')* [test]
37 del_stmt: 'del' exprlist
39 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
41 continue_stmt: 'continue'
42 return_stmt: 'return' [testlist]
43 raise_stmt: 'raise' [test [',' test [',' test]]]
44 import_stmt: 'import' dotted_name (',' dotted_name)* | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*)
45 dotted_name: NAME ('.' NAME)*
46 global_stmt: 'global' NAME (',' NAME)*
47 #access_stmt: 'access' ('*' | NAME (',' NAME)*) ':' accesstype (',' accesstype)*
49 ## accesstype should be ('public' | 'protected' | 'private') ['read'] ['write']
50 ## but can't be because that would create undesirable reserved words!
51 exec_stmt: 'exec' expr ['in' test [',' test]]
52 assert_stmt: 'assert' test [',' test]
54 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
55 if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
56 while_stmt: 'while' test ':' suite ['else' ':' suite]
57 for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
58 try_stmt: ('try' ':' suite (except_clause ':' suite)+ #diagram:break
59 ['else' ':' suite] | 'try' ':' suite 'finally' ':' suite)
60 # NB compile.c makes sure that the default except clause is last
61 except_clause: 'except' [test [',' test]]
62 suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
64 test: and_test ('or' and_test)* | lambdef
65 and_test: not_test ('and' not_test)*
66 not_test: 'not' not_test | comparison
67 comparison: expr (comp_op expr)*
68 comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
69 expr: xor_expr ('|' xor_expr)*
70 xor_expr: and_expr ('^' and_expr)*
71 and_expr: shift_expr ('&' shift_expr)*
72 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
73 arith_expr: term (('+'|'-') term)*
74 term: factor (('*'|'/'|'%') factor)*
75 factor: ('+'|'-'|'~') factor | power
76 power: atom trailer* ('**' factor)*
77 atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
78 lambdef: 'lambda' [varargslist] ':' test
79 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
80 subscriptlist: subscript (',' subscript)* [',']
81 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
83 exprlist: expr (',' expr)* [',']
84 testlist: test (',' test)* [',']
85 dictmaker: test ':' test (',' test ':' test)* [',']
87 classdef: 'class' NAME ['(' testlist ')'] ':' suite
89 arglist: argument (',' argument)* [',']
90 argument: [test '='] test # Really [keyword '='] test