0% found this document useful (0 votes)
82 views15 pages

SPCC

A macro processor is a program that copies text while making systematic replacements. It identifies macro definitions and calls, saving definitions for later expansion of calls by replacing parameters. The code implements a macro processor in two passes, with the first recognizing and saving definitions, and the second expanding calls by substituting parameters. Various files track macro names, definitions, parameters, and the expanded output, demonstrating the macro processing functionality.

Uploaded by

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

SPCC

A macro processor is a program that copies text while making systematic replacements. It identifies macro definitions and calls, saving definitions for later expansion of calls by replacing parameters. The code implements a macro processor in two passes, with the first recognizing and saving definitions, and the second expanding calls by substituting parameters. Various files track macro names, definitions, parameters, and the expanded output, demonstrating the macro processing functionality.

Uploaded by

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

Pranjali Patil TE-B SUB : SPCC ROLL NO – 86

Experiment no. 5

Aim :- Program to implement Macroprocessor.

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.

Features of macro processor:


1. Recognized the macro definition
2. Save macro definition
3. Recognized the macro call
4. Perform macro expansion

Macro Assembler : It performs expansion of each macro call in a program into a


sequence of assembly language statements and also assembles the resultant assembly
language program.
Macro pre-processor: It only processes the macro call . Other statements are processes
with the help of assembler a macro pre-processor merely performs expansion of macro in
program. It produces an assembly program in which a macro call has been replaced by
statements that resulted from its expansion but statements that were not macro calls have
been retained in their original from . This program can be assembled by using assembler.
Forward reference Problem
The assembler specifies that the macro definition should occur anywhere in the program .
So there can be chances of macro call before it’s definition witch gives rise to the forwards
reference problem od macro
Due to witch macro is divided into two passes

1. PASS 1-

Recognize macro definition save macro definition

1. PASS 2-

Recognize macro call perform macro expansion


Databases required for pass 2
In pass2 we perform recognize macro call and perform macro expansion
1.COPY FILE
It is a file it contains the out put given from PASS1
2.MNT
It is used for recognizing macro name
3.MDT
It is used to perform macro EXPANSION
4.MDTP
It is used to point to the index of MDT .
The starting index is given by MNT
5.ALA
It is used to replace the index notation by it actual value
6.ESC
It is used to contain the expanded macro call which is given to the assembler for further
processing.
Each macro invocation statement has been expanded into the statements that form the body of
the macro, with the arguments from the macro invocation substituted for the parameters in the
macro prototype. Parameters and arguments, both are associated with one another according to
their positions. After macro processing, the expanded file can be used as input to the assembler.
Code :-

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;

public class MacroP1 {

public static void main(String[] args)


throws IOException{
BufferedReader br=new
BufferedReader(new
FileReader("macro_input.asm"));

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.

You might also like