forked from lv-jiajun/S2FVD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_data.py
More file actions
67 lines (56 loc) · 1.99 KB
/
Copy pathprepare_data.py
File metadata and controls
67 lines (56 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pycparser.c_ast
from pycparser import c_parser, c_ast
import pandas as pd
import os
import re
import sys
from gensim.models.word2vec import Word2Vec
import pickle
from tree import ASTNode, SingleNode
import numpy as np
def selectDef(node):
for _, child in node.children():
if isinstance(child, pycparser.c_ast.FuncDef):
return child
def selectFunDecl(node):
for _, child in node.children():
if isinstance(child, pycparser.c_ast.FuncDecl):
return child
def get_sequences(node, sequence):
current = SingleNode(node)
tokens = current.get_token()
sequence.append(current.get_token())
# print(type(node))
for _, child in node.children():
# if child.type is 'FunDef':
get_sequences(child, sequence)
if current.get_token().lower() == 'compound':
sequence.append('End')
def get_blocks(node, block_seq):
children = node.children()
name = node.__class__.__name__
if name in ['FuncDef', 'If', 'For', 'While', 'DoWhile']:
block_seq.append(ASTNode(node))
if name is not 'For':
skip = 1
else:
skip = len(children) - 1
for i in range(skip, len(children)):
child = children[i][1]
if child.__class__.__name__ not in ['FuncDef', 'If', 'For', 'While', 'DoWhile', 'Compound']:
block_seq.append(ASTNode(child))
get_blocks(child, block_seq)
elif name is 'Compound':
block_seq.append(ASTNode(name))
for _, child in node.children():
if child.__class__.__name__ not in ['If', 'For', 'While', 'DoWhile']:
block_seq.append(ASTNode(child))
get_blocks(child, block_seq)
block_seq.append(ASTNode('End'))
else:
for _, child in node.children():
get_blocks(child, block_seq)
def trans_to_sequences(ast):
sequence = []
get_sequences(ast, sequence)
return sequence