0% found this document useful (0 votes)
11 views

Macro Pass2

Uploaded by

sewwy.69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Macro Pass2

Uploaded by

sewwy.69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Problem Statement : Write a Java program for pass-II of a two-pass macro-processor.

The output of
assignment-3
(MNT, MDT and file without any macro definitions) should be input for this assignment.

import java.io.*;
import java.util.HashMap;
import java.util.Vector;

public class macroPass2 {


public static void main(String[] Args) throws IOException{
BufferedReader b1 = new BufferedReader(new FileReader("intermediate.txt"));
BufferedReader b2 = new BufferedReader(new FileReader("mnt.txt"));
BufferedReader b3 = new BufferedReader(new FileReader("mdt.txt"));
BufferedReader b4 = new BufferedReader(new FileReader("kpdt.txt"));
FileWriter f1 = new FileWriter("Pass2.txt");
HashMap<Integer,String> aptab=new HashMap<Integer,String>();
HashMap<String,Integer> aptabInverse=new HashMap<String,Integer>();
HashMap<String,Integer> mdtpHash=new HashMap<String,Integer>();
HashMap<String,Integer> kpdtpHash=new HashMap<String,Integer>();
HashMap<String,Integer> kpHash=new HashMap<String,Integer>();
HashMap<String,Integer> macroNameHash=new HashMap<String,Integer>();
Vector<String>mdt=new Vector<String>();
Vector<String>kpdt=new Vector<String>();
String s,s1;
int i,pp,kp,kpdtp,mdtp,paramNo;
while((s=b3.readLine())!=null)
mdt.addElement(s);
while((s=b4.readLine())!=null)
kpdt.addElement(s);
while((s=b2.readLine())!=null){
String word[]=s.split("\t");
s1=word[0]+word[1];
macroNameHash.put(word[0],1);
kpHash.put(s1,Integer.parseInt(word[2]));
mdtpHash.put(s1,Integer.parseInt(word[3]));
kpdtpHash.put(s1,Integer.parseInt(word[4]));
}
while((s=b1.readLine())!=null){
String b1Split[]=s.split("\\s");
if(macroNameHash.containsKey(b1Split[0])){
pp= b1Split[1].split(",").length-b1Split[1].split("=").length+1;
kp=kpHash.get(b1Split[0]+Integer.toString(pp));
mdtp=mdtpHash.get(b1Split[0]+Integer.toString(pp));
kpdtp=kpdtpHash.get(b1Split[0]+Integer.toString(pp));
String actualParams[]=b1Split[1].split(",");
paramNo=1;
for(int j=0;j<pp;j++){
aptab.put(paramNo, actualParams[paramNo-1]);
aptabInverse.put(actualParams[paramNo-1],paramNo);
paramNo++;
}
i=kpdtp-1;
for(int j=0;j<kp;j++){
String temp[]=kpdt.get(i).split("\t");
aptab.put(paramNo,temp[1]);
aptabInverse.put(temp[0],paramNo);
i++;
paramNo++;
}
i=pp+1;
while(i<=actualParams.length){
String initializedParams[]=actualParams[i-1].split("=");

aptab.put(aptabInverse.get(initializedParams[0].substring(1,initializedParams[0].length())),initializedPar
ams[1].substring(0,initializedParams[1].length()));
i++;
}
i=mdtp-1;
while(mdt.get(i).compareToIgnoreCase("MEND")!=0){
f1.write("+ ");
for(int j=0;j<mdt.get(i).length();j++){
if(mdt.get(i).charAt(j)=='#')
f1.write(aptab.get(Integer.parseInt("" +
mdt.get(i).charAt(++j))));
else
f1.write(mdt.get(i).charAt(j));
}
f1.write("\n");
i++;
}
aptab.clear();
aptabInverse.clear();
}
else
f1.write("+ "+s+"\n");
}
b1.close();
b2.close();
b3.close();
b4.close();
f1.close();
}
}

/*
OUTPUT:

Input.txt
MACRO
ADDITION
&arg1,&arg2,&arg3
MOV ax,&arg1
ADD ax,&arg2
ADD ax,&arg3
MEND
ADDITION 34,45,44
END

ALA
Macro Name | Actual Parameters
------------|-------------------
ADDITION | 34 -> arg1, 45 -> arg2, 44 -> arg3

Output.txt
MOV ax, 34 ; Load arg1 (34) into ax
ADD ax, 45 ; Add arg2 (45) to ax
ADD ax, 44 ; Add arg3 (44) to ax

You might also like