Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » ParsingRSS Feeds

Program pf parser 1

Posted By: Audris Schmidt     Category: C++ Programming     Views: 2074

Write a Program pf parser 1.

Code for Program pf parser 1 in C++ Programming

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>

enum boolean{FALSE,TRUE};
struct mnemonics
{       char statement[10];
    char stat_class[3];
    int  no_or_routine;
    int length;
};
struct symtab           // symbol table
{       char symbol[8];
    int address;
    int length;
};
struct littab        // litral tab
{       char litrel[10];
    int address;
};

struct pooltab   // pool table
{ int no;
};
///////////////////////////////////////////////////////////////////////Structure that used to differenciate the instruction//////////////////////////////////////////////////////////////////struct instruction
{       char label[15];
    char opcode[10];
    char operand1[10];
    char operand2[10];
};
//////////////////////////////////////////////////////////////////////////////////////FUNCTIONS////////////////////////////////////void initalize_tables(mnemonics*);   // for initalize tablevoid differenciate_instruction(char*,instruction*); //for dividing instrucgtion
boolean checkinstruction(char*,char*,int*,int*); // check validity of instructionvoid insertlabel(char*,int *); // function to insert the label in symbol table or inquiryvoid processequ(char*,char*); // function to process EQUvoid processdc(char*,int);  // function to process DCvoid processds(char*,int,int); // function to process DSvoid processltorg();  // function to process LTORG
processimp(char*,char*,char*,int); // functio to process imperativer statementint validcondition(char*);  // check for valid conditionint validregister(char*);  // check for valid  register//////////////////// Global declaration//////////////////////////////////
    mnemonics  optab[18];
    symtab  stab[31];
    pooltab ptab[31];
    littab  ltab[31];
    int symtab_cntr=0;
    int loc_cntr=0;
    int pooltab_ptr=0;
    int littab_ptr=0;
//////////////////////////////////////////////////////////////////////////////MAIN PROGRAM///////////////////////////////////////////////////////////////////////////////////////////////////////////////int main()
{
    clrscr();
    fstream in_file;
    boolean flag;
    char one_line[80],i_class[3];
    instruction fetched;
    int i_no,i_length,dummy;
    clrscr();

    // calling function and/or initalize tables
    ptab[pooltab_ptr++].no=0;
    initalize_tables(optab);
    in_file.open("source.asm",ios::in);

    // reading from the filewhile(!in_file.eof())
       {

        in_file.getline(one_line,80);
        differenciate_instruction(one_line,&fetched);
       // checking the validity for instruction and fetching// parameter as well
        flag = checkinstruction(fetched.opcode,i_class,&i_no,&i_length);
        if(flag==FALSE)
        { cout<<"\nInvalid Instruction "; exit(0);
        }

        // if END instruction encounterif(strcmp(fetched.opcode,"END")==0)
        {
          processltorg();
          cout<<"\n"<<"(AD 2)";
          break;
        }
       // login start from here//(a)  if label exists thenif(strcmp(fetched.label,"")!=0)
         {
         insertlabel(fetched.label,&dummy);
         }
       //(B) if LTORG statementif(strcmp(fetched.opcode,"LTORG")==0)
         {
         processltorg();
         }
        //(C) if START or ORIGIN statementelseif(strcmp(fetched.opcode,"START")==0 ||strcmp(fetched.opcode,"ORIGIN")==0)
         {
             if(strcmp(fetched.opcode,"START")==0)
             cout<<"(AD  "<<1<<" ) "<<"   "<<
                "(C  "<<atoi(fetched.operand1)<<" ) ";
             loc_cntr=atoi(fetched.operand1);
         }
        elseif(strcmp(fetched.opcode,"EQU")==0)
         {
         processequ(fetched.label,fetched.operand1);
         }
        //(E) id Declarative statement exits thenelseif(strcmp(i_class,"DL")==0)
         {
           if(strcmp(fetched.opcode,"DS")==0)
           processds("DS",i_no,atoi(fetched.operand1));
           else
           processdc(one_line,i_no);
         }
       // (F) when imperative statement thenelseif(strcmp(i_class,"IS")==0)
       {
       processimp(fetched.opcode,fetched.operand1,fetched.operand2,i_no);
       loc_cntr++;
       }

    }// thile loop

    getch();
    // prinring table
    cout<<"\n\n\n";
    for(int i=0;i<symtab_cntr;i++)
    {cout<<endl<<stab[i].symbol<<"   "<<stab[i].address;
    }
    cout<<"\n\n\n";
    for(i=0;i<littab_ptr;i++)
    {cout<<endl<<ltab[i].litrel<<"   "<<ltab[i].address;
    }
    cout<<"\n\n\n";
    for(i=0;i<pooltab_ptr;i++)
    {cout<<endl<<ptab[i].no;
    }
    getch();
    return 0;
}// main ends////////////////////////////////////////////////////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////////////void initalize_tables(mnemonics *m)
{
    // for declarative statement
    strcpy(m[0].statement,"STOP");
    strcpy(m[1].statement,"ADD");
    strcpy(m[2].statement,"SUB");
    strcpy(m[3].statement,"MULT");
    strcpy(m[4].statement,"MOVER");
    strcpy(m[5].statement,"MOVEM");
    strcpy(m[6].statement,"COMP");
    strcpy(m[7].statement,"BC");
    strcpy(m[8].statement,"DIV");
    strcpy(m[9].statement,"READ");
    strcpy(m[10].statement,"PRINT");
    for(int i=0;i<11;i++)
    {m[i].length=1;
    strcpy(m[i].stat_class,"IS");
    m[i].no_or_routine=i;
    }
    // for assembler directive
    strcpy(m[11].statement,"START");
    strcpy(m[12].statement,"END");
    strcpy(m[13].statement,"ORIGIN");
    strcpy(m[14].statement,"EQU");
    strcpy(m[15].statement,"LTORG");
    for(i=11;i<16;i++)
    {m[i].length=1;
    strcpy(m[i].stat_class,"AD");
    m[i].no_or_routine=i-10;
    }
    // declarative statement
    strcpy(m[16].statement,"DC");
    m[16].length=1;
    strcpy(m[16].stat_class,"DL");
    m[16].no_or_routine=1;
    strcpy(m[17].statement,"DS");
    m[17].length=1;
    strcpy(m[17].stat_class,"DL");
    m[17].no_or_routine=2;
}

