You are on page 1of 3

Don Bosco Institute of Technology, Kurla(W)

Department of Computer Engineering


CSL601: System Programming and Compiler Construction Lab2022-23

Experiment No.
04

Experiment Title Design and Implement Pass 1 of a Macro processor

Task: Write a program to read a given text file which can identify
the macros in the program and display MNT, MDT and
Argument List Array.
Student Name Kaustubh Desale

Roll No. 13

Objectives: • Students will be able to learn and identify macro


definitionand macro name in a given program.

• Students will be able to understand the use of Macros in


a program
Theory /Algorithm: Procedure ExtractMacrosFromAssembly(assemblyFilePath):
Define a regular expression pattern for macro names
Define a regular expression pattern for macro definitions

Open the assembly file at assemblyFilePath for reading


Read the entire content of the assembly file into a string
Find all macro definitions in the content using the macro definition
pattern
For each macro definition found:
Extract the macro name and definition
Print the macro name and definition in a tabular format

Save the macro name to a file named "macro_[name]_name.txt"


Save the macro definition to a file named
"macro_[name]_definition.txt"

End For
End Procedure

Call ExtractMacrosFromAssembly with the path to the assembly file


Program Code: import re

def process_macros(file_path):
macro_name_table = {}
macro_definition_table = {}
argument_list_array = {}

with open(file_path, 'r') as file:


lines = file.readlines()
macro_flag = False
macro_name = ''
argument_list = []
macro_definition = []
for line in lines:
line = line.strip()

# Check if line contains a macro definition


if line.startswith('MACRO'):
macro_flag = True
macro_name = re.findall(r'\w+', line)[1]
argument_list = re.findall(r'\w+', line)[2:]
argument_list_array[macro_name] = argument_list
macro_definition = []

# Check if line contains a macro end


elif line.startswith('MEND'):
macro_flag = False
macro_definition_table[macro_name] = macro_definition

# If line is within macro definition


elif macro_flag:
macro_definition.append(line)

# If line contains a macro invocation


else:
for macro, arguments in argument_list_array.items():
if macro in line:
macro_invocation = re.findall(r'\b' + macro + r'\b', line)
macro_invocation_index =
line.index(macro_invocation[0])
arguments_values = line[macro_invocation_index +
len(macro_invocation[0]):].split(',')
arguments_values = [arg.strip() for arg in
arguments_values]

# Substitute arguments with their respective values


for i, arg in enumerate(arguments):
if arg in arguments_values:
line = line.replace(arg, arguments_values[i])

print(line) # Print the line after macro expansion

# Display MNT
print("\nMacro Name Table (MNT):")
print(macro_name_table)

# Display MDT
print("\nMacro Definition Table (MDT):")
print(macro_definition_table)

# Display Argument List Array


print("\nArgument List Array:")
print(argument_list_array)

# Example usage:
file_path = 'assembly_code.txt' # Specify the path to the assembly code
file
process_macros(file_path)

Input to the PG1 START


Program: MACRO
INCR &ARG1,&ARG2,&ARG3
A 1,&ARG1
A 2,&ARG2
A 3,&ARG3
MEND
INCR 5,3,8
END

macro_INCR_definition.txt

macro_INCR_name.txt

Output of the
program:

Outcome of the To extract macro names, definitions, and input parameters from an
Experiment: assembly program using Python, you can leverage the `re` module for
handling regular expressions. The script will read the assembly file,
identify macro definitions, and then print and save the macro names and
definitions in a tabular format. Each macro name and its corresponding
definition will be stored in separate files using the `open` function.
Furthermore, the script will manage the extraction of input parameters
for each macro, ensuring comprehensive handling of the macro
components.
References: https://elearn.dbit.in/course/view.php?id=162

Course in-charge-Mayura Gavhane

You might also like