Write a program in C code that takes two command line argume

Write a program in C code that takes two command line arguments (do not prompt the user). The first is the filename for an NFA file, and the second is the filename of a strings file. Your program will convert the NFA to an equivalent DFA and determine whether each string in the strings file is in the language of the NFA or not.

The NFA file has the following contents: The first line identifies the number of states (N) from 0 to N-1 The second line identifies the alphabet as a string of lowercase letters in no particular order, without spaces between the letters. Epsilon isn\'t identified here but is essentially a member of the alphabet. The third line identifies the initial state The fourth line is a list of final states by state number, in no particular order, with one space between each state number The remainder of the file contains one line for each of the N states from 0 to N-1. These lines indicate the destination states for transitions on alphabet members with the last entry for epsilon transitions. Each set of target states per alphabet member is a no-whitespace, comma-delimited list of states by number. -1 indicates no transition on the character.

Example NFA file:

4
ab
0
2
-1 1 -1
-1 1 2
0,2 3 -1
-1 3 1

The strings file contains one string per line to be tested against the given NFA. A line with only the string \"E\" is the same as the empty string. Your program should determine whether each string is accepted by the NFA by print \"good\" or \"fail\" followed by a space character and the string tested.

Example String file:

aab
ba
b
E
aaabbbaba
baaabbaaaa
abbaaba

Solution

Try this code, it will helps you...

#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char nfa[50][50],s[20],st[10][20],eclos[20],input[20];
int x,e,top=0,topd=0,n=0,ns,nos,in;
int checke(char a)
{
int i;
for(i=0;i<e;i++)
{
if(eclos[i]==a)
return i;
}
return -1;
}
int check(char a)
{
int i;
for(i=0;i<in;i++)
{
if(input[i]==a)
return i;
}
return -1;
}
void push(char a)
{
s[top]=a;
top++;
}
char pop()
{
top--;
return s[top];
}
void pushd(char *a)
{
strcpy(st[topd],a);
topd++;
}

char *popd()
{
topd--;
return st[topd];
}
int ctoi(char a)
{
int i=a-48;
return i;
}
char itoc(int a)
{
char i=a+48;
return i;
}
char *eclosure(char *a)
{
int i,j;
char c;
for(i=0;i<strlen(a);i++)
push(a[i]);
e=strlen(a);
strcpy(eclos,a);
while(top!=0)
{
c=pop();
for(j=0;j<ns;j++)
{
if(nfa[ctoi(c)][j]==\'e\')
{
if(check(itoc(j))==-1)
{
eclos[e]=itoc(j);
push(eclos[e]);
e++;
}
}
}
}
eclos[e]=\'\\0\';
return eclos;
}

void main()
{
int i,j,k,count;
char ec[20],a[20],b[20],c[20],dstates[10][10];
clrscr();
cout<<\"Enter the number of states\"<<endl;
cin>>ns;
for(i=0;i<ns;i++)
{
for(j=0;j<ns;j++)
{
cout<<\"Move[\"<<i<<\"][\"<<j<<\"]\";
cin>>nfa[i][j];
if(nfa[i][j]!=\'-\'&&nfa[i][j]!=\'e\')
{
if((check(nfa[i][j]))==-1)
input[in++]=nfa[i][j];
}
}
}
topd=0;
nos=0;
c[0]=itoc(0);
c[1]=\'\\0\';
pushd(eclosure(c));
strcpy(dstates[nos],eclosure(c));
for(x=0;x<in;x++)
cout<<\"\\t\"<<input[x];
cout<<\"\ \";
while(topd>0)
{
strcpy(a,popd());
cout<<a<<\"\\t\";
for(i=0;i<in;i++)
{
int len=0;
for(j=0;j<strlen(a);j++)
{
int x=ctoi(a[j]);
for(k=0;k<ns;k++)
{
if(nfa[x][k]==input[i])
ec[len++]=itoc(k);
}
}
ec[len]=\'\\0\';
strcpy(b,eclosure(ec));
count=0;
for(j=0;j<=nos;j++)
{
if(strcmp(dstates[j],b)==0)
count++;
}
if(count==0)
{
if(b[0]!=\'\\0\')
{
nos++;
pushd(b);
strcpy(dstates[nos],b);
}
}
cout<<b<<\"\\t\";
}
cout<<endl;
}
getch();
}
Thank You!

Write a program in C code that takes two command line arguments (do not prompt the user). The first is the filename for an NFA file, and the second is the filen
Write a program in C code that takes two command line arguments (do not prompt the user). The first is the filename for an NFA file, and the second is the filen
Write a program in C code that takes two command line arguments (do not prompt the user). The first is the filename for an NFA file, and the second is the filen
Write a program in C code that takes two command line arguments (do not prompt the user). The first is the filename for an NFA file, and the second is the filen

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site