Re: my recursive function call is wrong?
Dear Kev Thank you very much. I got it.:) 2009/8/16 Kev Dwyer > On Sun, 16 Aug 2009 16:57:41 +0900, Chang Min Jeon wrote: > > > Hello, > > You have placed recursive calls to the function in a number of different > locations; when len(macro) becomes zero control will return to the > calling function, but this calling function may have more code to > execute, including further calls to start_parse(), and further attempts > to index macro. > > I like to keep recursive calls at the end of a function, so that there is > a clean path back to the top level caller once the terminal condition is > reached. You can do it differently, but you need to bear in mind the > execution paths through your code. > > Cheers, > > Kev > > -- > http://mail.python.org/mailman/listinfo/python-list > -- CashFlow To be rich. -- http://mail.python.org/mailman/listinfo/python-list
Re: my recursive function call is wrong?
On Sun, 16 Aug 2009 16:57:41 +0900, Chang Min Jeon wrote: Hello, You have placed recursive calls to the function in a number of different locations; when len(macro) becomes zero control will return to the calling function, but this calling function may have more code to execute, including further calls to start_parse(), and further attempts to index macro. I like to keep recursive calls at the end of a function, so that there is a clean path back to the top level caller once the terminal condition is reached. You can do it differently, but you need to bear in mind the execution paths through your code. Cheers, Kev -- http://mail.python.org/mailman/listinfo/python-list
my recursive function call is wrong?
I'm trying to write program to translate define macro in 'C'. And start_parse has return condition that list's length is 0. At this time return statement invoke start_parse() function. I can't understand do that. I'm using Python 2.6.2 in Windows XP import re import sys comment = ''' #if defined (FEATURE_ONENESTED) #define PYTHON_POWERED #if defined(ANY_LANGUAGE) #error #endif #else #define FEATURE_NONE #endif ''' symbol_table = ['FEATURE_ONENESTED'] valid_area = False p_define = re.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]*') p_if = re.compile('^[\t ]*#[\t ]*if[\t ]+defined[\s]*[\(]*([a-zA-Z0-9_]+)[\)]*[\t ]*') p_elif = re.compile('^[\t ]*#[\t ]*elif[\t ]*') p_else = re.compile('^[\t ]*#[\t ]*else[\t ]*') p_endif = re.compile('^[\t ]*#[\t ]*endif[\t ]*') def start_parse(macro): global valid_area if len(macro) == 0: return if valid_area == True: if p_else.match(macro[0]): valid_area = False macro.pop(0) start_parse(macro) match = p_define.match(macro[0]) if match: symbol_table.append(match.group(1)) macro.pop(0) start_parse(macro) match = p_if.match(macro[0]) if match: for symbol in symbol_table:#print match.group(1) if match.group(1) == symbol: #print match.group(1) valid_area = True else: valid_area = False if p_else.match(macro[0]): macro.pop(0) start_parse(macro) match = p_endif.match(macro[0]) if match: valid_area = False macro.pop(0) start_parse(macro) if __name__ == '__main__': l = comment.splitlines() start_parse(l) print symbol_table -- CashFlow To be rich. -- http://mail.python.org/mailman/listinfo/python-list