SPCC
SPCC
Experiment no. 5
Theory :-
A macro processor is a program that copies a stream of text from one place to another,
making a systematic set of replacements as it does so. Macro processors are often
embedded in other programs, such as assemblers and compilers. Sometimes they are
standalone programs that can be used to process any kind of text.
Macro processors have been used for language expansion (defining new language
constructs that can be expressed in terms of existing language components), for systematic
text replacements that require decision making, and for text reformatting.
It is used for for identifying the macro name and performing expansion. Features of macro
processor: Recognized the macro definition. Save macro definition. Recognized the macro
call.
1. PASS 1-
1. PASS 2-
import
java.io.Buff
eredReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
FileWriter mnt=new
FileWriter("mnt.txt");
FileWriter mdt=new
FileWriter("mdt.txt");
FileWriter kpdt=new
FileWriter("kpdt.txt");
FileWriter pnt=new
FileWriter("pntab.txt");
FileWriter ir=new
FileWriter("intermediate.txt");
LinkedHashMap<String, Integer>
pntab=new LinkedHashMap<>();
String line;
String Macroname = null;
int
mdtp=1,kpdtp=0,paramNo=1,pp=0,kp=0,flag=0;
while((line=br.readLine())!=null)
{
String
parts[]=line.split("\\s+");
if(parts[0].equalsIgnoreCase("MACRO"))
{
flag=1;
line=br.readLine();
parts=line.split("\\s+");
Macroname=parts[0];
if(parts.length<=1)
{
mnt.write(parts[0]+"\t"+pp+"\t"+kp+"\t"+md
tp+"\t"+(kp==0?kpdtp:(kpdtp+1))+"\n");
continue;
}
for(int
i=1;i<parts.length;i++) //processing of
parameters
{
parts[i]=parts[i].replaceAll("[&,]", "");
//System.out.println(parts[i]);
if(parts[i].contains("="))
{
++kp;
String
keywordParam[]=parts[i].split("=");
pntab.put(keywordParam[0], paramNo++);
if(keywordParam.length==2)
{
kpdt.write(keywordParam[0]+"\t"+keywordPar
am[1]+"\n");
}
else
{
kpdt.write(keywordParam[0]+"\t-\n");
}
}
else
{
pntab.put(parts[i], paramNo++);
pp++;
}
}
mnt.write(parts[0]+"\t"+pp+"\t"+kp+"\t"+md
tp+"\t"+(kp==0?kpdtp:(kpdtp+1))+"\n");
kpdtp=kpdtp+kp;
//System.out.println("KP="+kp);
}
else
if(parts[0].equalsIgnoreCase("MEND"))
{
mdt.write(line+"\n");
flag=kp=pp=0;
mdtp++;
paramNo=1;
pnt.write(Macroname+":\t");
Iterator<String>
itr=pntab.keySet().iterator();
while(itr.hasNext())
{
pnt.write(itr.next()+"\t");
}
pnt.write("\n");
pntab.clear();
}
else if(flag==1)
{
for(int
i=0;i<parts.length;i++)
{
if(parts[i].contains("&"))
{
parts[i]=parts[i].replaceAll("[&,]", "");
mdt.write("(P,"+pntab.get(parts[i])+")\t")
;
}
else
{
mdt.write(parts[i]+"\t");
}
}
mdt.write("\n");
mdtp++;
}
else
{
ir.write(line+"\n");
}
}
br.close();
mdt.close();
mnt.close();
ir.close();
pnt.close();
kpdt.close();
System.out.println("MAcro PAss1
Processing done. :)");
}
}
INPUT FILE :-
MACRO
M1 &X, &Y, &A=AREG, &B=
MOVER &A, &X
ADD &A, ='1'
MOVER &B, &Y
ADD &B, ='5'
MEND
MACRO
M2 &P, &Q, &U=CREG, &V=DREG
MOVER &U, &P
MOVER &V, &Q
ADD &U, ='15'
ADD &V, ='10'
MEND
START 100
M1 10, 20, &B=CREG
M2 100, 200, &V=AREG, &U=BREG
END
Output :-
Conclusion :- Hence implementation of Macroprocessor is successfully executed.