affiliate marketing

Saturday, 17 December 2011

C-program for implementing a macro processor.


IMPLEMENTATION OF MACRO PROCESSOR
AIM
To write a C-program for implementing a macro processor.
ALGORITHM
1.Get the state from input file.
2.From the line read,check if opcode is directive “MACRO” if so then number of macro “n” must be incremented.
3.Repeat step 1 and 2 until end of file is encountered.
4.Open “n” number of files in write mode.These files will later hold body of “n” macro respectively.
5.Rewind the input file pointer.
6.If opcode is “MACRO”
7.Enter macro name present in operand file field into array names “m”.
8.Write line to expanded output file.
9.Enter lines in body of each macro into corresponding files already opened in file.
10.Write body of each macro to be expanded output file also until “END” is reached.
11.Else if OPCODE is “CALL” the line must be a macro invocation statement so the macro body is retrieved from the corresponding file.
12.Write all the remaining lines directly to expanded lines.
PROGRAM:
#include<stdio.h>

#include<stdlib.h>
#include<conio.h>
#include<string.h>
FILE *f1,*f2,*f3,*f4,*f5;
void main()
 {
   char lbl[20],opc[20],opr[20],mname[20],arg[20],check[20];
   char ch,dlbl[20],dopc[20],dopr[20];
   int c;
   clrscr();
   f1=fopen("MACIN.DAT","r");
   rewind(f1);
   f2=fopen("NAMETAB.DAT","r");
   rewind(f2);
   f3=fopen("DEFTAB.DAT","r");
   f4=fopen("EXPAND.DAT","w");
   f5=fopen("ARGTAB.DAT","w");
   while(!feof(f1))
    {
     l1:
     fscanf(f1,"%s%s%s",lbl,opc,opr);
     if(strcmp(opc,mname)==0)
     c=1;
     if(strcmp(opc,"MACRO")==0)
      {
                while(strcmp(opc,"MEND")!=0)
                 {
                   fscanf(f1,"%s%s%s",lbl,opc,opr);
                   continue;
                 }
                goto l1;
      }
     rewind(f2);
     rewind(f3);
     fscanf(f2,"%s",mname);
     if(strcmp(opc,mname)==0)
      {
                fprintf(f5,"%s",opr);
                rewind(f5);
                while(!feof(f3))
                 {
                   fscanf(f3,"%s%s%s",dlbl,dopc,dopr);
                   if(strcmp(dopc,"MEND")!=0)
                    {
                      if(strcmp(dopc,"MACRO")==0)
                       {
                                 continue;
                       }
                      if(strcmp(dopr,"=X'?1'")==0)
                      strcpy(dopr,"=X'F1'");
                      if(strcmp(dopr,"?2,X")==0)
                      strcpy(dopr,"BUFFER,X");
                      if(strcmp(dopr,"?3")==0)
                      strcpy(dopr,"LENGTH");
                      if(c==1)
                       {
                                fprintf(f4,"%s\t%s\t%s\n",lbl,opc,opr);
                                c=0;
                       }
                      fprintf(f4,"%s\t%s\t%s\n",dlbl,dopc,dopr);
                    }
                 }
                goto l1;
      }
     fprintf(f4,"%s\t%s\t%s\n",lbl,opc,opr);
   }
  fcloseall();
  printf("\nINPUT\n\nMacro program before expanded:\n");
  printf("-----------------\n");
  f1=fopen("MACIN.DAT","r");
  ch=fgetc(f1);
  while(ch!=EOF)
   {
     printf("%c",ch);
     ch=fgetc(f2);
   }
  printf("\nDefinition table\n");
  printf("-----------------\n");
  f2=fopen("DEFTAB.DAT","r");
  ch=fgetc(f2);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(f2);
   }
  printf("\nName table\n");
  printf("-----------------\n");
  f3=fopen("NAMETAB.DAT","r");
  ch=fgetc(f3);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(f3);
   }
  getch();
  clrscr();
  printf("\n\nOUTPUT\n\nMacro Program after expanded\n");
  printf("----------------\n\n");
  f4=fopen("EXPAND.DAT","r");
  ch=fgetc(f4);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(f4);
   }
  printf("\nArgument Table\n");
  printf("---------------------\n\n");
  f5=fopen("ARGTAB.DAT","r");
  ch=fgetc(f5);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(f4);
   }
  fcloseall();
  getch();
 }
INPUT FILES:
MACIN.DAT
COPY START NULL
RDBUFF MACRO INDEV,BUFADR,RECLTH
NULL CLEAR X
NULL CLEAR A
NULL CLEAR S
NULL +LDT #4096
NULL TD =X'&INDEV'
NULL JEQ *_3
NULL RD =X'&INDEV'
NULL COMPR A,S
NULL JEQ *+11
NULL STCH BUFADR,X
NULL TIXR T
NULL JLT *-19
NULL STX RECLTH
NULL MEND NULL
FIRST STL RETADR
CLOOP RDBUFF F1,BUFFER,LENGTH
NULL LDA LENGTH
NULL COMP #0
NULL JEQ ENDFIL
EOF BYTE C'EOF'
THREE WORD 3
RETADR RESW 1
LENGTH RESW 1
BUFFER RESB 4096
NULL END FIRST


DEFTAB.DAT
COPY START NULL
RDBUFF MACRO &INDEC,&BUFADR,&RECLTH
NULL CLEAR X
NULL CLEAR A
NULL CLEAR S
NULL +LDT #4096
NULL TD =x'?1'
NULL JEQ *-3
NULL RD =X'?1'
NULL COMPR A,S
NULL JEQ *+11
NULL STCH ?2,X
NULL TIXR T
NULL JLT *-19
NULL STX ?3
NULL MEND NULL
NAMETAB.DAT
RDBUFF








OUTPUT:
MACRO PROGRAM BEFORE EXPANSION:

MACRO PROGRAM AFTER EXPANSION:


RESULT
Thus macro processor has been successfully implemented using C.

1 comment: