PageRenderTime 11ms CodeModel.GetById 7ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Grammar/Grammar

http://unladen-swallow.googlecode.com/
#! | 151 lines | 135 code | 16 blank | 0 comment | 0 complexity | 35fa3c2a59738bb774667d06c4f8caa9 MD5 | raw file
  1# Grammar for Python
  2
  3# Note:  Changing the grammar specified in this file will most likely
  4#        require corresponding changes in the parser module
  5#        (../Modules/parsermodule.c).  If you can't make the changes to
  6#        that module yourself, please co-ordinate the required changes
  7#        with someone who can; ask around on python-dev for help.  Fred
  8#        Drake <fdrake@acm.org> will probably be listening there.
  9
 10# NOTE WELL: You should also follow all the steps listed in PEP 306,
 11# "How to Change Python's Grammar"
 12
 13# Commands for Kees Blom's railroad program
 14#diagram:token NAME
 15#diagram:token NUMBER
 16#diagram:token STRING
 17#diagram:token NEWLINE
 18#diagram:token ENDMARKER
 19#diagram:token INDENT
 20#diagram:output\input python.bla
 21#diagram:token DEDENT
 22#diagram:output\textwidth 20.04cm\oddsidemargin  0.0cm\evensidemargin 0.0cm
 23#diagram:rules
 24
 25# Start symbols for the grammar:
 26#	single_input is a single interactive statement;
 27#	file_input is a module or sequence of commands read from an input file;
 28#	eval_input is the input for the eval() and input() functions.
 29# NB: compound_stmt in single_input is followed by extra NEWLINE!
 30single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
 31file_input: (NEWLINE | stmt)* ENDMARKER
 32eval_input: testlist NEWLINE* ENDMARKER
 33
 34decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
 35decorators: decorator+
 36decorated: decorators (classdef | funcdef)
 37funcdef: 'def' NAME parameters ':' suite
 38parameters: '(' [varargslist] ')'
 39varargslist: ((fpdef ['=' test] ',')*
 40              ('*' NAME [',' '**' NAME] | '**' NAME) |
 41              fpdef ['=' test] (',' fpdef ['=' test])* [','])
 42fpdef: NAME | '(' fplist ')'
 43fplist: fpdef (',' fpdef)* [',']
 44
 45stmt: simple_stmt | compound_stmt
 46simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
 47small_stmt: (expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt |
 48             import_stmt | global_stmt | exec_stmt | assert_stmt)
 49expr_stmt: testlist (augassign (yield_expr|testlist) |
 50                     ('=' (yield_expr|testlist))*)
 51augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
 52            '<<=' | '>>=' | '**=' | '//=')
 53# For normal assignments, additional restrictions enforced by the interpreter
 54print_stmt: 'print' ( [ test (',' test)* [','] ] |
 55                      '>>' test [ (',' test)+ [','] ] )
 56del_stmt: 'del' exprlist
 57pass_stmt: 'pass'
 58flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
 59break_stmt: 'break'
 60continue_stmt: 'continue'
 61return_stmt: 'return' [testlist]
 62yield_stmt: yield_expr
 63raise_stmt: 'raise' [test [',' test [',' test]]]
 64import_stmt: import_name | import_from
 65import_name: 'import' dotted_as_names
 66import_from: ('from' ('.'* dotted_name | '.'+)
 67              'import' ('*' | '(' import_as_names ')' | import_as_names))
 68import_as_name: NAME ['as' NAME]
 69dotted_as_name: dotted_name ['as' NAME]
 70import_as_names: import_as_name (',' import_as_name)* [',']
 71dotted_as_names: dotted_as_name (',' dotted_as_name)*
 72dotted_name: NAME ('.' NAME)*
 73global_stmt: 'global' NAME (',' NAME)*
 74exec_stmt: 'exec' expr ['in' test [',' test]]
 75assert_stmt: 'assert' test [',' test]
 76
 77compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
 78if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
 79while_stmt: 'while' test ':' suite ['else' ':' suite]
 80for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
 81try_stmt: ('try' ':' suite
 82           ((except_clause ':' suite)+
 83	    ['else' ':' suite]
 84	    ['finally' ':' suite] |
 85	   'finally' ':' suite))
 86with_stmt: 'with' test [ with_var ] ':' suite
 87with_var: 'as' expr
 88# NB compile.c makes sure that the default except clause is last
 89except_clause: 'except' [test [('as' | ',') test]]
 90suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
 91
 92# Backward compatibility cruft to support:
 93# [ x for x in lambda: True, lambda: False if x() ]
 94# even while also allowing:
 95# lambda x: 5 if x else 2
 96# (But not a mix of the two)
 97testlist_safe: old_test [(',' old_test)+ [',']]
 98old_test: or_test | old_lambdef
 99old_lambdef: 'lambda' [varargslist] ':' old_test
100
101test: or_test ['if' or_test 'else' test] | lambdef
102or_test: and_test ('or' and_test)*
103and_test: not_test ('and' not_test)*
104not_test: 'not' not_test | comparison
105comparison: expr (comp_op expr)*
106comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
107expr: xor_expr ('|' xor_expr)*
108xor_expr: and_expr ('^' and_expr)*
109and_expr: shift_expr ('&' shift_expr)*
110shift_expr: arith_expr (('<<'|'>>') arith_expr)*
111arith_expr: term (('+'|'-') term)*
112term: factor (('*'|'/'|'%'|'//') factor)*
113factor: ('+'|'-'|'~') factor | power
114power: atom trailer* ['**' factor]
115atom: ('(' [yield_expr|testlist_gexp] ')' |
116       '[' [listmaker] ']' |
117       '{' [dictmaker] '}' |
118       '`' testlist1 '`' |
119       NAME | NUMBER | STRING+)
120listmaker: test ( list_for | (',' test)* [','] )
121testlist_gexp: test ( gen_for | (',' test)* [','] )
122lambdef: 'lambda' [varargslist] ':' test
123trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
124subscriptlist: subscript (',' subscript)* [',']
125subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
126sliceop: ':' [test]
127exprlist: expr (',' expr)* [',']
128testlist: test (',' test)* [',']
129dictmaker: test ':' test (',' test ':' test)* [',']
130
131classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
132
133arglist: (argument ',')* (argument [',']
134                         |'*' test (',' argument)* [',' '**' test] 
135                         |'**' test)
136argument: test [gen_for] | test '=' test  # Really [keyword '='] test
137
138list_iter: list_for | list_if
139list_for: 'for' exprlist 'in' testlist_safe [list_iter]
140list_if: 'if' old_test [list_iter]
141
142gen_iter: gen_for | gen_if
143gen_for: 'for' exprlist 'in' or_test [gen_iter]
144gen_if: 'if' old_test [gen_iter]
145
146testlist1: test (',' test)*
147
148# not used in grammar, but may appear in "node" passed from Parser to Compiler
149encoding_decl: NAME
150
151yield_expr: 'yield' [testlist]