void differenciate_instruction(char *s,instruction *instruct)
{
    int i=0,j=0,counter=1;
    char word[20],c;
    // default setting
    *instruct->opcode=NULL;
    *instruct->operand1=NULL;
    *instruct->operand2=NULL;
        while(s[j]!=NULL)    // 0 loop
        {
              i=0;
              while(s[j]!=' '&& s[j]!=NULL)   // differencate words
              {word[i]=s[j];i++; j++;}
              word[i]=NULL;
              while(s[j]==' ')   // removing extra space
              j++;
            // placing the word in the structureif(counter==1)
            {
             if(s[0]==32)
             strcpy(instruct->label,NULL);
             else
             strcpy(instruct->label,word); counter++;
            }
            elseif(counter==2)
            { strcpy(instruct->opcode,word); counter++;}
            elseif(counter==3)
            { strcpy(instruct->operand1,word); counter++;}
            else
            { strcpy(instruct->operand2,word); counter++;}
               // 0 loop over
          }
}


// check validity of the opcode
boolean checkinstruction(char *opcode,char *i_class,int *i_no ,int *i_length )
{

    for(int i=0;i<18;i++)
    {
     if(strcmp(optab[i].statement,opcode)==0)
     break;
    }
    if(i>17)
    return FALSE;
    strcpy(i_class,optab[i].stat_class);
    *i_no = optab[i].no_or_routine;
    *i_length=optab[i].length;
    return TRUE;
}

// insert label in the symbol tablevoid insertlabel(char *label,int *located_at)
{
    /// checking wether the symbol already exist or notfor(int i=0;i< symtab_cntr;i++)
    {
     if(strcmp(stab[i].symbol,label)==0)
     break;
    }

    if(i< symtab_cntr)
    {
    *located_at=i+1;
    return;
    }
    strcpy(stab[symtab_cntr].symbol,label);
    stab[symtab_cntr].address = loc_cntr;
    *located_at=symtab_cntr+1;
    symtab_cntr++;
}

