/
Assignment No 01
353 lines (276 loc) · 8.18 KB
/
Assignment No 01
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.lpcc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Assign1 {
public static class emot{
String Mnemonic;
int Class;
String Opcode;
emot(String Mnemonic, int Class, String Opcode){
this.Mnemonic=Mnemonic;
this.Class=Class;
this.Opcode=Opcode;
}
}
public static class symbolTable{
String symbol;
int loc;
symbolTable(String symbol, int loc){
this.symbol=symbol;
this.loc=loc;
}
@Override
public String toString() {
return "symbolTable [symbol=" + symbol + ", loc=" + loc + "]";
}
}
public static class literalTable{
String literal;
int loc;
literalTable(String literal, int loc){
this.literal=literal;
this.loc=loc;
}
@Override
public String toString() {
return "literalTable [literal=" + literal + ", loc=" + loc + "]";
}
}
public static void acceptALP() {
Scanner s=new Scanner(System.in);
String ALP="";
System.out.println("Enter 1 line of ALP: ");
ALP=s.nextLine();
//replace comma with space
ALP=ALP.replace(",", " ");
//split into substrings
String[] substr=ALP.split(" ");
//substrings count
int count=substr.length;
//print substrings
System.out.println("Substrings: ");
for(int i=0; i<substr.length; i++) {
System.out.println(substr[i]);
}
System.out.println();
//print count
System.out.println("Count of substrings: "+count);
}
public static void printALP() throws IOException {
//open file
File file=new File("a.txt");
//open for read mode
BufferedReader bf=new BufferedReader(new FileReader(file));
String s="";
int i=1;
//read string
while((s=bf.readLine())!=null) {
System.out.println(i+". "+s);
i++;
}
//close read mode
bf.close();
}
public static int findInEmot(String s, ArrayList<emot> emotTable) {
for(int i=0; i<emotTable.size(); i++) {
if(s.equals(emotTable.get(i).Mnemonic)) {
return 1;
}
}
return 0;
}
public static void generateSymbolTable(ArrayList<emot> emotTable) throws IOException{
ArrayList<symbolTable> symbolTable=new ArrayList<>();
//open file
File file=new File("a.txt");
//open for read mode
BufferedReader bf=new BufferedReader(new FileReader(file));
String s="";
int i=0;
int lc=0;
int start=0;
//read string
while((s=bf.readLine())!=null) {
if(i==0) {
s=s.replace(",", " ");
String[] sub=s.split(" ");
start=Integer.parseInt(sub[sub.length-1]);
}
else {
//update location counter
lc=start+i-1;
//split string
String[] sub=s.split(" ");
//check if emot table contains current symbol
int res=findInEmot(sub[0], emotTable);
if(res==0) {
//not found in emot table, means it is a symbol
symbolTable.add(new symbolTable(sub[0], lc));
}
}
i++;
}
//print symbol table
System.out.println("Symbol Location");
System.out.println("====================");
for(int j=0; j<symbolTable.size(); j++) {
System.out.println(symbolTable.get(j).symbol+" "+symbolTable.get(j).loc);
}
}
public static void generateLiteralTable1(ArrayList<emot> emotTable) throws IOException {
generateSymbolTable(emotTable);
System.out.println();
ArrayList<literalTable> literalTable=new ArrayList<>();
//open file
File file=new File("a.txt");
//open for read mode
BufferedReader bf=new BufferedReader(new FileReader(file));
int totalLines=Integer.parseInt((bf.lines().count()-1)+"");
bf=new BufferedReader(new FileReader(file));
String s="";
int i=0;
int lc=0;
int start=0;
//read string
while((s=bf.readLine())!=null) {
if(i==0) {
s=s.replace(",", " ");
String[] sub=s.split(" ");
start=Integer.parseInt(sub[sub.length-1]);
}
else {
//update location counter
lc=start+i-1;
//split string
s=s.replace(",", " ");
String[] sub=s.split(" ");
//check if current symbol is literal
if(sub[sub.length-1].startsWith("'") && sub[sub.length-1].endsWith("'")) {
String last=sub[sub.length-1].replace("'", "").replace("'", "");
int literal=Integer.parseInt(last);
//add into literal table
if(literalTable.size()==0) {
literalTable.add(new literalTable(literal+"", totalLines+start));
}
else {
int prevLoc=literalTable.get(literalTable.size()-1).loc;
literalTable.add(new literalTable(literal+"", prevLoc+1));
}
}
}
i++;
}
//print literalTable
System.out.println("Literal Location");
System.out.println("====================");
for(int j=0; j<literalTable.size(); j++) {
System.out.println(literalTable.get(j).literal+" "+literalTable.get(j).loc);
}
}
public static void generateLiteralTable2(ArrayList<emot> emotTable) throws IOException {
ArrayList<Integer> literalTable=new ArrayList<>();
//open file
File file=new File("p5.txt");
//open for read mode
BufferedReader bf=new BufferedReader(new FileReader(file));
String s="";
int i=0;
int lc=0;
int start=0;
//read string
while((s=bf.readLine())!=null) {
if(i==0) {
s=s.replace(",", " ");
String[] sub=s.split(" ");
start=Integer.parseInt(sub[sub.length-1]);
}
else {
//update location pointer
lc=start+i-1;
//split string
s=s.replace(",", " ");
String[] sub=s.split(" ");
//check if current symbol is literal
if(sub[sub.length-1].startsWith("='") && sub[sub.length-1].endsWith("'")) {
String last=sub[sub.length-1].replace("='", "").replace("'", "");
int literal=Integer.parseInt(last);
//add into literal table
literalTable.add(literal);
}
}
i++;
}
//print literalTable
System.out.println("Literal");
System.out.println("====================");
for(int j=0; j<literalTable.size(); j++) {
System.out.println(literalTable.get(j));
}
}
public static void main(String[] args) throws IOException {
ArrayList<emot> emotTable=new ArrayList<>();
emotTable.add(new emot("STOP", 1, "00"));
emotTable.add(new emot("ADD", 1, "01"));
emotTable.add(new emot("SUB", 1, "02"));
emotTable.add(new emot("MULT", 1, "03"));
emotTable.add(new emot("MOVER", 1, "04"));
emotTable.add(new emot("MOVEM", 1, "05"));
emotTable.add(new emot("COMP", 1, "06"));
emotTable.add(new emot("BC", 1, "07"));
emotTable.add(new emot("DIV", 1, "08"));
emotTable.add(new emot("READ", 1, "09"));
emotTable.add(new emot("PRINT", 1, "10"));
emotTable.add(new emot("START", 3, "01"));
emotTable.add(new emot("END", 3, "02"));
emotTable.add(new emot("ORIGIN", 3, "03"));
emotTable.add(new emot("EQU", 3, "04"));
emotTable.add(new emot("LTORG", 3, "05"));
emotTable.add(new emot("DS", 2, "01"));
emotTable.add(new emot("DC", 2, "02"));
emotTable.add(new emot("AREG", 4, "01"));
emotTable.add(new emot("BREG", 4, "02"));
emotTable.add(new emot("CREG", 4, "03"));
emotTable.add(new emot("EQ", 5, "01"));
emotTable.add(new emot("LT", 5, "02"));
emotTable.add(new emot("GT", 5, "03"));
emotTable.add(new emot("NE", 5, "04"));
emotTable.add(new emot("LE", 5, "05"));
emotTable.add(new emot("GT", 5, "06"));
emotTable.add(new emot("ANY", 5, "07"));
//que1
System.out.println("Question-1");
System.out.println("===========================");
System.out.println();
acceptALP();
System.out.println('\n');
//que2
System.out.println("Question-2");
System.out.println("===========================");
System.out.println();
printALP();
System.out.println('\n');
//que3
System.out.println("Question-3");
System.out.println("===========================");
System.out.println();
generateSymbolTable(emotTable);
System.out.println('\n');
//que4
System.out.println("Question-4");
System.out.println("===========================");
System.out.println();
generateLiteralTable1(emotTable);
System.out.println('\n');
//que5
System.out.println("Question-5");
System.out.println("===========================");
System.out.println();
generateLiteralTable2(emotTable);
System.out.println('\n');
}
}