Assignment No 2
Name : Pranav Jawale
Rollno : T21067
Div: A
Batch : A3
Practical 2: Design suitable data structures and implement Pass-I and Pass-II of a
twopass macro-processor. The output of Pass-I (MNT, MDT and intermediate code file
without any macrodefinitions) should be input for Pass-II.
Code:
Ou package macro; import java.util.*;
public class SimpleMacroProcessor {
// Macro Name Table: macro name -> index in MDT
HashMap<String, Integer> MNT = new HashMap<>();
// Macro Definition Table: stores macro body lines
ArrayList<String> MDT = new ArrayList<>();
// Intermediate code (source without macro
definitions) ArrayList<String> intermediateCode =
new ArrayList<>();
// Formal Arguments Table: macro name -> list of formal args
HashMap<String, List<String>> formalArgsTable = new
HashMap<>();
// Pass-I: Build MNT, MDT, Formal Args Table, generate intermediate code
public void passOne(List<String> source) { boolean inMacro = false;
String currentMacro = null; int mdtIndex
= 0;
for (String line :
source) { line =
line.trim();
if (line.equalsIgnoreCase("MACRO"))
{ inMacro = true; continue;
} if (inMacro)
{
if (currentMacro == null) {
// Parse macro header line: e.g. "INCR &ARG1, &ARG2"
String[] parts = line.split("\\s+", 2); currentMacro
= parts[0];
MNT.put(currentMacro,
mdtIndex); MDT.add(line);
mdtIndex+
+;
// Parse formal args list
List<String> formalArgs = new ArrayList<>();
if (parts.length > 1) {
String[] args = parts[1].split(","); for
(String arg : args)
{ formalArgs.add(arg.trim());
} }
formalArgsTable.put(currentMacro, formalArgs); }
else if (line.equalsIgnoreCase("ENDM")) {
MDT.add(line); mdtIndex+
+; inMacro = false;
currentMacro = null;
} else
{ MDT.add(line); mdtIndex++;
}
} else {
// Not in macro def, copy to intermediate code
intermediateCode.add(line);
}
}
}
// Pass-II: Expand macros in intermediate code using formal
args public List<String> passTwo()
{ List<String> output = new ArrayList<>();
for (String line : intermediateCode)
{ String[] parts = line.split("\\s+",
2);
String firstWord = parts[0]; if
(MNT.containsKey(firstWord)) { //
It's a macro call, expand it int
startIndex = MNT.get(firstWord);
String argsLine = (parts.length > 1) ? parts[1] : ""; String[]
actualArgs = argsLine.isEmpty() ? new String[0] :
argsLine.split(","); for (int i = 0; i < actualArgs.length; i+
+)
{ actualArgs[i] = actualArgs[i].trim();
}
List<String> formalArgs =
formalArgsTable.get(firstWord); int idx = startIndex
+ 1; // skip macro header line while (!
MDT.get(idx).equalsIgnoreCase("ENDM"))
{ String macroLine = MDT.get(idx);
// Replace each formal arg with actual arg in the line for
(int i = 0; i < formalArgs.size(); i++) {
String formal = formalArgs.get(i);
String actual = (i < actualArgs.length) ? actualArgs[i] : "";
macroLine = macroLine.replace(formal, actual);
}
output.add(macroLine)
; idx++; }
} else {
// Normal line, add as is output.add(line);
} } return output;
}
// Test the macroprocessor public static void
main(String[] args) {
SimpleMacroProcessor mp = new
SimpleMacroProcessor(); List<String> sourceCode =
Arrays.asList(
"MACRO",
"INCR &ARG1, &ARG2",
"LOAD &ARG1",
"ADD &ARG2",
"STORE
&ARG1",
"ENDM",
"START 100",
"INCR A, B",
"END" );
mp.passOne(sourceCode)
;
System.out.println("MNT (Macro Name Table): " +
mp.MNT); System.out.println("Formal Arguments Table:
" + mp.formalArgsTable);
System.out.println("\nMDT (Macro Definition
Table):"); for (String line : mp.MDT)
{ System.out.println(line);
}
System.out.println("\nIntermediate Code:"); for
(String line : mp.intermediateCode)
{ System.out.println(line);
}
List<String> expandedCode = mp.passTwo();
System.out.println("\nExpanded Code after Pass-
II:"); for (String line : expandedCode)
{ System.out.println(line);
} }
}
Output: