LPCC Assignment7
LPCC Assignment7
Assignment - 7
Title: Design suitable data structures & implement pass-I of a two-pass Macro processor
Firstly, a symbol table is essential to store information about macros encountered during Pass-I.
This table typically contains entries for each macro, including its name, parameters, and the
location of its definition in the source code. This allows for quick retrieval and manipulation of
macro information during processing.
Secondly, a macro definition table is necessary to store the expanded form of each macro
encountered in the source code. This table maps each macro name to its expanded sequence of
instructions. As macros can be defined multiple times in different parts of the source code, this
table ensures consistency in macro expansion throughout the program.
Thirdly, a macro call stack can be utilized to handle nested macro invocations. As macros may
contain other macros within their definitions, a stack-based approach allows for the proper
handling of nested expansions. Each time a macro is called, its parameters and expansion are
pushed onto the stack, and popped off once the expansion is complete.
Additionally, a location counter is maintained to keep track of the current position in the source
code being processed. This counter is incremented as instructions are read, allowing for accurate
determination of the location of macro invocations and expansions.
Finally, a suitable input buffer is required to efficiently read and process the source code. This
buffer stores a portion of the source code being analyzed, allowing for sequential scanning and
parsing during Pass-I. As the buffer is processed, macro invocations are identified and expanded,
and the resulting intermediate representation is generated for further processing in Pass-II.
import java.io.BufferedReader;
java.io.FileWriter; import
java.io.IOException; import
java.util.Iterator; import
java.util.LinkedHashMap; public class
Main { public static void
main(String[] args) throws
IOException{
BufferedReader br=new BufferedReader(new FileReader("macro_input.asm"));
mnt.write(parts[0]+"\t"+pp+"\t"+kp+"\t"+mdtp+"\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"+keywordParam[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"+mdtp+"\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
(part
s[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. :)"); }
Macro_input.asm
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:
From this assignment I understood about the data structures of pass-1 of a two pass macro
processor. I’ve learned the significance of efficient symbol, macro, and parameter tables in
managing macro processing tasks. In this assignment I have implemented data structures of pass1
of a two pass macro processor.