From f048715b7ab64be5658b96a9d08276bb6b359bd1 Mon Sep 17 00:00:00 2001 From: TanmayMachkar Date: Sun, 13 Oct 2024 12:33:05 +0530 Subject: [PATCH] added 2 pass macro code --- Codes/Group A/Assignment - 2/2 Pass Macro.py | 111 +++++++++++++++++++ README.md | 3 + 2 files changed, 114 insertions(+) create mode 100644 Codes/Group A/Assignment - 2/2 Pass Macro.py diff --git a/Codes/Group A/Assignment - 2/2 Pass Macro.py b/Codes/Group A/Assignment - 2/2 Pass Macro.py new file mode 100644 index 0000000..db9de40 --- /dev/null +++ b/Codes/Group A/Assignment - 2/2 Pass Macro.py @@ -0,0 +1,111 @@ +class MacroProcessor: + def __init__(self): + self.macro_name_table = {} + self.macro_definition_table = {} + self.mdt_index = 0 + + def process_pass1(self, source_code): + inside_macro = False + current_macro_name = None + macro_definition = [] + + for line in source_code: + tokens = line.strip().split() + + if not tokens: + continue + + if tokens[0] == 'MACRO': + inside_macro = True + continue + + if inside_macro and tokens[0] == 'MEND': + inside_macro = False + self.macro_definition_table[current_macro_name] = macro_definition[:] + self.macro_name_table[current_macro_name] = self.mdt_index + self.mdt_index += len(macro_definition) + macro_definition = [] + current_macro_name = None + continue + + if inside_macro: + if not current_macro_name: + current_macro_name = tokens[0] + macro_definition.append(line.strip()) + else: + pass + + def process_pass2(self, source_code): + output = [] + inside_macro = False + + for line in source_code: + tokens = line.strip().split() + + if not tokens: + continue + + if tokens[0] == 'MACRO': + inside_macro = True + continue + elif tokens[0] == 'MEND': + inside_macro = False + continue + + if inside_macro: + continue + + macro_name = tokens[0] + if macro_name in self.macro_name_table: + macro_def = self.macro_definition_table[macro_name] + args = tokens[1:] + output.extend(self.expand_macro(macro_def, args)) + else: + output.append(line.strip()) + + return output + + def expand_macro(self, macro_def, args): + expanded_lines = [] + for line in macro_def: + for i, arg in enumerate(args): + line = line.replace(f"&ARG{i+1}", arg) + expanded_lines.append(line) + return expanded_lines + + def display_tables(self): + print("Macro Name Table (MNT):") + for name, index in self.macro_name_table.items(): + print(f"Macro Name: {name}, MDT Index: {index}") + + print("\nMacro Definition Table (MDT):") + for name, definition in self.macro_definition_table.items(): + print(f"Macro Name: {name}") + for line in definition: + print(f"\t{line}") + +source_code = [ + "MACRO", + "INCR &ARG1", + "ADD &ARG1, ONE", + "MEND", + "MACRO", + "DECR &ARG1", + "SUB &ARG1, ONE", + "MEND", + "START", + "INCR A", + "DECR B", + "END", +] + +macro_processor = MacroProcessor() + +macro_processor.process_pass1(source_code) +macro_processor.display_tables() + +expanded_code = macro_processor.process_pass2(source_code) + +print("\nExpanded Source Code (Pass 2):") +for line in expanded_code: + print(line) diff --git a/README.md b/README.md index ccd9e6b..ed86480 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,9 @@ This repository serves as a comprehensive resource for the Systems Programming a ## Index ### Codes +##### Group A +2. [Pass 1 and Pass 2 of 2-Pass Macroprocessor] + ##### Group B 5. [CPU Scheduling Algorithms: FCFS, SJF (Preemptive), Priority (Non-Preemptive) and Round Robin (Preemptive)](https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20B/Assignment%20-%205) - [FCFS (Non-Preemptive)](https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20B/Assignment%20-%205/FCFS%20%28Non-Preemptive%29.cpp)