-
Notifications
You must be signed in to change notification settings - Fork 0
/
cftg2iwg.py
144 lines (109 loc) · 4.01 KB
/
cftg2iwg.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
import sys
class Tree(object):
def __init__(self):
self.label = None
self.children = []
def __str__(self):
children_str = ""
if len(self.children) > 0:
children_str = "("
for idx, child in enumerate(self.children):
if idx > 0:
children_str += ","
children_str += str(child)
children_str += ")"
return self.label + children_str
def parse_tree(tree):
# print("Parsing: " + tree)
root = Tree()
root.label = tree
if '(' in root.label:
root.label = root.label[:root.label.index('(')]
if "(" not in tree:
return root
tree = tree[tree.index('('):]
# print("Next: " + tree)
parenthesis = 0
element = ""
for l in tree:
if l == '(':
if parenthesis > 0:
element += l
parenthesis += 1
elif l == ')':
parenthesis -= 1
if parenthesis == 0:
root.children.append(parse_tree(element))
element = ""
else:
element += l
else:
if l == ',' and parenthesis == 1:
root.children.append(parse_tree(element))
element = ""
else:
element += l
return root
def parse_rule(rule):
parts = rule.split("->")
left_non_terminal = parts[0].strip()
if '(' in left_non_terminal:
left_non_terminal = left_non_terminal[:left_non_terminal.index('(')].strip()
right_tree = parse_tree(parts[1].strip())
return left_non_terminal, right_tree
def gorn_address(p, idx):
if p == "e":
return str(idx + 1)
else:
return p + "." + str(idx + 1)
def get_all_x_pairs(p, right):
if right.label[0] == 'x':
return [[p, int(right.label[1])-1]] # here we suppose that maximum x index is 9
pairs = []
for idx, child in enumerate(right.children):
pairs.extend(get_all_x_pairs(gorn_address(p, idx), child))
return pairs
def find_terminals(i, p, left, right, rules):
new_rules = []
common_left = "(" + str(i + 1) + "," + p + ")[] -> "
is_term = not right.label.istitle()
x_term = right.label[0] == 'x'
if len(right.children) > 0:
this_rule = ""
for idx, child in enumerate(right.children):
new_rules.extend(find_terminals(i, gorn_address(p, idx), left, child, rules))
this_rule += "(" + str(i + 1) + "," + gorn_address(p, idx) + ")[]"
if is_term and not x_term:
new_rules.append(common_left + this_rule)
elif is_term and not x_term:
new_rules.append("(" + str(i + 1) + "," + p + ")[] -> " + right.label)
if not is_term:
for j, pair in enumerate(rules):
if pair[0] == right.label:
new_rules.append(common_left + "(" + str(j + 1) + ",e)[(" + str(i + 1) + "," + p + ")]")
x_pairs = get_all_x_pairs("e", pair[1])
for x_pair in x_pairs:
new_rules.append(
"(" + str(j + 1) + "," + x_pair[0] + ")[("+ str(i+1)+ "," + p + ")] -> (" + str(i + 1) + "," + gorn_address(p, x_pair[1]) + ")[]")
return new_rules
def transform(i, left, right, rules):
new_rules = []
if left == "S":
new_rules.append("S[] -> (" + str(i + 1) + ",e)[]")
new_rules.extend(find_terminals(i, "e", left, right, rules))
return new_rules
# Read contents of the input file
rules = [line.rstrip('\n') for line in open(sys.argv[1])]
transformed_rules = []
parsed_rules = []
# parse each line as a rule
for idx, rule in enumerate(rules):
left, right = parse_rule(rule)
print(str(idx + 1) + ". " + left + " -> " + str(right))
parsed_rules.append([left, right])
for idx, pair in enumerate(parsed_rules):
transformed_rules.extend(transform(idx, pair[0], pair[1], parsed_rules))
print("Transformed:")
for idx, transformed_rule in enumerate(transformed_rules):
print(str(idx + 1) + ". " + transformed_rule)