my recursive function call is wrong?

Chang Min Jeon jcm1981 at gmail.com
Sun Aug 16 09:57:41 CEST 2009


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090816/8a05ac1a/attachment.html>


More information about the Python-list mailing list