Logo 
Search:

C++ Programming Articles

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

Program of parser 2

Posted By: Emily Brown     Category: C++ Programming     Views: 2310

Write a Program of parser 2.

Code for Program of parser 2 in C++ Programming

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

struct tab
{
    char sym[10];
    int adr;
};

tab stab[10],ltab[5];

ifstream pass1,symb,lit;
int lcnt=0;

void main()
{
int nxt_adr();
pass1.open("d:\\ss\\pass1.txt");
symb.open("d:\\ss\\symtab.txt");
lit.open("d:\\ss\\littab.txt");
symb.read((char*)& stab,sizeof(stab));
lit.read((char*)& ltab,sizeof(ltab));
symb.close();
lit.close();

char str[10];
lcnt = nxt_adr();
void when_dl(char []);
void when_ad(char []);
void when_is(char []);
clrscr();
while (!pass1.eof())
    {
            pass1>>str;
             if(str[0] == 'I')
                     when_is(str);
             elseif(str[0] == 'D')
             when_dl(str);
             elseif(str[0] == 'A')
             when_ad(str);
             else
                lcnt = atoi(str);
        }
pass1.close();
}
int nxt_adr()
{
char str[10];
    do
            pass1>>str;
        while(!isdigit(str[0]));
return(atoi(str));
}

void when_dl(char st1[])
{
    char st2[10],incr[5],inst[3];
    pass1>>st2;
        int i,x=0;
        int size = strlen(st2);
    if(!strcmp(st1,"DL#1"))
           {         for(i=3;i<size-1;i++)
                   inst[x++] = st2[i];
              inst[x] = '\0';
             cout <<endl<<lcnt<< "\t00 \t 0 \t"<<inst;
           }
}

void when_ad(char st1[])
{
int  nxtlc;
if (!strcmp(st1,"AD#5"))
        return;
else
    {    nxtlc = nxt_adr();
        if (!strcmp(st1,"AD#4"))
           {    for (int x=lcnt+1;x<nxtlc;x++)
                cout <<endl<< x << "\t00 \t 0 \t000";
           }
           lcnt = nxtlc;
       }
}

void adrtab()
{
int x=0;
char str[5],adr[5];
pass1>>str;
for (int i=2;i<strlen(str);i++)
        adr[x++] = str[i];
    adr[x] = '\0';
int indx=atoi(adr)-1;
if (str[0] == 's')
        cout<<"\t"<<stab[indx].adr;
else
        cout<<"\t"<<ltab[indx].adr;
}
void when_is(char st1[])
{
char st2[10],inst[3];
    int x=0;
    cout<<endl<<lcnt;
    for (int i=3;i<strlen(st1);i++)
        inst[x++]=st1[i];
    inst[x] = '\0';
    cout<<"\t"<<inst;
    if (strcmp(inst,"10"))
    {    if(st1[3]=='9' || st1[3]=='8')
        {    cout << "\t 0 ";
            adrtab();
        }
        else
        {    char reg;
            pass1>>reg;
            cout<<"\t "<<reg;
            adrtab();
        }
      }
     else
        cout <<"\t 0 \t000";
}


/*Input Files:


pass1.txt

AD#1 c:200
200 DL#1 c:'20'
201 DL#2 c:30
231 IS#6 1 s:1
232 IS#6 2 l:1
233 IS#1 1 l:2
234 IS#4 2 s:4
235 IS#5 1 s:2
236 IS#3 2 s:2
AD#3 s:2+10
237 IS#7 2 s:3
AD#4
240 IS#8 s:4
AD#2 s:5+1
212 DL#1 c:'5'
AD#2 s:6+1
241 IS#5 3 l:3
242 IS#10
243 DL#2 c:2
AD#5 s:3
symtab.txt

A 200
B 201
LOOP 231
C 243
SECOND 211
X 240


littab.txt

='3' 238
='9' 239
='3' 245


Output:


200 00 0 20
231 6 1 200
232 6 2 238
233 1 1 239
234 4 2 243
235 5 1 201
236 3 2 201
237 7 2 231
238 00 0 000
239 00 0 000
240 8 0 243
212 00 0 5
241 5 3 245
242 10 0 000

*/
  
Share: 


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

Emily Brown
Emily Brown author of Program of parser 2 is from London, United Kingdom.
 
View All Articles

 
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!