Python for循环的控制流图

Python for循环的控制流图,python,loops,control-flow-graph,Python,Loops,Control Flow Graph,我正在开发一个python库,该库将python源代码转换为控制流图(CFG)。作为中间步骤,我已将源代码转换为xml表示 例如,以下源代码: def bar(): # line 32 a += 1 # line 33 for x in [1,2]: # line 34 print x # line 35 if x%2: # line 36 print x**2 # line 37 break # l

我正在开发一个python库,该库将python源代码转换为控制流图(CFG)。作为中间步骤,我已将源代码转换为xml表示

例如,以下源代码:

def bar(): # line 32
    a += 1 # line 33
    for x in [1,2]: # line 34
        print x # line 35
        if x%2: # line 36
            print x**2 # line 37
            break # line 38
        else: # line 39
            print x**3 # line 40
    else: # line 41
        print "wpp" # line 42
    print "done" # line 43
转换为以下xml:

<?xml version="1.0" ?>
<module>
:   0
:   <functiondef>
:   :   32
:   :   <augassign>
:   :   :   33
:   :   :   <name>33</name>
:   :   :   <add>add</add>
:   :   :   <num>33</num>
:   :   </augassign>
:   :   <for>
:   :   :   34
:   :   :   <print>
:   :   :   :   35
:   :   :   :   <name>35</name>
:   :   :   </print>
:   :   :   <if>
:   :   :   :   36
:   :   :   :   <binop>
:   :   :   :   :   36
:   :   :   :   :   <name>36</name>
:   :   :   :   :   <mod>mod</mod>
:   :   :   :   :   <num>36</num>
:   :   :   :   </binop>
:   :   :   :   <print>
:   :   :   :   :   37
:   :   :   :   :   <binop>
:   :   :   :   :   :   37
:   :   :   :   :   :   <name>37</name>
:   :   :   :   :   :   <pow>pow</pow>
:   :   :   :   :   :   <num>37</num>
:   :   :   :   :   </binop>
:   :   :   :   </print>
:   :   :   :   <break>38</break>
:   :   :   :   <else>
:   :   :   :   :   39
:   :   :   :   :   <print>
:   :   :   :   :   :   40
:   :   :   :   :   :   <binop>
:   :   :   :   :   :   :   40
:   :   :   :   :   :   :   <name>40</name>
:   :   :   :   :   :   :   <pow>pow</pow>
:   :   :   :   :   :   :   <num>40</num>
:   :   :   :   :   :   </binop>
:   :   :   :   :   </print>
:   :   :   :   </else>
:   :   :   </if>
:   :   :   <else>
:   :   :   :   41
:   :   :   :   <print>
:   :   :   :   :   42
:   :   :   :   :   <str>42</str>
:   :   :   :   </print>
:   :   :   </else>
:   :   </for>
:   :   <print>
:   :   :   43
:   :   :   <str>43</str>
:   :   </print>
:   </functiondef>
</module>
到目前为止,我已经处理了我见过的大多数案件。但是,我在将for循环的正确端连接回for循环的top/definition(第34行)时遇到了一些问题

如果有人能帮我找出for循环的哪些节点连接回for循环的顶部,我将不胜感激。在此示例中,只有一个这样的节点,即第40行上的节点

0 [32]
32 [33]
33 [34]
34 [35, 42]
35 [36]
36 [37, 40]
37 [38]
38 [43]
40 [34]
42 [43]
43 []