1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/pyparser/data/Grammar2.7 Sun Jan 08 20:20:39 2017 +0100
1.3 @@ -0,0 +1,143 @@
1.4 +# Grammar for Python
1.5 +
1.6 +# Note: Changing the grammar specified in this file will most likely
1.7 +# require corresponding changes in the parser module
1.8 +# (../Modules/parsermodule.c). If you can't make the changes to
1.9 +# that module yourself, please co-ordinate the required changes
1.10 +# with someone who can; ask around on python-dev for help. Fred
1.11 +# Drake <fdrake@acm.org> will probably be listening there.
1.12 +
1.13 +# NOTE WELL: You should also follow all the steps listed in PEP 306,
1.14 +# "How to Change Python's Grammar"
1.15 +
1.16 +# Start symbols for the grammar:
1.17 +# single_input is a single interactive statement;
1.18 +# file_input is a module or sequence of commands read from an input file;
1.19 +# eval_input is the input for the eval() and input() functions.
1.20 +# NB: compound_stmt in single_input is followed by extra NEWLINE!
1.21 +single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
1.22 +file_input: (NEWLINE | stmt)* ENDMARKER
1.23 +eval_input: testlist NEWLINE* ENDMARKER
1.24 +
1.25 +decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
1.26 +decorators: decorator+
1.27 +decorated: decorators (classdef | funcdef)
1.28 +funcdef: 'def' NAME parameters ':' suite
1.29 +parameters: '(' [varargslist] ')'
1.30 +varargslist: ((fpdef ['=' test] ',')*
1.31 + ('*' NAME [',' '**' NAME] | '**' NAME) |
1.32 + fpdef ['=' test] (',' fpdef ['=' test])* [','])
1.33 +fpdef: NAME | '(' fplist ')'
1.34 +fplist: fpdef (',' fpdef)* [',']
1.35 +
1.36 +stmt: simple_stmt | compound_stmt
1.37 +simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
1.38 +small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
1.39 + import_stmt | global_stmt | exec_stmt | assert_stmt)
1.40 +expr_stmt: testlist (augassign (yield_expr|testlist) |
1.41 + ('=' (yield_expr|testlist))*)
1.42 +augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
1.43 + '<<=' | '>>=' | '**=' | '//=')
1.44 +# For normal assignments, additional restrictions enforced by the interpreter
1.45 +print_stmt: 'print' ( [ test (',' test)* [','] ] |
1.46 + '>>' test [ (',' test)+ [','] ] )
1.47 +del_stmt: 'del' exprlist
1.48 +pass_stmt: 'pass'
1.49 +flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
1.50 +break_stmt: 'break'
1.51 +continue_stmt: 'continue'
1.52 +return_stmt: 'return' [testlist]
1.53 +yield_stmt: yield_expr
1.54 +raise_stmt: 'raise' [test [',' test [',' test]]]
1.55 +import_stmt: import_name | import_from
1.56 +import_name: 'import' dotted_as_names
1.57 +import_from: ('from' ('.'* dotted_name | '.'+)
1.58 + 'import' ('*' | '(' import_as_names ')' | import_as_names))
1.59 +import_as_name: NAME ['as' NAME]
1.60 +dotted_as_name: dotted_name ['as' NAME]
1.61 +import_as_names: import_as_name (',' import_as_name)* [',']
1.62 +dotted_as_names: dotted_as_name (',' dotted_as_name)*
1.63 +dotted_name: NAME ('.' NAME)*
1.64 +global_stmt: 'global' NAME (',' NAME)*
1.65 +exec_stmt: 'exec' expr ['in' test [',' test]]
1.66 +assert_stmt: 'assert' test [',' test]
1.67 +
1.68 +compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
1.69 +if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1.70 +while_stmt: 'while' test ':' suite ['else' ':' suite]
1.71 +for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
1.72 +try_stmt: ('try' ':' suite
1.73 + ((except_clause ':' suite)+
1.74 + ['else' ':' suite]
1.75 + ['finally' ':' suite] |
1.76 + 'finally' ':' suite))
1.77 +with_stmt: 'with' with_item (',' with_item)* ':' suite
1.78 +with_item: test ['as' expr]
1.79 +# NB compile.c makes sure that the default except clause is last
1.80 +except_clause: 'except' [test [('as' | ',') test]]
1.81 +suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
1.82 +
1.83 +# Backward compatibility cruft to support:
1.84 +# [ x for x in lambda: True, lambda: False if x() ]
1.85 +# even while also allowing:
1.86 +# lambda x: 5 if x else 2
1.87 +# (But not a mix of the two)
1.88 +testlist_safe: old_test [(',' old_test)+ [',']]
1.89 +old_test: or_test | old_lambdef
1.90 +old_lambdef: 'lambda' [varargslist] ':' old_test
1.91 +
1.92 +test: or_test ['if' or_test 'else' test] | lambdef
1.93 +or_test: and_test ('or' and_test)*
1.94 +and_test: not_test ('and' not_test)*
1.95 +not_test: 'not' not_test | comparison
1.96 +comparison: expr (comp_op expr)*
1.97 +comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
1.98 +expr: xor_expr ('|' xor_expr)*
1.99 +xor_expr: and_expr ('^' and_expr)*
1.100 +and_expr: shift_expr ('&' shift_expr)*
1.101 +shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1.102 +arith_expr: term (('+'|'-') term)*
1.103 +term: factor (('*'|'/'|'%'|'//') factor)*
1.104 +factor: ('+'|'-'|'~') factor | power
1.105 +power: atom trailer* ['**' factor]
1.106 +atom: ('(' [yield_expr|testlist_comp] ')' |
1.107 + '[' [listmaker] ']' |
1.108 + '{' [dictorsetmaker] '}' |
1.109 + '`' testlist1 '`' |
1.110 + NAME | NUMBER | STRING+)
1.111 +listmaker: test ( list_for | (',' test)* [','] )
1.112 +testlist_comp: test ( comp_for | (',' test)* [','] )
1.113 +lambdef: 'lambda' [varargslist] ':' test
1.114 +trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
1.115 +subscriptlist: subscript (',' subscript)* [',']
1.116 +subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
1.117 +sliceop: ':' [test]
1.118 +exprlist: expr (',' expr)* [',']
1.119 +testlist: test (',' test)* [',']
1.120 +dictmaker: test ':' test (',' test ':' test)* [',']
1.121 +dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
1.122 + (test (comp_for | (',' test)* [','])) )
1.123 +
1.124 +classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
1.125 +
1.126 +arglist: (argument ',')* (argument [',']
1.127 + |'*' test (',' argument)* [',' '**' test]
1.128 + |'**' test)
1.129 +# The reason that keywords are test nodes instead of NAME is that using NAME
1.130 +# results in an ambiguity. ast.c makes sure it's a NAME.
1.131 +argument: test [comp_for] | test '=' test
1.132 +
1.133 +list_iter: list_for | list_if
1.134 +list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1.135 +list_if: 'if' old_test [list_iter]
1.136 +
1.137 +comp_iter: comp_for | comp_if
1.138 +comp_for: 'for' exprlist 'in' or_test [comp_iter]
1.139 +comp_if: 'if' old_test [comp_iter]
1.140 +
1.141 +testlist1: test (',' test)*
1.142 +
1.143 +# not used in grammar, but may appear in "node" passed from Parser to Compiler
1.144 +encoding_decl: NAME
1.145 +
1.146 +yield_expr: 'yield' [testlist]