answersLogoWhite

0


Best Answer
Conversion of a Regular Expression to NFA Algorithm Source code C programmingCS342 Compiler Lab Source code Algorithm C Programming

#include

#include

void main()

{

char reg[20];

int q[20][3],i,j,len,a,b;

clrscr();

for(a=0;a<20;a++)

{

for(b=0;b<3;b++)

{

q[a][b]=0;

}

}

printf("Regular expression: \n");

scanf("%s",reg);

len=strlen(reg);

i=0;

j=1;

while(i

{

if(reg[i]=='a'&®[i+1]!='/'&®[i+1]!='*')

{

q[j][0]=j+1;

j++;

}

if(reg[i]=='b'&®[i+1]!='/'&®[i+1]!='*')

{

q[j][1]=j+1;

j++;

}

if(reg[i]=='e'&®[i+1]!='/'&®[i+1]!='*')

{

q[j][2]=j+1;

j++;

}

if(reg[i]=='a'&®[i+1]=='/'&®[i+2]=='b')

{

q[j][2]=((j+1)*10)+(j+3);

j++;

q[j][0]=j+1;

j++;

q[j][2]=j+3;

j++;

q[j][1]=j+1;

j++;

q[j][2]=j+1;

j++;

i=i+2;

}

if(reg[i]=='b'&®[i+1]=='/'&®[i+2]=='a')

{

q[j][2]=((j+1)*10)+(j+3);

j++;

q[j][1]=j+1;

j++;

q[j][2]=j+3;

j++;

q[j][0]=j+1;

j++;

q[j][2]=j+1;

j++;

i=i+2;

}

if(reg[i]=='a'&®[i+1]=='*')

{

q[j][2]=((j+1)*10)+(j+3);

j++;

q[j][0]=j+1;

j++;

q[j][2]=((j+1)*10)+(j-1);

j++;

}

if(reg[i]=='b'&®[i+1]=='*')

{

q[j][2]=((j+1)*10)+(j+3);

j++;

q[j][1]=j+1;

j++;

q[j][2]=((j+1)*10)+(j-1);

j++;

}

if(reg[i]==')'&®[i+1]=='*')

{

q[0][2]=((j+1)*10)+1;

q[j][2]=((j+1)*10)+1;

j++;

}

i++;

}

printf("Transition function \n");

for(i=0;i<=j;i++)

{

if(q[i][0]!=0)

printf("\n q[%d,a]-->%d",i,q[i][0]);

if(q[i][1]!=0)

printf("\n q[%d,b]-->%d",i,q[i][1]);

if(q[i][2]!=0)

{

if(q[i][2]<10)

printf("\n q[%d,e]-->%d",i,q[i][2]);

else

printf("\n q[%d,e]-->%d & %d",i,q[i][2]/10,q[i][2]%10);

}

}

getch();

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#include

#include

#include

#include

void main()

{

clrscr();

char c[20];

int a[10][2];

int l,n=0,s=0,t;

cout<<"Enter the string:";

cin>>c;

l=strlen(c);

for(int f=0;f {

if(c[f]=='+')

{

s++;

}

}

n=s+1;

cout<<"Thus states="< for(int i=0;i {

t=1;

a[i][1]=t;

a[i][2]=(t-1);

t++;

if(i>=1)

{

a[i][2]=n-2;

if(i==2)

{

a[i][1]=n-1;

a[i][2]=n-1;

}

}

}

cout<<"\n\n\tNFA\n\n";

cout<<"state \t"<<"a\t"<<"b\t";

for(int j=0;j {

cout<<"\nq"< if(j==1)

{

cout<<",q"< }

cout<<"\t"<<"q"< }

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include

#include

#include

#include

void main()

{

clrscr();

char c[20];

int a[10][2];

int l,n=0,s=0,t;

cout<<"Enter the string:";

cin>>c;

l=strlen(c);

for(int f=0;f {

if(c[f]=='+')

{

s++;

}

}

n=s+1;

cout<<"Thus states="< for(int i=0;i {

t=1;

a[i][1]=t;

a[i][2]=(t-1);

t++;

if(i>=1)

{

a[i][2]=n-2;

if(i==2)

{

a[i][1]=n-1;

a[i][2]=n-1;

}

}

}

cout<<"\n\n\tNFA\n\n";

cout<<"state \t"<<"a\t"<<"b\t";

for(int j=0;j {

cout<<"\nq"< if(j==1)

{

cout<<",q"< }

cout<<"\t"<<"q"< }

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

re to dfa

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program for regular expression to nfa?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What do you mean by statements in C program?

A statement in C is an expression terminated with a semi-colon. That is, a semi-colon turns an expression into a statement.


What is the coding of program which can simplify mathematical expression in c?

34.768


How do you generate a parse tree from an expression using C program?

c code for top down parser


What is regular grammar of a for loops C programming language?

Something like this:statement -> for (opt_expression; opt_expression; opt_expression) statementstatement -> while (expression) statementstatement -> do statement while (expression);opt_expression -> | expression


Who heads the National Food Authority in the Philippines?

Mr. Jessup P. Navarro is the current Administrator of the Philippine National Food Authority (NFA). However, since the NFA is under the Department of Agriculture (DA), the Secretary of DA, Sec. Arthur C. Yap, chairs the NFA Council.


Define an expression that evaluates to true when i equals j?

In Java, or C, the expression is simply:i == jIf the two are equal, this expression will evaluate to true; if not, it will evaluate to false.In Java, or C, the expression is simply:i == jIf the two are equal, this expression will evaluate to true; if not, it will evaluate to false.In Java, or C, the expression is simply:i == jIf the two are equal, this expression will evaluate to true; if not, it will evaluate to false.In Java, or C, the expression is simply:i == jIf the two are equal, this expression will evaluate to true; if not, it will evaluate to false.


How do you write as an expression the sum of a and c?

It is simply: a+c


What are components of expression in c?

no


C program was introduced in the year?

c program was introduced in the year 1972 by Dennis RitchieNo, it was the C language, not the C program.


Features of c program?

the features of a C program


Why the semicoloumn is used in c program?

Why semicolon? Tradition.What does it do? Terminates a single statement, eg:i+= 3; /* expression is a single statement */{ i= 3; --j; } /* no semicolon after the compound statement */


What is a quadratic expression?

A quadratic expression is an expression which is written in the form ax2+bx+c, where a, b, and c represent constants, x represents a variable, and a is not equal to 0.