mnt
import java.util.*;
import java.io.*;
class MacroEntry {
String name;
int parameterCount;
int mdtIndex;
List<String> parameters;
public MacroEntry(String name, int parameterCount, int mdtIndex) {
this.name = name;
this.parameterCount = parameterCount;
this.mdtIndex = mdtIndex;
this.parameters = new ArrayList<>();
}
}
public class MacroProcessorMNT {
private List<String> inputLines = new ArrayList<>();
private List<String> mdt = new ArrayList<>();
private List<MacroEntry> mnt = new ArrayList<>();
private int currentLine = 0;
private int mdtCounter = 1; // MDT index starts at 1
public static void main(String[] args) {
MacroProcessorMNT processor = new MacroProcessorMNT();
processor.readInputFromTerminal();
processor.passOne();
processor.printMNT();
processor.printMDT();
}
public void readInputFromTerminal() {
System.out.println("Enter your assembly code (type 'END' on a new line to finish):");
Scanner scanner = new Scanner(System.in);
while (true) {
String line = scanner.nextLine().trim();
if (line.equals("END")) {
inputLines.add(line);
break;
}
if (!line.isEmpty()) {
inputLines.add(line);
}
}
}
public void passOne() {
System.out.println("\nProcessing Macro Definitions...");
while (currentLine < inputLines.size()) {
String line = inputLines.get(currentLine);
if (line.startsWith("MACRO")) {
processMacroDefinition();
} else {
currentLine++;
}
}
}
private void processMacroDefinition() {
String macroDecl = inputLines.get(currentLine);
String[] parts = macroDecl.split("\\s+");
String macroName = parts[1];
// Create new macro entry
MacroEntry macro = new MacroEntry(macroName, parts.length - 2, mdtCounter);
// Process parameters
if (parts.length > 2) {
for (int i = 2; i < parts.length; i++) {
String param = parts[i].replace(",", "");
macro.parameters.add(param);
}
}
mnt.add(macro);
currentLine++;
// Process macro body and add to MDT
while (currentLine < inputLines.size()) {
String line = inputLines.get(currentLine);
if (line.equals("MEND")) {
mdt.add("MEND");
mdtCounter++;
break;
}
mdt.add(line);
mdtCounter++;
currentLine++;
}
currentLine++; // Skip MEND line
}
public void printMNT() {
System.out.println("\n=== MACRO NAME TABLE (MNT) ===");
System.out.printf("%-10s %-15s %-20s %-30s%n",
"MDT Index", "Macro Name", "Parameter Count", "Parameters");
System.out.println("----------------------------------------------------------------");
for (MacroEntry entry : mnt) {
System.out.printf("%-10d %-15s %-20d %-30s%n",
entry.mdtIndex,
entry.name,
entry.parameterCount,
String.join(", ", entry.parameters));
}
}
public void printMDT() {
System.out.println("\n=== MACRO DEFINITION TABLE (MDT) ===");
System.out.printf("%-5s %s%n", "Index", "Definition");
System.out.println("----------------------------");
for (int i = 0; i < mdt.size(); i++) {
System.out.printf("%-5d %s%n", i + 1, mdt.get(i));
}
}
}
#ip
MACRO INCR &ARG
LOAD &ARG
ADD 1
STORE &ARG
MEND
MACRO SWAP &X, &Y
LOAD &X
STORE TEMP
LOAD &Y
STORE &X
LOAD TEMP
STORE &Y
MEND
END
Comments
Post a Comment