0% found this document useful (0 votes)
68 views

Nfa 2 Dfa

This document contains C code for converting a non-deterministic finite automaton (NFA) to a deterministic finite automaton (DFA). It defines functions for initializing an NFA table, getting the next states from the current states and symbols, indexing states, performing the NFA to DFA conversion by getting next states and indexing them, and printing the resulting DFA state transition table. The main function calls these functions to initialize an NFA table as an example, convert it to a DFA stored in a table, and print the resulting DFA table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Nfa 2 Dfa

This document contains C code for converting a non-deterministic finite automaton (NFA) to a deterministic finite automaton (DFA). It defines functions for initializing an NFA table, getting the next states from the current states and symbols, indexing states, performing the NFA to DFA conversion by getting next states and indexing them, and printing the resulting DFA state transition table. The main function calls these functions to initialize an NFA table as an example, convert it to a DFA stored in a table, and print the resulting DFA table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include

#include
#include
#define STATES 25
#define SYMBOLS 20

int N_symbols; /* number of input symbols */


int NFA_states; /* number of NFA states */
char *NFAtab[STATES][SYMBOLS];

int DFA_states; /* number of DFA states */


int DFAtab[STATES][SYMBOLS];

/*
Print state-transition table.
State names: 'A', 'B', 'C', ...
*/
void put_dfa_table(
int tab[][SYMBOLS], /* DFA table */
int nstates, /* number of states */
int nsymbols) /* number of input symbols */
{
int i, j;

puts("STATE TRANSITION TABLE");

/* input symbols: '0', '1', ... */


printf(" | ");
for (i = 0; i < nsymbols; i++) printf(" %c ", '0'+i);

printf("
-----+--");
for (i = 0; i < nsymbols; i++) printf("-----");
printf("
");

for (i = 0; i < nstates; i++) {


printf(" %c | ", 'A'+i); /* state */
for (j = 0; j < nsymbols; j++)
printf(" %c ", 'A'+tab[i][j]);
printf("
");
}
}

/*
Initialize NFA table.
*/
void init_NFA_table()
{
/*
NFA table for ex.21 at p.76

NFAtab[0][0] = "01";
NFAtab[0][1] = "0";
NFAtab[1][0] = "";
NFAtab[1][1] = "01";

NFA_states = 2;
DFA_states = 0;
N_symbols = 2;
*/
/*
NFA table for ex.17 at p.72
*/
NFAtab[0][0] = "12";
NFAtab[0][1] = "13";
NFAtab[1][0] = "12";
NFAtab[1][1] = "13";
NFAtab[2][0] = "4";
NFAtab[2][1] = "";
NFAtab[3][0] = "";
NFAtab[3][1] = "4";
NFAtab[4][0] = "4";
NFAtab[4][1] = "4";

NFA_states = 5;
DFA_states = 0;
N_symbols = 2;
}

/*
String 't' is merged into 's' in an alphabetical order.
*/
void string_merge(char *s, char *t)
{
char temp[STATES], *r=temp, *p=s;

while (*p && *t) {


if (*p == *t) {
*r++ = *p++; t++;
} else if (*p < *t) {
*r++ = *p++;
} else
*r++ = *t++;
}
*r = '';

if (*p) strcat(r, p);


else if (*t) strcat(r, t);

strcpy(s, temp);
}

void get_next_state(char *nextstates, char *cur_states,


char *nfa[STATES][SYMBOLS], int n_nfa, int symbol)
{
int i;
char temp[STATES];

temp[0] = '';
for (i = 0; i < strlen(cur_states); i++)
string_merge(temp, nfa[cur_states[i]-'0'][symbol]);
strcpy(nextstates, temp);
}

int state_index(char *state, char statename[][STATES], int *pn)


{
int i;

if (!*state) return -1; /* no next state */

for (i = 0; i < *pn; i++)


if (!strcmp(state, statename[i])) return i;

strcpy(statename[i], state); /* new state-name */


return (*pn)++;
}

int nfa_to_dfa(char *nfa[STATES][SYMBOLS], int n_nfa,


int n_sym, int dfa[][SYMBOLS])
{
char statename[STATES][STATES];
int i = 0; /* current index of DFA */
int n = 1; /* number of DFA states */

char nextstate[STATES];
int j;

strcpy(statename[0], "0"); /* start state */

for (i = 0; i < n; i++) { /* for each DFA state */


for (j = 0; j < n_sym; j++) { /* for each input symbol */
get_next_state(nextstate, statename[i], nfa, n_nfa, j);
dfa[i][j] = state_index(nextstate, statename, &n);
}
}

return n; /* number of DFA states */


}

void main()
{
clrscr();
init_NFA_table();
DFA_states = nfa_to_dfa(NFAtab, NFA_states, N_symbols, DFAtab);
put_dfa_table(DFAtab, DFA_states, N_symbols);
getch();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy