Program Code:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
class TableRow {
String symbol;
int address;
int index;
public TableRow(String symbol, int address) {
super();
[Link] = symbol;
[Link] = address;
public TableRow(String symbol, int address, int index) {
super();
[Link] = symbol;
[Link] = address;
[Link] = index;
public int getIndex() {
return index;
public void setIndex(int index) {
[Link] = index;
public String getSymbol() {
return symbol;
public void setSymbol(String symbol) {
[Link] = symbol;
public int getAddress() {
return address;
public void setAddress(int address) {
[Link] = address;
public class Pass_2 {
ArrayList<TableRow> SYMTAB, LITTAB;
public Pass_2() {
SYMTAB = new ArrayList<>();
LITTAB = new ArrayList<>();
public static void main(String[] args) {
Pass_2 pass2 = new Pass_2();
try {
[Link]("[Link]");
[Link]("IC");
pass2.printPass2Output("[Link]");
[Link]("Literal table");
pass2.printPass2Output("[Link]");
[Link]("Symbol table");
pass2.printPass2Output("[Link]");
[Link]("Machine Code");
pass2.printPass2Output("[Link]"); // Print [Link] content after generation
} catch (Exception e) {
[Link]();
public void readtables() {
BufferedReader br;
String line;
try {
br = new BufferedReader(new FileReader("[Link]"));
while ((line = [Link]()) != null) {
String parts[] = [Link]("\\s+");
[Link](new TableRow(parts[1], [Link](parts[2]), [Link](parts[0])));
[Link]();
br = new BufferedReader(new FileReader("[Link]"));
while ((line = [Link]()) != null) {
String parts[] = [Link]("\\s+");
[Link](new TableRow(parts[1], [Link](parts[2]), [Link](parts[0])));
[Link]();
} catch (Exception e) {
[Link]([Link]());
public void generateCode(String filename) throws IOException {
readtables();
BufferedReader br = new BufferedReader(new FileReader(filename));
BufferedWriter bw = new BufferedWriter(new FileWriter("[Link]"));
String line, code;
while ((line = [Link]()) != null) {
String parts[] = [Link]("\\s+");
if (parts[0].contains("AD") || parts[0].contains("DL,02")) {
[Link]("\n");
continue;
} else if ([Link] == 2) {
if (parts[0].contains("DL")) {
parts[0] = parts[0].replaceAll("[^0-9]", "");
if ([Link](parts[0]) == 1) {
int constant = [Link](parts[1].replaceAll("[^0-9]", ""));
code = "00\t0\t" + [Link]("%03d", constant) + "\n";
[Link](code);
} else if (parts[0].contains("IS")) {
int opcode = [Link](parts[0].replaceAll("[^0-9]", ""));
if (opcode == 10) {
if (parts[1].contains("S")) {
int symIndex = [Link](parts[1].replaceAll("[^0-9]", ""));
code = [Link]("%02d", opcode) + "\t0\t"
+ [Link]("%03d", [Link](symIndex - 1).getAddress()) + "\n";
[Link](code);
} else if (parts[1].contains("L")) {
int symIndex = [Link](parts[1].replaceAll("[^0-9]", ""));
code = [Link]("%02d", opcode) + "\t0\t"
+ [Link]("%03d", [Link](symIndex - 1).getAddress()) + "\n";
[Link](code);
} else if ([Link] == 1 && parts[0].contains("IS")) {
int opcode = [Link](parts[0].replaceAll("[^0-9]", ""));
code = [Link]("%02d", opcode) + "\t0\t" + [Link]("%03d", 0) + "\n";
[Link](code);
} else if (parts[0].contains("IS") && [Link] == 3) // All OTHER IS INSTR
int opcode = [Link](parts[0].replaceAll("[^0-9]", ""));
int regcode = [Link](parts[1]);
if (parts[2].contains("S")) {
int symIndex = [Link](parts[2].replaceAll("[^0-9]", ""));
code = [Link]("%02d", opcode) + "\t" + regcode + "\t"
+ [Link]("%03d", [Link](symIndex - 1).getAddress()) + "\n";
[Link](code);
} else if (parts[2].contains("L")) {
int symIndex = [Link](parts[2].replaceAll("[^0-9]", ""));
code = [Link]("%02d", opcode) + "\t" + regcode + "\t"
+ [Link]("%03d", [Link](symIndex - 1).getAddress()) + "\n";
[Link](code);
[Link]();
[Link]();
public void printPass2Output(String filename) {
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
while ((line = [Link]()) != null) {
[Link](line);
[Link]();
} catch (IOException e) {
[Link]();
-------------------------------------------------------------------
OUTPUT:
IC
(AD,01) (C,100)
(IS,04) 1 (L,1)
(IS,05) 2 (S,02)
(IS,01) 1 (L,2)
(DL,01) (C,5)
(DL,01) (C,2)
(IS,04) 1 (S,03)
(DL,01) (C,5)
(DL,02) (C,2)
(AD,02)
Literal table
1 5 104
2 2 105
Symbol table
1 L1 100
2 X 106
3 Y 107
Machine Code
04 1 104
05 2 106
01 1 105
00 0 005
00 0 002
04 1 107
00 0 005