Skip to content

Instantly share code, notes, and snippets.

@Vivek-abstract
Created April 9, 2018 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vivek-abstract/a6e022e4d10f6b89d511cc9bab0e5c74 to your computer and use it in GitHub Desktop.
Save Vivek-abstract/a6e022e4d10f6b89d511cc9bab0e5c74 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class MacroPass1 {
public static void main(String[] args) {
String[][] replacements = {{"&ARG0", "#0"},
{"&ARG1", "#1"},
{"&ARG2", "#2"},
{"&ARG3", "#3"}};
String line=null;
try {
FileReader fileReader = new FileReader("prog.txt");
BufferedReader fr = new BufferedReader(fileReader);
int t=1;
String[] mdt = new String[100];
String[] mnt = new String[100];
String[] ala = new String[100];
String elements[] = new String[100];
int mdtc = 0, mntc = 0, flag = 0;
while((line = fr.readLine()) != null)
{
if(line.equals("MACRO")) {
do {
line = fr.readLine();
if(flag == 0) {
elements= line.split("[ ,]");
mnt[mntc++] = elements[0] + " " + mdtc;
flag = 1;
}
if(line != null) {
for(String[] replacement: replacements) {
line = line.replace(replacement[0], replacement[1]);
}
mdt[mdtc++] = line;
} else {
break;
}
} while(!line.equals("MEND"));
}
flag = 0;
}
System.out.println("\n***** MDT: *****\n");
for(int i = 0; i < mdtc; i++) {
System.out.println(i + "\t" + mdt[i]);
}
System.out.println("\n***** MNT: *****\n");
for(int i = 0; i < mntc; i++) {
System.out.println(i + "\t" + mnt[i]);
}
System.out.println("\n***** ALA *****\n");
for(int i = 1; i < elements.length; i++) {
System.out.println("#" + (i-1) + "\t" + elements[i]);
}
fr.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
/*
INPUT: prog.txt:
MACRO
INCR &ARG1,&ARG2,&ARG3
A 1,&ARG1
A 2,&ARG2
A 3,&ARG3
MEND
INCR DATA1,DATA2,DATA3
OUTPUT:
***** MDT: *****
0 INCR #1,#2,#3
1 A 1,#1
2 A 2,#2
3 A 3,#3
4 MEND
***** MNT: *****
0 INCR 0
***** ALA *****
#0 &ARG1
#1 &ARG2
#2 &ARG3
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment