0% found this document useful (0 votes)
15 views5 pages

Exp 8 SPCC

Compiler construction and System programming

Uploaded by

work.mihirkate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Exp 8 SPCC

Compiler construction and System programming

Uploaded by

work.mihirkate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

08
Date:
Roll No:

Aim: Write a C program to generate a Macro name table .


Theory:
Macro instruction is a notational convenience for the programmer. For every occurrence of
macro, the whole macro body or macroblock of statements gets expanded in the main source
code. Thus Macro instructions make writing code more convenient.
macro definition may be written using formal parameters . A macro call on such a macro
specifies actual parameters.
Salient features of Macro Processor:
● Macro represents a group of commonly used statements in the source programming
language.
● Macro Processor replaces each macro instruction with the corresponding group of
source language statements. This is known as the expansion of macros.
● Using Macro instructions programmers can leave the mechanical details to be handled
by the macro processor.
● Macro Processor designs are not directly related to the computer architecture on
which it runs.
● Macro Processor involves definition, invocation, and expansion.
Macro Expansion can be performed by using two kinds of language processors and when we
use a macro name with a set of actual parameters it is replaced by the same code generated
from its body .This is called macro expansion.

Macro Definition and Call


● It has been aforementioned that a macro consists of a name, a set of formal
parameters, and a body of codes.
● A macro can be defined by enclosing a set of statements between a macro header and
a macro end statement.
● The formal structure of a macro includes the following features:
○ Macro prototype statement: Specifies the name of the macro and name and
type of formal parameters.
○ Model statements: Specify the statements in the body of the macro from which
assembly language statements are to be generated during expansion.
○ Macro preprocessor statement: Specifies the statement used for performing
auxiliary function during macro expansion.
● A macro prototype statement can be written as follows:
<name_of_macro> [<formal parameter spec> [,...]]
where [<formal parameter spec> [,...]] defines the parameter name and its kind,
which are of the following form:
&<name_of_parameter> [<parameter_type>]

● A macro can be called by writing the name of the macro in the mnemonic field of the
assembly language. The syntax of a typical macro call can be of the following form:
<name_of_macro> [<actual_parameter_spec> [,…]]

● Eg.
Macro Definition:
MACRO
INCR &MEM_VAL, &INC_VAL, &REG
MOVER &REG &MEM_VAL
ADD &REG &INC_VAL
MOVEM &REG &MEM_VAL
MEND

Macro Call:
INCR A, B

The macro name table (MNT):


The macro name table (MNT) used to store the names of defined macros.

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int mnt_line_counter = 0;
int mdt_line_counter = 0;

void printFile(char* filename, char* printName) {


char temp[100];
printf("\n\n%s\n", printName);
FILE *fl = fopen(filename, "r");
while(fgets(temp, sizeof(temp), fl)) {
printf("\t%s", temp);
}
fclose(fl);
}

int main() {
FILE *fl, *f2, *f3;
char mne[20], la[20];
fl = fopen("input.txt", "r");
f2 = fopen("MNT.txt", "w");
f3 = fopen("MDT.txt", "w");
printFile("input.txt", "INPUT PROGRAM");
while(fgets(mne, sizeof(mne), fl)) {
mne[strcspn(mne, "\n")] = 0;
if(strcmp(mne, "MACRO") == 0) {
fgets(la, sizeof(la), fl);
la[strcspn(la, "\n")] = 0;
char *token = strtok(la, " ");
fprintf(f2, "%d %s %d\n", mnt_line_counter, token, mdt_line_counter);
fprintf(f3, "%d %s\n", mdt_line_counter, token);
mnt_line_counter++;
mdt_line_counter++;
fgets(mne, sizeof(mne), fl);
mne[strcspn(mne, "\n")] = 0;
while(strcmp(mne, "MEND") != 0) {
fprintf(f3, "%d %s\n", mdt_line_counter, mne);
mdt_line_counter++;
fgets(mne, sizeof(mne), fl);
mne[strcspn(mne, "\n")] = 0;
}
fprintf(f3, "%d %s\n", mdt_line_counter, mne);
mdt_line_counter++;
}
}
fclose(fl);
fclose(f2);
fclose(f3);
printFile("MNT.txt", "Macro Name Table");
return 0;
}
Output:

Input.txt:
MDT.txt:

MNT.txt:

Conclusion: Here, we have understood the Macro name table of the Macro processor and
implemented it using c.

You might also like