Documente Academic
Documente Profesional
Documente Cultură
/*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++;
}
}
}
}
}
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 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='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.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=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;
//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();
}