void processds(char *s,int i_no,int length)
{
    cout<<"\n"<<"( DS "<<i_no<<" )"<<"   "
    <<"( C , "<<length<<" )";
    loc_cntr=loc_cntr+length;
}


// processing DC statementvoid processdc(char *s,int i_no)
{
    int i=0,j;
    char word[5];
    while( s[i]!='D'&&s[i+1]!='C')
    i++;
    i=i+2;
    while(s[i]==' ')    // space ate
    i++;
        while(s[i]!=NULL)
        {
            j=0;
            while(s[i]!=' ' && s[i]!=NULL)
            {
                if(s[i]!='=')
                { word[j]=s[i];j++;}
                i++;
            }
            word[j]=NULL;
            while(s[i]==' ')    // space ate
            i++;
            cout<<"\n"<<"( DC "<<i_no<<" )"<<"   "
            <<"( C , "<<atoi(word)<<" )";
            loc_cntr++;
        }

}
// processing EQU statementvoid processequ(char *source, char *desti)
{          int located_at,address;
    insertlabel(desti,&located_at);
    address = stab[located_at].address;
    insertlabel(source,&located_at);
    stab[located_at].address=address;
}
processimp(char *opcode,char *operand1,char *operand2,int i_no)
{
    int i=validregister(operand1);
    int j=validcondition(operand1);
    int located_at;
    char s[20];
    cout<<endl<<"(IS "<<i_no<<" )";
    if(strcmp(operand1,"")!=0)
    {
        if(i!=0)         // valid register
        cout<<" ( "<<i<<" ) ";
        elseif(j!=0)   //  valid condition
        cout<<" ( "<<j<<" ) ";
        else// symbol
        {
        insertlabel(operand1,&located_at);
        cout<<"( S "<<located_at<<") ";
        }
    }
    // because PRINT and READ has only one operandif(strcmp(operand2,"")!=0)
    {
             i=validregister(operand2);
         if(i!=0)         // valid register
             cout<<" ( "<<i<<" ) ";
         elseif(operand2[0]!='=')         // symbol
         {   insertlabel(operand2,&located_at);
             cout<<"( S "<<located_at<<"  ) ";
         }
          else// litrel
          {
            for(int k=1;k<strlen(operand2);k++)
            s[k-1]=operand2[k];
            s[k-1]=NULL;
            strcpy(ltab[littab_ptr].litrel,s);
            ltab[littab_ptr++].address=0;
             cout<<"( L "<<littab_ptr<<" ) ";

         }
    }
}

// this are the valid registerint validregister(char *s)
{ if(strcmp(s,"AREG")==0)
  return 1;
  elseif(strcmp(s,"BREG")==0)
  return 2;
  elseif(strcmp(s,"CREG")==0)
  return 3;
  elseif(strcmp(s,"DREG")==0)
  return 4;
  elseif(strcmp(s,"EREG")==0)
  return 5;
  elsereturn 0;
}
// these are the valid conditionsint validcondition(char *s)
{ if(strcmp(s,"LT")==0)
  return 1;
  elseif(strcmp(s,"LE")==0)
  return 2;
  elseif(strcmp(s,"EQ")==0)
  return 3;
  elseif(strcmp(s,"GT")==0)
  return 4;
  elseif(strcmp(s,"GE")==0)
  return 5;
  elseif(strcmp(s,"ANY")==0)
  return 6;
  elsereturn 0;
  }
  // processing LTORG statementvoid processltorg()
{
    for(int i= ptab[pooltab_ptr-1].no;i<littab_ptr;i++)
    {
       cout<<endl<<"\n"<<"( DC "<<1<<" )"<<"   "
       <<"( C , "<<atoi(ltab[i].litrel)<<" )";
       ltab[i].address=loc_cntr+1;
       loc_cntr++;
    }
    ptab[pooltab_ptr++].no=littab_ptr;
}
  
Share: 


Didn't find what you were looking for? Find more on Program pf parser 1 Or get search suggestion and latest updates.

Audris Schmidt
Audris Schmidt author of Program pf parser 1 is from Frankfurt, Germany.
 
View All Articles

 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!