Sunteți pe pagina 1din 6

/*PROGRAM FOR REGULAR EXPRESSION TO DFA*/

/*NAME:
CLASS:TEIT
ROLL NO.:
*/
#include<stdio.h>
#include<conio.h>
char re[10],opr_stack[10],temp;
int i,j,state_top=0,lc=0,priority,state=0,opr_top=0,dlc=0,initial,final;
struct nfae_dfa
{
char symbol; //n[]contains NFAe table AND d[]contain DFA table.
int from,to;
}n[50],d[50],state_stack[50],buff;
//-----------------------------------------------------
void state_push(struct nfae_dfa a) //state stack push operation.
{
state_stack[state_top]=a;
state_top++;
}

struct nfae_dfa state_pop() //state stack operation.


{
state_stack[state_top].symbol='-';
state_top--;
return(state_stack[state_top]);
}

void opr_push(char a) //Operator stack push operation.


{
opr_stack[opr_top]=a;
opr_top++;
}

char opr_pop() //Operato stack pop operatrion.


{
opr_stack[opr_top]='-';
opr_top--;
return(opr_stack[opr_top]);
}

int validation(char str)


{ //checking the encounterd character
if(str>='a'&& str<='z')
return(0);
else if(str=='(')
return(1);
else if(str==')')
return(2);
else if(str=='+')
return(3);
else if(str=='.')
return(4);
else if(str=='^')
return(5);
else if(str=='*')
return(6);
else if(str=='#')
return(7);

void construct_dfa() //construct DFA from NFAe.


{
for(i=0;i<lc;i++) //Rplace smaller value of estate with greater value in
whole.
{
if(n[i].symbol=='E')
{
if(n[i].from<n[i].to)
{
for(j=0;j<lc;j++)
{
if(n[j].from==n[i].to && j!=i) //Here i!=j is
except
n[j].from=n[i].from; //the estate in which we
compare.
if(n[j].to==n[i].to && j!=i)
n[j].to=n[i].from;
if(initial==n[i].to)
initial=n[i].from;
if(final==n[i].to)
final=n[i].from;
}
}
else
{
for(j=0;j<=lc;j++)
{
if(n[j].from==n[i].from && i!=j) //same
as above.
n[j].from=n[i].to; //the estate in which
we compare.
if(n[j].to==n[i].from && i!=j)
n[j].to=n[i].to;
if(initial==n[i].from)
initial=n[i].to;
if(final==n[i].from)
final=n[i].to;

}
}
}
}
for(i=0;i<=lc;i++) //move all the state which is from without'e'to DF
table.
if(n[i].symbol!='E')
{
d[dlc]=n[i];
dlc++;
}

void new_state(char symbol) //creating new state.


{
buff.symbol=symbol;//crating new state.
buff.from=state;// "
buff.to=++state;// "
n[lc]=buff; // Enterint state into nfae table.
state_push(buff);//pushing to the stack.
lc++; //Incrementing table conuter.
state++; //Incrementing state conuter.
}

void check_operator() //This func is for checking whether there is operator at top
op_stack
{ //if yes then perform the joining operation according to
operator at top.
struct nfae_dfa buff1,buff2;

if(opr_top!=0)
{
temp=opr_pop();
priority=validation(temp);
if(priority==1)
{
opr_push(temp);
}
else if(priority== 4) //in case of operator.
{
buff2=state_pop();
buff1=state_pop();

buff.symbol='E'; // Creating new 'E' state


buff.from=buff1.to; // "
buff.to=buff2.from; // "
n[lc]=buff; //Entering state into nfae table.
lc++; // Incrementing table counter.

buff.symbol='X';
buff.from=buff1.from; //Initial state.
buff.to=buff2.to; //Final state.
state_push(buff); //Pushing initial & final state to the stack.
}
else if(priority==3) //Incase of operator +
{
buff2=state_pop();
buff1=state_pop();

buff.symbol='E'; // Creating new 'E' state

buff.from=state;
buff.to=buff1.from;
n[lc]=buff; //Entering state into nfae table.
lc++; // Incrementing table counter.

buff.from=state;
buff.to=buff2.from;
n[lc]=buff; //Entering state into nfae table.
lc++; // Incrementing table counter.

buff.from=buff1.to;
buff.to=state+1;
n[lc]=buff; //Entering state into nfae table.
lc++; // Incrementing table counter.

buff.from=buff2.to;
buff.to=state+1;
n[lc]=buff; //Entering state into nfae table.
lc++; // Incrementing table counter.

buff.symbol='X';
buff.from=state; //Initial state.
buff.to=state+1; //Final state.
state_push(buff); //Pushing initial & final state to the stack.

state=state+2;
}
else if(priority==6) //when ^* found.
{
buff1=state_pop();
buff.symbol='E';
buff.from=buff1.to; //Creating E state by combining
buff.to=buff1.from; //final to initial state.
n[lc]=buff; //Entering state into nfae table.
lc++; //Incrementing table counter.

buff.from=state;
buff.to=state+1;
n[lc]=buff; //Entering state into nfae table.
lc++; // Incrementing table counter.

buff.from=state;
buff.to=buff2.from;
n[lc]=buff; //Entering state into nfae table.
lc++; // Incrementing table counter.

buff.from=buff1.to;
buff.to=state+1;
n[lc]=buff; //Entering state into nfae table.
lc++; // Incrementing table counter.

buff.symbol='X';
buff.from=state; //Initial state.
buff.to=state+1; //Final state.
state_push(buff); //Pushing initial & final state to the stack.

state=state+2;
}
else if(priority==7) //incase of ^#.
{
buff1=state_pop();
buff.symbol='E';

buff.from=buff1.to;//Creating E state by combining.


buff.to=buff1.from;//Final to initial state.
n[lc]=buff; //Entering state into nfae table.
lc++; // Incrementing table counter.

buff.from=state;
buff.to=buff1.from;
n[lc]=buff; //Entering state into nfae table.
lc++; // Incrementing table counter.

buff.from=buff1.to;
buff.to=state+1;
n[lc]=buff; //Entering state into nfae table.
lc++; // Incrementing table counter.

buff.symbol='X';
state_push(buff); //Pushing initial & final state to the stack.
state=state+2;

}
}

}
void print_nfae()
{
printf("\n\t--------------NFAe-----------------\n");
printf("\n\t| FROM\t|\t|\tSYMB |\n");
printf("\n\t-----------------------------------\n");
for(i=0;i<=lc;i++)
printf("\n\t| %d\t|\t %d\t|\t %c |",n[i].from,n[i].to,n[i].symbol);
printf("\n\n\t--------------------------------");
buff=state_pop();
initial=buff.from;
final=buff.to;
printf("\n\tstart state: %d and End state:%d",initial,final);
}

void print_dfa()
{
printf("\n\n\t-----------------DFA-------------\n");
printf("\n\t|from\t|\tTO\t|\tSYMB |\n");
printf("\n\t------------------------------------");
for(i=0;i<dlc;i++)
printf("\n\n\t| %d\t|\t %d\t|\t %c |",d[i].from,d[i].to,d[i].symbol);
printf("\n\n\t-------------------------------");
printf("\n\tstart state: %d and End state : %d",initial,final);
}
void main()
{
clrscr();
printf("Enter the regular Expression");
scanf("%s",re);
for(i=0;re[i]!='\0';i++)
{
priority=validation(re[i]);
switch(priority)
{
case 0: new_state(re[i]); //create new state.
check_operator(); //check whether
opr_stack[top] contain;

//operator if yes then create E state.

//according to requrement.
break;
case 1: opr_push(re[i]); //push ( to operator stack.
break;
case 2: while(opr_pop()!='(') //pop until '(' found.
opr_top--;
check_operator(); //check whether
opr_stack contain operator.
break;
case 3: opr_push(re[i]); //+ operator found push on to
opr_stack.
break;
case 4: opr_push(re[i]); //. operator found push on to
opr_stack.
break;
case 5: i++; // ^operator found take its next
operator by incrementing i++.
opr_push(re[i]); //Here operator will be
it whether * or #.
check_operator(); //check whether
opr_stack contain operator.
break;
}
}
print_nfae();
construct_dfa();
print_dfa();
getch();
}

S-ar putea să vă placă și