Saturday 30 April 2016

C Program to construct a DFA from a regular expression.



#include<conio.h>
#include<strings.h>

void main()
{
int table[2][2],i,j,l,status=0,success;
char input[100];
printf("Program for implmenting DFA of language (a+aa*b)*\n\n\nEnter Input String \n");
table[0][0]=1;
table[0][1]=-1;
table[1][0]=1;
table[1][1]=0;
scanf("%s",input);
l=strlen(input);

for(i=0;i<l;i++)
{
if(input[i]!='a'&&input[i]!='b')
{
printf("Value entered is wrong");
getch();
exit(0);
}
if(input[i]=='a')
status=table[status][0];
else
status=table[status][1];

if(status==-1)
{
printf("String not Accepted by this Language");
break;
}
}
if(i==l)
printf("String Accepted");
getch();
}

C Program to eliminate left recursion from the grammar

.

#include<stdio.h>
  #include<string.h>
  #define SIZE 10
  int main () {
       char non_terminal;
       char beta,alpha;
       int num;
       int i;
       char production[10][SIZE];
       int index=3; /* starting of the string following "->" */
       printf("Enter Number of Production : ");
       scanf("%d",&num);
       printf("Enter the grammar as E->E-A :\n");
       for(i=0;i<num;i++){
            scanf("%s",production[i]);
       }
       for(i=0;i<num;i++){
            printf("\nGRAMMAR : : : %s",production[i]);
            non_terminal=production[i][0];
            if(non_terminal==production[i][index]) {
                 alpha=production[i][index+1];
                 printf(" is left recursive.\n");
                 while(production[i][index]!=0 && production[i][index]!='|')  {
           index++;  }
                 if(production[i][index]!=0)  {
                      beta=production[i][index+1];
                      printf("Grammar without left recursion:\n");
                      printf("%c->%c%c\'",non_terminal,beta,non_terminal);
                      printf("\n%c\'->%c%c\'|^\n",non_terminal,alpha,non_terminal);
                  }
                 else
                      printf(" can't be reduced\n");
            }
            else
                 printf(" is not left recursive.\n");
            index=3;
       }
  getche();
  }  

C Program to implement SR Parser on the Pre-assumed grammar.



#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
char ip_sym[15],stack[15];
int ip_ptr=0,st_ptr=0,len,i;
char temp[2],temp2[2];
char act[15];
void check();
void main()
{
//clrscr();
printf("\n\t\t SHIFT REDUCE PARSER\n");
printf("\n GRAMMER\n");
printf("\n E->E+E\n E->E/E");
printf("\n E->E*E\n E->a/b");
printf("\n enter the input symbol:\t");
gets(ip_sym);
printf("\n\t stack implementation table");
printf("\n stack\t\t input symbol\t\t action");
printf("\n______\t\t ____________\t\t ______\n");
printf("\n $\t\t%s$\t\t\t--",ip_sym);
strcpy(act,"shift  ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
len=strlen(ip_sym);
for(i=0;i<=len-1;i++)
{
stack[st_ptr]=ip_sym[ip_ptr];
stack[st_ptr+1]='\0';
ip_sym[ip_ptr]=' ';
ip_ptr++;
printf("\n $%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
check();
st_ptr++;
}
st_ptr++;
check();
}
void check()
{
int flag=0;
temp2[0]=stack[st_ptr];
temp2[1]='\0';
if((!strcmpi(temp2,"a"))||(!strcmpi(temp2,"b")))
{
stack[st_ptr]='E';
if(!strcmpi(temp2,"a"))
printf("\n $%s\t\t%s$\t\t\tE->a",stack, ip_sym);
else
printf("\n $%s\t\t%s$\t\t\tE->b",stack,ip_sym);
flag=1;
}
if((!strcmpi(temp2,"+"))||(strcmpi(temp2,"*"))||(!strcmpi(temp2,"/")))
{
flag=1;
}
if((!strcmpi(stack,"E+E"))||(!strcmpi(stack,"E\E"))||(!strcmpi(stack,"E*E")))
{
strcpy(stack,"E");
st_ptr=0;
if(!strcmpi(stack,"E+E"))
printf("\n $%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
else
if(!strcmpi(stack,"E\E"))
printf("\n $%s\t\t %s$\t\t\tE->E\E",stack,ip_sym);
else
printf("\n $%s\t\t%s$\t\t\tE->E*E",stack,ip_sym);
flag=1;
}

if(!strcmpi(stack,"E")&&ip_ptr==len)
{
 printf("\n $%s\t\t%s$\t\t\tACCEPT",stack,ip_sym);
getche();
}if(flag==0){
 printf("\n%s\t\t\t%s\t\t reject",stack,ip_sym);
}
getche();
}

C Program to check whether the given string is a valid identifier or not.

C Program to check whether the given string is a valid identifier or not.

#include <stdio.h>
#include <string.h>
#include <conio.h>
main (){
char string [50];
printf ("Enter value to be identified: ");
scanf ("%s", string);
int length = strlen (string) - 1;
bool alpha_string = false;
if ( string [0] >= 'A' && string [0] <= 'Z' )
alpha_string = true;
if ( string [0] >= 'a' && string [0] <= 'z' )
alpha_string = true;
bool underScore = true;
if ( string [length] == '_' )
underScore = false;
int num = 0;
for ( int i = 1; i <= length; i++ ) {
if ( string [i] == '_' )
num++;
else
num = 0;
if ( num == 2 ) {
underScore = false;
break;
}}
if ( alpha_string && underScore)
printf ("Answer: %s is a valid identifier\n", string);
else
printf ("Answer: %s is not a valid identifier\n", string);
getche();
}