Tuesday, May 3, 2011




1. Simulation of finite state of automata to recognize the tokens of various statements

 2.
Write a program for backtrack parser

 3. Write a program  for  recursive decent parser


 4.
Simulation of finite state machine to distinguish among integer no., real no. and exponent no.

 5. Write a program for Shift reduce parser

 6. Write a program for Predictive Parser


 7. Write a program for OPG




                                                                                           Faculty’s Signature





                                                       

                                                            PROGRAM- 1
/* Simulation of finite state of automata to recognize the tokens of various satament*/                
#include<conio.h>
#include<stdio.h>
void state0();
void state1();
void fail();
void state2();
int i=0,l;
char c;
char str[100];
char str1[100];
void main()
{
clrscr();
printf("Enter a string===========>:");
gets(str);
//puts(str);
l=strlen(str);
//printf("%d",l);
state0();
getch();
}
void state0()
{
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')||(str[i]=='_'))
{
i=i+1;
state1();
}
else
fail();
}
void state1()
{
if(((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')||(str[i]=='_')||(str[i]>='0'&&str[i]<='9')))
{
i=i+1;
       // printf("%d",i);
state1();
}
else if((str[i]==';'))
state2();
else
fail();
}
void fail()
{
printf("invalid String");
}
void state2()
{
int j;
for(j=0;j<i;j++)
{
str1[j]=str[j];
}
printf("String %s is a valid identifier",str1);
}


                                         



                                                  PROGRAM- 2
/* A program for backtrack parser for the grammar-
S -> cAd
A ->a/ab
*/
#include<conio.h>
#include<stdio.h>
#include<iostreame.h>
#include<string.h>
int pt=0,len,ptr;
char str[10];
int S();
int A();
//void T();
//void TPRIME();
//void F();
void main()
{
cout<<"enter string::";
cin>>str;
len=strlen(str);
str[len]='$';
cout<<str;
if(S()&&str[ptr]=='$')
{
cout<<"Success";
}
else cout<<"Error";
getch();
}
int S()
{
//cout<<"e\n";
if(str[ptr]=='c')
{
ptr++;
if(A()==1)
{
if(str[ptr]=='d')
{
ptr++;
return 1;
} else return 0;
}
else return 0;
}else return 0;
}

int A()
{
int p;
p=ptr;
if(str[ptr]=='a')
{
ptr++;
if(str[ptr]=='b')
{
ptr++;
return 1;
}
else
{
ptr=p;
if(str[ptr]=='a')
{
ptr++;
return 1;
}
else
{
return 2;
}
}
}
else
{
return 2;
}
}





                                                  PROGRAM - 3
/*A program for Recursive descent parser for the grammar-
E -> TE'
E'-> +TE'/ @
T -> FT'
T'-> *FT'/ @
F -> (E) / id

where symbol '@' is used to represent null
*/
#include<conio.h>
#include<stdio.h>
#include<iostreame.h>
#include<string.h>
int pt=0,len;
char str[10];
void E();
void EPRIME();
void T();
void TPRIME();
void F();
void main()
{
cout<<"enter string::";
cin>>str;
len=strlen(str);
str[len]='$';
cout<<str;
E();
if(str[pt]=='$')
{
cout<<"success";
}
else
{
cout<<"error";
}
//kp:
getch();
}
void E()
{
cout<<"e\n";
T();
EPRIME();
//cout<<"e\n";
}
void EPRIME()
{
cout<<"ep\n" ;
if(str[pt]=='+')
{
pt=pt+1;
T();
EPRIME();
}
}
void T()
{
cout<<"t\n";
F();
TPRIME();
}
void TPRIME()
{
cout<<"tp\n";
if(str[pt]=='*')
{
pt=pt+1;
F();
TPRIME();
}
}
void F()
{
cout<<"f\n";
if(str[pt]=='d')
{
pt=pt+1;
}
else if(str[pt]=='(')
{
pt=pt+1;
E();
if(str[pt]==')')
{
pt=pt+1;
}
else
{
cout<<"error\n";
//goto kp;
}
}
else
{
cout<<"error\n";
      // goto kp;     } }
PROGRAM - 4
/*A program to check that the entered number is Integer, Real,and Exponential
with the help of number grammer-
digit -> 0/1/2.../9
sign -> +/-/@
Integer -> (digit)+
Sign_Integer -> sign(digit)+
Real -> Sign_Integer.Integer
Exponential -> Real E Sign_Integer
*/
#include<conio.h>
#include<iostreame.h>
#include<string.h>
void I();
int pt=0,c,t=0,l,p=0,e=0;
char arr[10];

void main()
{
clrscr();
cout<<"enter a number to check integer,real,exponential:"<<"\n";
cin>>arr;
l=strlen(arr);
arr[l]='$';
cout<<arr<<"\n";
I();
if(t==1)
{
if(p==1&&e==1)
{
cout<<"Number is Exponential";
}
else if(p==1&&e==0)
{
cout<<"Number is Real";
}
else if(p==0&&e==1)
{
cout<<"Number is Exponential";
}
else
{
cout<<"Number is Integer";
}
}
else
{
cout<<"Wrong Number";
}
getch();
}
void I()
{
c=arr[pt];
if(((c==43)||(c==45))||((c>=48)&&(c<58)))
{
pt=pt+1;
I();
}
else if(c=='$')
{
t=1;
}
else if(c=='e'||c=='E')
{
e=e+1;
pt=pt+1;
if(e==2)
{
t=0;
}
else
{
I();
}
}
else if(c==46&&e==0)
{
p=p+1;
pt=pt+1;
if(p==2)
{
t=0;
pt=l;

}
else
{
I();
}
}
else
{
t=0; }  }
{
return 2;
}
}
                                              PROGRAM-5
/*A program for stack implementation of shift reduce parser for the grammar-
E -> E * E
E -> E + E
E -> d
*/
#include<conio.h>
#include<iostreame.h>
#include<string.h>

void main()
{
char input[15],stacks[15];
int l,pt=0,stp=0,tops=0,l1,i,t,t1,m=0;
char h2[]="E+E";
char h3[]="d";
char h1[]="E*E";
cout<<"Enter a string to parse::";
cin>>input;
l=strlen(input);
input[l]='$';
cout<<input<<"\n";
stacks[tops]='$';
//tops=tops+1;
while(stacks[tops]!='E'||input[pt]!='$')
{
//m=m+1;
tops=tops+1;
//stacks[top+1]='\0';
       /// cout<<stack<<"\n";
stacks[tops]=input[pt];
stacks[tops+1]='\0';
       cout<<stacks<<"\n";
if(stacks[tops]=='d')
{
stacks[tops]='E';
}
if(tops>=3)
{
//cout<<"kp";
t=tops;
t1=tops;
for(i=2;i>=0;i--)
{
      // cout<<stacks[t]<<"t="<<t<<"=\t"<<h1[i]<<"i="<<i<<"\n";
if(stacks[t]==h1[i])
{
if(i==0)
{
tops=t;
stacks[tops]='E';
     // cout<<"top="<<tops<<"\n";
}
t=t-1;
}
else
{
i=0;
}
}
for(i=2;i>=0;i--)
{
      // cout<<stacks[t]<<"t="<<t<<"=\t"<<h1[i]<<"i="<<i<<"\n";
if(stacks[t1]==h2[i])
{
if(i==0)
{
tops=t1;
stacks[tops]='E';
//cout<<"top="<<tops<<"\n";
}
t1=t1-1;
}
else
{
i=0;
}
}

     }
else
{}
pt=pt+1;
     stacks[tops+1]='\0';
     cout<<stacks<<"\n";
     }
if(input[pt]=='$'&&stacks[tops]=='E'&&tops==1)
{cout<<"Success";}
else
{cout<<"error";}
  getch();
}






                                                               PROGRAM-6
/* Predictive Parsing for following grammer
E->E+T/T
F->F*T/F
F->id(Identifier)
*/
#include<string.h>
#include<conio.h>
char a[10];
int top=-1,i;
void error(){
printf("Syntax Error");
}
void push(char k[]) //Pushes The Set Of Characters on to the Stack
{
  for(i=0;k[i]!='\0';i++)
  {
    if(top<9)
    a[++top]=k[i];
  }
}
char TOS()        //Returns TOP of the Stack
{
  return a[top];
}
void pop()       //Pops 1 element from the Stack
{
  if(top>=0)
    a[top--]='\0';
}
void display()  //Displays Elements Of Stack
{
  for(i=0;i<=top;i++)
    printf("%c",a[i]);
}
void display1(char p[],int m) //Displays The Present Input String
{
  int l;
  printf("\t");
  for(l=m;p[l]!='\0';l++)
    printf("%c",p[l]);
}
char* stack(){
return a;
}
int main()
{
  char ip[20],r[20],st,an;
  int ir,ic,j=0,k;
  char t[5][6][10]={"$","$","TH","$","TH","$",
  "+TH","$","e","e","$","e",
  "$","$","FU","$","FU","$",
  "e","*FU","e","e","$","e",
  "$","$","(E)","$","i","$"};
  clrscr();
  printf("\nEnter any String(Append with $)");
  gets(ip);
  printf("Stack\tInput\tOutput\n\n");
  push("$E");
  display();
  printf("\t%s\n",ip);
  for(j=0;ip[j]!='\0';)
  {
  if(TOS()==an)
      {
pop();
display();
display1(ip,j+1);
printf("\tPOP\n");
j++;
      }
    an=ip[j];
    st=TOS();
      if(st=='E')ir=0;
      else if(st=='H')ir=1;
      else if(st=='T')ir=2;
      else if(st=='U')ir=3;
      else if(st=='F')ir=4;
      else {
   error();
   break;
   }
      if(an=='+')ic=0;
      else if(an=='*')ic=1;
      else if(an=='(')ic=2;
      else if(an==')')ic=3;
      else if((an>='a'&&an<='z')||(an>='A'&&an<='Z')){ic=4;an='i';}
      else if(an=='$')ic=5;
      strcpy(r,strrev(t[ir][ic]));
      strrev(t[ir][ic]);
      pop();
      push(r);
      if(TOS()=='e')
      {
pop();
display();
display1(ip,j);
printf("\t%c->%c\n",st,238);
      }
      else{
      display();
      display1(ip,j);
      printf("\t%c->%s\n",st,t[ir][ic]);
      }
      if(TOS()=='$'&&an=='$')
      break;
      if(TOS()=='$'){
error();
break;
}
      }
      k=strcmp(stack(),"$");
      if(k==0 && i==strlen(ip))
    printf("\n Given String is accepted");
    else
    printf("\n Given String is not accepted");
  return 0;
}













                                                         PROGRAM-7  
                      /* PROGRAM OF  OPG */
#include <stdio.h>   /*All c program must have this*/
#define bufsize 1024 /*A defined integer for our buffer size*/

int main(){          /*Program entry point*/
     FILE* fp;       /*Declare file pointer variable*/
     char *buf[bufsize], *tok, fname[15];
   
     printf("Enter filename: ");
     scanf("%s",&fname);
   
     /*If file doesn't exist or filetype isn't allowed exit and*/
     /*error message & return (1) control to the OS*/
     if ((fp = fopen(fname,"rt")) == NULL){
             fprintf(stderr,"Error opening file: %s\n",fname);
             return 1;
      }
     
     printf("\n"); /*Newline effect*/
   
     /*Read into the buffer contents within thr file stream*/
     while(fgets(buf, bufsize, fp) != NULL){
                             /*Here we tokenize our string and scan for " \n" characters*/
          for(tok = strtok(buf," \n");tok;tok=strtok(0," \n")){
                  printf("%s\n",tok);
          }                
          fclose(fp); /*Close file*/
     }  /*Continue until EOF is encoutered*/
     printf("\n");
     system("pause"); /*System delay function (windows)*/
     return 0; /*Executed without errors*/
}/*End main*/

No comments:

Post a Comment