|
| 1 | +file = open("test.cpp",'r') |
| 2 | +file = file.read() |
| 3 | +file = file.split("\n") |
| 4 | +content=[""] |
| 5 | +for item in file: |
| 6 | + content.append(item) |
| 7 | +print(content) |
| 8 | + |
| 9 | +graph=[] |
| 10 | +for i in range(20): |
| 11 | + temp=[] |
| 12 | + for j in range(20): |
| 13 | + temp.append(0) |
| 14 | + graph.append(temp) |
| 15 | + |
| 16 | +#current line number |
| 17 | +line = 1 |
| 18 | + |
| 19 | +def func(start,end): |
| 20 | + global line |
| 21 | + while(start <= end): |
| 22 | + |
| 23 | + ############################################################# |
| 24 | + #if else case |
| 25 | + if('if' in content[start]): |
| 26 | + graph[start-1][start]=1 |
| 27 | + print("if detected") |
| 28 | + startOfIf=start |
| 29 | + startOfElif= -1 |
| 30 | + startOfElse= -1 |
| 31 | + endOfIf=0 |
| 32 | + endOfElif=[] |
| 33 | + endOfElse=-1 |
| 34 | + |
| 35 | + #find end of if else including elif and else |
| 36 | + temp = startOfIf+2 |
| 37 | + |
| 38 | + inIf=True |
| 39 | + inElse=False |
| 40 | + inElif=False |
| 41 | + stack=["{"] |
| 42 | + while(True): |
| 43 | + #####if statement |
| 44 | + if inIf: |
| 45 | + if len(stack)==0: |
| 46 | + inIf=False |
| 47 | + endOfIf=temp-1 |
| 48 | + func(startOfIf+1,endOfIf) |
| 49 | + stack.append("{") |
| 50 | + |
| 51 | + if "else if" in content[temp]: |
| 52 | + inElif=True |
| 53 | + graph[startOfIf][temp]=1 |
| 54 | + startOfElif=temp |
| 55 | + temp+=2 |
| 56 | + elif "else" in content[temp]: |
| 57 | + inElse=True |
| 58 | + startOfElse=temp |
| 59 | + graph[startOfIf][temp]=1 |
| 60 | + temp+=2 |
| 61 | + else: |
| 62 | + break |
| 63 | + |
| 64 | + elif "{" in content[temp]: |
| 65 | + stack.append("{") |
| 66 | + temp+=1 |
| 67 | + elif "}" in content[temp]: |
| 68 | + stack.pop() |
| 69 | + temp+=1 |
| 70 | + else: |
| 71 | + temp+=1 |
| 72 | + |
| 73 | + ##else if statement |
| 74 | + elif inElif: |
| 75 | + if len(stack)==0: |
| 76 | + inElif=False |
| 77 | + stack.append("{") |
| 78 | + func(startOfElif+1,temp-1) |
| 79 | + endOfElif.append(temp-1) |
| 80 | + if "else if" in content[temp]: |
| 81 | + inElif=True |
| 82 | + graph[startOfIf][temp]=1 |
| 83 | + startOfElif=temp |
| 84 | + temp+=2 |
| 85 | + elif "else" in content[temp]: |
| 86 | + startOfElse=temp |
| 87 | + graph[startOfIf][temp]=1 |
| 88 | + inElse=True |
| 89 | + temp+=2 |
| 90 | + else: |
| 91 | + break |
| 92 | + |
| 93 | + elif "{" in content[temp]: |
| 94 | + stack.append("{") |
| 95 | + temp+=1 |
| 96 | + elif "}" in content[temp]: |
| 97 | + stack.pop() |
| 98 | + temp+=1 |
| 99 | + else : |
| 100 | + temp+=1 |
| 101 | + |
| 102 | + ###else statement |
| 103 | + elif inElse: |
| 104 | + if len(stack)==0: |
| 105 | + inElse=False |
| 106 | + endOfElse=temp-1 |
| 107 | + func(startOfElse+1,endOfElse) |
| 108 | + break |
| 109 | + |
| 110 | + elif "{" in content[temp]: |
| 111 | + stack.append("{") |
| 112 | + temp+=1 |
| 113 | + elif "}" in content[temp]: |
| 114 | + stack.pop() |
| 115 | + temp+=1 |
| 116 | + else: |
| 117 | + temp+=1 |
| 118 | + |
| 119 | + |
| 120 | + graph[endOfIf][temp]=1 |
| 121 | + |
| 122 | + if startOfElse!=-1: |
| 123 | + graph[endOfElse][temp]=1 |
| 124 | + |
| 125 | + if len(endOfElif)!=0: |
| 126 | + for item in endOfElif: |
| 127 | + graph[item][temp]=1 |
| 128 | + |
| 129 | + start = temp |
| 130 | + continue |
| 131 | + |
| 132 | + ############################################################# |
| 133 | + #while case |
| 134 | + elif 'while' in content[start]: |
| 135 | + print("while_detected") |
| 136 | + startOfWhile=start |
| 137 | + endOfWhile=0 |
| 138 | + |
| 139 | + #find ending line of for loop |
| 140 | + stack=["{"] |
| 141 | + for i in range(startOfWhile+2,1000): |
| 142 | + if len(stack)==0: |
| 143 | + endOfWhile=i-1 |
| 144 | + break |
| 145 | + if "{" in content[i]: |
| 146 | + stack.append("{") |
| 147 | + elif "}" in content[i]: |
| 148 | + stack.pop() |
| 149 | + |
| 150 | + graph[start-1][start]=1 |
| 151 | + start+=1 |
| 152 | + func(start+1,endOfWhile) |
| 153 | + start-=1 |
| 154 | + graph[endOfWhile][startOfWhile]=1 |
| 155 | + continue |
| 156 | + |
| 157 | + ############################################################# |
| 158 | + #for case |
| 159 | + elif('for' in content[start]): |
| 160 | + print("for_detected") |
| 161 | + startOfFor=start |
| 162 | + endOfFor=0 |
| 163 | + |
| 164 | + #find ending line of for loop |
| 165 | + stack=["{"] |
| 166 | + for i in range(startOfFor+2,1000): |
| 167 | + if len(stack)==0: |
| 168 | + endOfFor=i-1 |
| 169 | + break |
| 170 | + if "{" in content[i]: |
| 171 | + stack.append("{") |
| 172 | + elif "}" in content[i]: |
| 173 | + stack.pop() |
| 174 | + |
| 175 | + graph[start-1][start]=1 |
| 176 | + start+=1 |
| 177 | + func(start+1,endOfFor) |
| 178 | + start-=1 |
| 179 | + graph[endOfFor][startOfFor]=1 |
| 180 | + continue |
| 181 | + |
| 182 | + ############################################################# |
| 183 | + #normal statement |
| 184 | + else: |
| 185 | + print("normal_statement detected") |
| 186 | + graph[start-1][start]=1 |
| 187 | + start+=1 |
| 188 | + continue |
| 189 | + ############################################################# |
| 190 | + |
| 191 | +func(1,19) |
| 192 | + |
| 193 | +for i in range(len(graph)): |
| 194 | + print (i," ",graph[i]) |
0 commit comments