1 # Grammar for Lichen 2 3 # Start symbols for the grammar: 4 # single_input is a single interactive statement; 5 # file_input is a module or sequence of commands read from an input file; 6 # eval_input is the input for the eval() and input() functions. 7 # NB: compound_stmt in single_input is followed by extra NEWLINE! 8 single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE 9 file_input: (NEWLINE | stmt)* ENDMARKER 10 eval_input: testlist NEWLINE* ENDMARKER 11 12 funcdef: 'def' NAME parameters ':' suite 13 parameters: '(' [varargslist] ')' 14 varargslist: ((fpdef ['=' test] ',')* 15 ('*' NAME [',' '**' NAME] | '**' NAME) | 16 fpdef ['=' test] (',' fpdef ['=' test])* [',']) 17 fpdef: NAME | '(' fplist ')' 18 fplist: fpdef (',' fpdef)* [','] 19 20 stmt: simple_stmt | compound_stmt 21 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE 22 small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | 23 import_stmt | global_stmt | exec_stmt | assert_stmt) 24 expr_stmt: testlist (augassign testlist | 25 ('=' testlist)*) 26 augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | 27 '<<=' | '>>=' | '**=' | '//=') 28 # For normal assignments, additional restrictions enforced by the interpreter 29 print_stmt: 'print' ( [ test (',' test)* [','] ] | 30 '>>' test [ (',' test)+ [','] ] ) 31 del_stmt: 'del' exprlist 32 pass_stmt: 'pass' 33 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt 34 break_stmt: 'break' 35 continue_stmt: 'continue' 36 return_stmt: 'return' [testlist] 37 raise_stmt: 'raise' [test [',' test [',' test]]] 38 import_stmt: import_name | import_from 39 import_name: 'import' dotted_as_names 40 import_from: ('from' dotted_name 41 'import' ('*' | '(' import_as_names ')' | import_as_names)) 42 import_as_name: NAME ['as' NAME] 43 dotted_as_name: dotted_name ['as' NAME] 44 import_as_names: import_as_name (',' import_as_name)* [','] 45 dotted_as_names: dotted_as_name (',' dotted_as_name)* 46 dotted_name: NAME ('.' NAME)* 47 global_stmt: 'global' NAME (',' NAME)* 48 exec_stmt: 'exec' expr ['in' test [',' test]] 49 assert_stmt: 'assert' test [',' test] 50 51 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef 52 if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] 53 while_stmt: 'while' test ':' suite ['else' ':' suite] 54 for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] 55 try_stmt: ('try' ':' suite 56 ((except_clause ':' suite)+ 57 ['else' ':' suite] 58 ['finally' ':' suite] | 59 'finally' ':' suite)) 60 # NB compile.c makes sure that the default except clause is last 61 except_clause: 'except' [test [('as' | ',') test]] 62 suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT 63 64 # Backward compatibility cruft to support: 65 # [ x for x in lambda: True, lambda: False if x() ] 66 # even while also allowing: 67 # lambda x: 5 if x else 2 68 # (But not a mix of the two) 69 testlist_safe: old_test [(',' old_test)+ [',']] 70 old_test: or_test | old_lambdef 71 old_lambdef: 'lambda' [varargslist] ':' old_test 72 73 test: or_test | lambdef 74 or_test: and_test ('or' and_test)* 75 and_test: not_test ('and' not_test)* 76 not_test: 'not' not_test | comparison 77 comparison: expr (comp_op expr)* 78 comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' 79 expr: xor_expr ('|' xor_expr)* 80 xor_expr: and_expr ('^' and_expr)* 81 and_expr: shift_expr ('&' shift_expr)* 82 shift_expr: arith_expr (('<<'|'>>') arith_expr)* 83 arith_expr: term (('+'|'-') term)* 84 term: factor (('*'|'/'|'%'|'//') factor)* 85 factor: ('+'|'-'|'~') factor | power 86 power: atom trailer* ['**' factor] 87 atom: ('(' [testlist_comp] ')' | 88 '[' [listmaker] ']' | 89 '{' [dictorsetmaker] '}' | 90 '`' testlist1 '`' | 91 NAME | NUMBER | STRING+) 92 listmaker: test ( (',' test)* [','] ) 93 testlist_comp: test ( (',' test)* [','] ) 94 lambdef: 'lambda' [varargslist] ':' test 95 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME 96 subscriptlist: subscript (',' subscript)* [','] 97 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] 98 sliceop: ':' [test] 99 exprlist: expr (',' expr)* [','] 100 testlist: test (',' test)* [','] 101 dictmaker: test ':' test (',' test ':' test)* [','] 102 dictorsetmaker: ( (test ':' test ( (',' test ':' test)* [','])) | 103 (test ( (',' test)* [','])) ) 104 105 classdef: 'class' NAME ['(' [testlist] ')'] ':' suite 106 107 arglist: (argument ',')* (argument [','] 108 |'*' test (',' argument)* [',' '**' test] 109 |'**' test) 110 # The reason that keywords are test nodes instead of NAME is that using NAME 111 # results in an ambiguity. ast.c makes sure it's a NAME. 112 argument: test | test '=' test 113 114 testlist1: test (',' test)* 115 116 # not used in grammar, but may appear in "node" passed from Parser to Compiler 117 encoding_decl: NAME