Java Code For 2-Pass MacroProcessor
















Pass 1 Of 2-Pass MacroProcessor:-

import java.io.*;
import java.util.*;
class Macro
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);
String mn[]=new String[50];
String md[]=new String[50];
int mdti[]=new int[50];
String ala[]=new String[50];
int c=0,a=0;
System.out.println("Enter the name of file with format");
String f=src.next();
        try
{
        FileReader fr=new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
        String x;
while((x=br.readLine())!=null)
{
if(x.equals("MACRO"))
{
x=br.readLine();
int i = x.indexOf(" ");
mn[c]=x.substring(0,i);
mdti[c]=c+1;
ala[a++]=x.substring(i+1,x.length());
md[c]="";
while(!x.equals("MEND"))
{
md[c]=md[c]+x+'\n'+"\t";
x=br.readLine();
}
md[c]+="MEND\n";
c++;
}
}

}
catch(Exception e){ System.out.println(e);}

System.out.println("MNT");
System.out.println("Index\tMacroname\tMDT index");
for(int j=0;j<c;j++)
{
System.out.println((j+1)+"\t"+mn[j]+"\t\t"+mdti[j]);
}
System.out.println();

System.out.println("MDT");
System.out.println("Index\tMacrodefinition");
for(int j=0;j<c;j++)
{
System.out.print((j+1)+"\t"+md[j]);
}
System.out.println();
System.out.println("ALA with formal parameters");
System.out.println("Index\tArguments");
for(int j=0;j<a;j++)
{
System.out.println((j+1)+"\t"+ala[j]);
}
System.out.println();
}
}


Pass 2 Of 2-Pass MacroProcessor:-
import java.io.*; import java.util.*; class Macro2 { public static void main(String args[]) { Scanner src=new Scanner(System.in); String mn[]=new String[50]; String md[]=new String[50]; int mdti[]=new int[50]; String u,t=""; String ala[]=new String[50]; String ala1[]=new String[50]; int c=0,a=0,k=0; System.out.println("Enter the name of file with format"); String f=src.next(); try { FileReader fr=new FileReader(f); BufferedReader br = new BufferedReader(fr); String x,y; boolean flag=false; while((x=br.readLine())!=null) { if(x.equals("MACRO")) { x=br.readLine(); int i = x.indexOf(" "),j; mn[c]=x.substring(0,i); mdti[c]=c+1; ala1[k++]=x.substring(i+1,x.length()); md[c]=""; while(!x.equals("MEND")) { md[c]=md[c]+x+'\n'+"\t"; x=br.readLine(); } md[c]+="MEND\n"; c++; } if(x.contains("START"))flag=true; if(flag==true) { for(int j=0;j<c;j++) { y=mn[j]; if(x.contains(y)) { int i = x.indexOf(" "); ala[a++]=x.substring(i+1,x.length()); break; } } } } } catch(IOException e){ System.out.println(e);} System.out.println("MNT"); System.out.println("Index\tMacroname\tMDT index"); for(int j=0;j<c;j++) { System.out.println((j+1)+"\t"+mn[j]+"\t\t"+mdti[j]); } System.out.println(); System.out.println("MDT"); System.out.println("Index\tMacrodefinition"); for(int j=0;j<c;j++) { System.out.print((j+1)+"\t"+md[j]); } System.out.println(); System.out.println("ALA with actual parameters"); System.out.println("Index\tArguments"); for(int j=0;j<a;j++) { System.out.println((j+1)+"\t"+ala[j]); } System.out.println("\nESC\n"); try { FileReader free=new FileReader(f); BufferedReader b = new BufferedReader(free); String w; boolean zenda=false; more : while((w=b.readLine())!=null) { int i =0,k1=0; if(w.contains("START")) zenda=true; if(zenda==true) { for(int j=0;j<c;j++) { if(w.contains(mn[j])) { u = md[j].substring(md[j].indexOf("\n"),md[j].indexOf("MEND")); for(int l=0;l<a;l++) { if(u.contains(ala1[l])) { t=""; while((i=u.indexOf(ala1[l],k1))!=-1) { t = t + u.substring(k1,i)+ala[l]; k1=u.indexOf("\n", i); } break; } } System.out.println(t); continue more; } } } System.out.println(w); } } catch(Exception e){} } }




4 comments :