-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.py
211 lines (129 loc) · 4.1 KB
/
1.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# -*- coding: utf-8 -*-
with open("aa.txt","r") as file:
line=file.readlines()
file.close()
import re
list1=[]
count=0
r=[]
address=""
lc=[]
lc.append(0)
symbol=[]
sym=[]
for i in line:
r=re.split("\s*",i)
list1.append(r)
print(list1)
for i in range(0,len(list1)):
if(list1[1][2] == "*"):
address="R"
else:
address="A"
for i in range(0, len(list1)):
if((list1[i][1] == "start") or (list1[i][1] == "using")):
count=0
lc.append(count)
elif(list1[i][0] == "-"):
if((list1[i][2].isdigit()) and (list1[i][3].isdigit()) ):
count+=2
lc.append(count)
else:
count+=4
lc.append(count)
elif(list1[i][0] != "-"):
if(list1[i][2] == "b" or list1[i][3] == "b"):
count+=1
lc.append(count)
elif(list1[i][2] == "h" or list1[i][3] == "h"):
count+=2
lc.append(count)
elif(list1[i][2] == "f" or list1[i][3] == "f"):
count+=4
lc.append(count)
elif(list1[i][2] == "d" or list1[i][3] == "d"):
count+=8
lc.append(count)
if(list1[i][0]!="-"):
if(i==0):
symbol.append(list1[i][0])
symbol.append(str(lc[-2]))
symbol.append(str(1))
if(list1[1][2]=="*"):
symbol.append('R')
else:
symbol.append("A")
else:
symbol.append(list1[i][0])
symbol.append(str(lc[-2]))
symbol.append(str(lc[-1]-lc[-2]))
if(list1[1][2]=="*"):
symbol.append('R')
else:
symbol.append("A")
file1=open("symboltable.txt","w+")
file1.write("********SYMBOL TABLE**********\n\n")
file1.write("S\t\tV\t\tL\t\tR/A\n")
final = [symbol[i * 4:(i + 1) * 4] for i in range((len(symbol) + 4 - 1) // 4 )]
print(final)
for i in final:
file1.write(str(i))
file1.write("\n")
file1.close()
file2=open("pass1.txt","w+")
file2.write("************PASS 1****************\n\n")
for i in range(0,len(list1)):
file2.write(str(lc[i]))
file2.write("\t\t")
if(list1[i][1] == "dc"):
file2.write(str(list1[i][3]))
file2.write('\n')
elif(list1[i][1] == "ds"):
file2.write('\n')
elif(list1[i][1] == "start" or list1[i][1] == "using"):
file2.write('\n')
elif(list1[i][3].isalpha()):
file2.write(str(list1[i][1]))
file2.write('\t\t')
file2.write(str(list1[i][2]))
file2.write('\n')
else:
file2.write(str(list1[i][1]))
file2.write('\n')
"""
Created on Sun Feb 2 16:28:20 2020
@author: Gaurav Kanojia
for line in file:
r=re.split(r"[\s*\t\n]",line)
for line in file:
for letter in r:
print(r)
if ("using" or "*") in line:
continue
if str in r:
count+=4
continue
if "f,h,d,b" in line:
count+=4
"""
"""
#print(count)
keyword=["start","using","end"]
statements=["st","l","a"]
att=["dc","ds"]
count=0
r=[]
#import re
i=0
List=[""]
lines_list = open('aa.txt').read().splitlines()
print(lines_list)
#import string
with open("aa.txt","r") as file:
with open("out.txt","w") as file1:
for line in lines_list:
for letter in lines_list:
if letter in att:
count+=4
print(count)
"""