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 | with_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 with_stmt: 'with' with_item (',' with_item)* ':' suite 61 with_item: test ['as' expr] 62 # NB compile.c makes sure that the default except clause is last 63 except_clause: 'except' [test [('as' | ',') test]] 64 suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT 65 66 # Backward compatibility cruft to support: 67 # [ x for x in lambda: True, lambda: False if x() ] 68 # even while also allowing: 69 # lambda x: 5 if x else 2 70 # (But not a mix of the two) 71 testlist_safe: old_test [(',' old_test)+ [',']] 72 old_test: or_test | old_lambdef 73 old_lambdef: 'lambda' [varargslist] ':' old_test 74 75 test: or_test | lambdef 76 or_test: and_test ('or' and_test)* 77 and_test: not_test ('and' not_test)* 78 not_test: 'not' not_test | comparison 79 comparison: expr (comp_op expr)* 80 comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' 81 expr: xor_expr ('|' xor_expr)* 82 xor_expr: and_expr ('^' and_expr)* 83 and_expr: shift_expr ('&' shift_expr)* 84 shift_expr: arith_expr (('<<'|'>>') arith_expr)* 85 arith_expr: term (('+'|'-') term)* 86 term: factor (('*'|'/'|'%'|'//') factor)* 87 factor: ('+'|'-'|'~') factor | power 88 power: atom trailer* ['**' factor] 89 atom: ('(' [testlist_comp] ')' | 90 '[' [listmaker] ']' | 91 '{' [dictorsetmaker] '}' | 92 '`' testlist1 '`' | 93 NAME | NUMBER | STRING+) 94 listmaker: test ( (',' test)* [','] ) 95 testlist_comp: test ( (',' test)* [','] ) 96 lambdef: 'lambda' [varargslist] ':' test 97 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME 98 subscriptlist: subscript (',' subscript)* [','] 99 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] 100 sliceop: ':' [test] 101 exprlist: expr (',' expr)* [','] 102 testlist: test (',' test)* [','] 103 dictmaker: test ':' test (',' test ':' test)* [','] 104 dictorsetmaker: ( (test ':' test ( (',' test ':' test)* [','])) | 105 (test ( (',' test)* [','])) ) 106 107 classdef: 'class' NAME ['(' [testlist] ')'] ':' suite 108 109 arglist: (argument ',')* (argument [','] 110 |'*' test (',' argument)* [',' '**' test] 111 |'**' test) 112 # The reason that keywords are test nodes instead of NAME is that using NAME 113 # results in an ambiguity. ast.c makes sure it's a NAME. 114 argument: test | test '=' test 115 116 testlist1: test (',' test)* 117 118 # not used in grammar, but may appear in "node" passed from Parser to Compiler 119 encoding_decl: NAME