SlideShare a Scribd company logo
1 of 4
Download to read offline
Write a C program that converts an NFA file to a DFA, where the language of the NFA consists
of lowercase letters of the alphabet. Then have it be able to determine whether strings in a string
file are in the language or not. The program must take the two files as command line arguments
(do not prompt the user).
Solution
#include
#include
#define STATES 256
#define SYMBOLS 20
int N_symbols;
int NFA_states;
char *NFAtab[STATES][SYMBOLS];
int DFA_states; /* number of DFA states */
int DFAtab[STATES][SYMBOLS];
/*Print state-transition table.*/
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()
{
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 = '0';
if (*p) strcat(r, p);
else if (*t) strcat(r, t);
strcpy(s, temp);
}
/*Get next-state string for current-state string.*/
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] = '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)++;
}
/*
Convert NFA table to DFA table.
Return value: number of DFA states.
*/
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()
{
init_NFA_table();
DFA_states = nfa_to_dfa(NFAtab, NFA_states, N_symbols, DFAtab);
put_dfa_table(DFAtab, DFA_states, N_symbols);
}
#include

More Related Content

Similar to Write a C program that converts an NFA file to a DFA, where the lang.pdf

So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
ezonesolutions
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
Adamq0DJonese
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
mohdjakirfb
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
amitbagga0808
 
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdfThe Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
bhim1213
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
Aggarwalelectronic18
 
write the code for Part 1 ContextFree Grammars Create con.pdf
write the code for Part 1  ContextFree Grammars Create con.pdfwrite the code for Part 1  ContextFree Grammars Create con.pdf
write the code for Part 1 ContextFree Grammars Create con.pdf
aaryanentp
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
stn_tkiller
 
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
apleather
 
Unit 4
Unit 4Unit 4
Unit 4
siddr
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
raosir123
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdf
ezzi552
 

Similar to Write a C program that converts an NFA file to a DFA, where the lang.pdf (20)

So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
 
Input And Output
 Input And Output Input And Output
Input And Output
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
 
Gps c
Gps cGps c
Gps c
 
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdfThe Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
 
FMDB - SLC-Cocoaheads
FMDB - SLC-CocoaheadsFMDB - SLC-Cocoaheads
FMDB - SLC-Cocoaheads
 
write the code for Part 1 ContextFree Grammars Create con.pdf
write the code for Part 1  ContextFree Grammars Create con.pdfwrite the code for Part 1  ContextFree Grammars Create con.pdf
write the code for Part 1 ContextFree Grammars Create con.pdf
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
 
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
 
Unit 4
Unit 4Unit 4
Unit 4
 
week-6x
week-6xweek-6x
week-6x
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
C programming
C programmingC programming
C programming
 
For this project, you must complete the provided partial C++ program.pdf
For this project, you must complete the provided partial C++ program.pdfFor this project, you must complete the provided partial C++ program.pdf
For this project, you must complete the provided partial C++ program.pdf
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdf
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
 

More from info998421

I need help with two things. First Id like someone to check over m.pdf
I need help with two things. First Id like someone to check over m.pdfI need help with two things. First Id like someone to check over m.pdf
I need help with two things. First Id like someone to check over m.pdf
info998421
 
Explain Internal Controls and Corporate Governance.SolutionInt.pdf
Explain Internal Controls and Corporate Governance.SolutionInt.pdfExplain Internal Controls and Corporate Governance.SolutionInt.pdf
Explain Internal Controls and Corporate Governance.SolutionInt.pdf
info998421
 
Environmental Laws and Acts Project Research all of the following law.pdf
Environmental Laws and Acts Project Research all of the following law.pdfEnvironmental Laws and Acts Project Research all of the following law.pdf
Environmental Laws and Acts Project Research all of the following law.pdf
info998421
 
discuss the drawbacks of programmed and interrupt driven io and des.pdf
discuss the drawbacks of programmed and interrupt driven io and des.pdfdiscuss the drawbacks of programmed and interrupt driven io and des.pdf
discuss the drawbacks of programmed and interrupt driven io and des.pdf
info998421
 
Describe the structure and location of the parathyroid glands.Desc.pdf
Describe the structure and location of the parathyroid glands.Desc.pdfDescribe the structure and location of the parathyroid glands.Desc.pdf
Describe the structure and location of the parathyroid glands.Desc.pdf
info998421
 
Describe how the composition of MALT tissue bypasses the need for.pdf
Describe how the composition of MALT tissue bypasses the need for.pdfDescribe how the composition of MALT tissue bypasses the need for.pdf
Describe how the composition of MALT tissue bypasses the need for.pdf
info998421
 
Year d) interactions with other species for space or resources-classi.pdf
Year d) interactions with other species for space or resources-classi.pdfYear d) interactions with other species for space or resources-classi.pdf
Year d) interactions with other species for space or resources-classi.pdf
info998421
 
Write a program that creates a linked list of families. Each family .pdf
Write a program that creates a linked list of families. Each family .pdfWrite a program that creates a linked list of families. Each family .pdf
Write a program that creates a linked list of families. Each family .pdf
info998421
 
Which of these hypotheses cannot be tested by an experiment Biologi.pdf
Which of these hypotheses cannot be tested by an experiment  Biologi.pdfWhich of these hypotheses cannot be tested by an experiment  Biologi.pdf
Which of these hypotheses cannot be tested by an experiment Biologi.pdf
info998421
 
Which is the mode of inheritance that is most likely [That is the mo.pdf
Which is the mode of inheritance that is most likely [That is the mo.pdfWhich is the mode of inheritance that is most likely [That is the mo.pdf
Which is the mode of inheritance that is most likely [That is the mo.pdf
info998421
 
What happens at the mid-blastula transition in Xenopus Which part o.pdf
What happens at the mid-blastula transition in Xenopus  Which part o.pdfWhat happens at the mid-blastula transition in Xenopus  Which part o.pdf
What happens at the mid-blastula transition in Xenopus Which part o.pdf
info998421
 

More from info998421 (20)

If Mendel had chosen factors to follow in his pea plants poorly,.pdf
If Mendel had chosen factors to follow in his pea plants poorly,.pdfIf Mendel had chosen factors to follow in his pea plants poorly,.pdf
If Mendel had chosen factors to follow in his pea plants poorly,.pdf
 
If the significance level of a test is increased (i.e. increased fr.pdf
If the significance level of a test is increased (i.e.  increased fr.pdfIf the significance level of a test is increased (i.e.  increased fr.pdf
If the significance level of a test is increased (i.e. increased fr.pdf
 
I need an inttroduction for my paper on Conducting and Analyzing Sta.pdf
I need an inttroduction for my paper on Conducting and Analyzing Sta.pdfI need an inttroduction for my paper on Conducting and Analyzing Sta.pdf
I need an inttroduction for my paper on Conducting and Analyzing Sta.pdf
 
Identify the contributions to Microbiology made by Redi, Tyndall, an.pdf
Identify the contributions to Microbiology made by Redi, Tyndall, an.pdfIdentify the contributions to Microbiology made by Redi, Tyndall, an.pdf
Identify the contributions to Microbiology made by Redi, Tyndall, an.pdf
 
I need help with two things. First Id like someone to check over m.pdf
I need help with two things. First Id like someone to check over m.pdfI need help with two things. First Id like someone to check over m.pdf
I need help with two things. First Id like someone to check over m.pdf
 
Explain Internal Controls and Corporate Governance.SolutionInt.pdf
Explain Internal Controls and Corporate Governance.SolutionInt.pdfExplain Internal Controls and Corporate Governance.SolutionInt.pdf
Explain Internal Controls and Corporate Governance.SolutionInt.pdf
 
Environmental Laws and Acts Project Research all of the following law.pdf
Environmental Laws and Acts Project Research all of the following law.pdfEnvironmental Laws and Acts Project Research all of the following law.pdf
Environmental Laws and Acts Project Research all of the following law.pdf
 
discuss the drawbacks of programmed and interrupt driven io and des.pdf
discuss the drawbacks of programmed and interrupt driven io and des.pdfdiscuss the drawbacks of programmed and interrupt driven io and des.pdf
discuss the drawbacks of programmed and interrupt driven io and des.pdf
 
do it on excel and please kindly upload the excel file. or explain m.pdf
do it on excel and please kindly upload the excel file. or explain m.pdfdo it on excel and please kindly upload the excel file. or explain m.pdf
do it on excel and please kindly upload the excel file. or explain m.pdf
 
Describe the structure and location of the parathyroid glands.Desc.pdf
Describe the structure and location of the parathyroid glands.Desc.pdfDescribe the structure and location of the parathyroid glands.Desc.pdf
Describe the structure and location of the parathyroid glands.Desc.pdf
 
Describe how the composition of MALT tissue bypasses the need for.pdf
Describe how the composition of MALT tissue bypasses the need for.pdfDescribe how the composition of MALT tissue bypasses the need for.pdf
Describe how the composition of MALT tissue bypasses the need for.pdf
 
C. wye D. delta E. not defined Question 6 What is the configuration o.pdf
C. wye D. delta E. not defined Question 6 What is the configuration o.pdfC. wye D. delta E. not defined Question 6 What is the configuration o.pdf
C. wye D. delta E. not defined Question 6 What is the configuration o.pdf
 
A bullet that lodges in the heart would a. be located in the ventra.pdf
A bullet that lodges in the heart would  a. be located in the ventra.pdfA bullet that lodges in the heart would  a. be located in the ventra.pdf
A bullet that lodges in the heart would a. be located in the ventra.pdf
 
Year d) interactions with other species for space or resources-classi.pdf
Year d) interactions with other species for space or resources-classi.pdfYear d) interactions with other species for space or resources-classi.pdf
Year d) interactions with other species for space or resources-classi.pdf
 
Write a program that creates a linked list of families. Each family .pdf
Write a program that creates a linked list of families. Each family .pdfWrite a program that creates a linked list of families. Each family .pdf
Write a program that creates a linked list of families. Each family .pdf
 
Why do Radicals maintain capitalism will always tend to enter into c.pdf
Why do Radicals maintain capitalism will always tend to enter into c.pdfWhy do Radicals maintain capitalism will always tend to enter into c.pdf
Why do Radicals maintain capitalism will always tend to enter into c.pdf
 
Which of these hypotheses cannot be tested by an experiment Biologi.pdf
Which of these hypotheses cannot be tested by an experiment  Biologi.pdfWhich of these hypotheses cannot be tested by an experiment  Biologi.pdf
Which of these hypotheses cannot be tested by an experiment Biologi.pdf
 
Which is the mode of inheritance that is most likely [That is the mo.pdf
Which is the mode of inheritance that is most likely [That is the mo.pdfWhich is the mode of inheritance that is most likely [That is the mo.pdf
Which is the mode of inheritance that is most likely [That is the mo.pdf
 
Which of the following is a good example of homoplasy (convergent ev.pdf
Which of the following is a good example of homoplasy (convergent ev.pdfWhich of the following is a good example of homoplasy (convergent ev.pdf
Which of the following is a good example of homoplasy (convergent ev.pdf
 
What happens at the mid-blastula transition in Xenopus Which part o.pdf
What happens at the mid-blastula transition in Xenopus  Which part o.pdfWhat happens at the mid-blastula transition in Xenopus  Which part o.pdf
What happens at the mid-blastula transition in Xenopus Which part o.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

Write a C program that converts an NFA file to a DFA, where the lang.pdf

  • 1. Write a C program that converts an NFA file to a DFA, where the language of the NFA consists of lowercase letters of the alphabet. Then have it be able to determine whether strings in a string file are in the language or not. The program must take the two files as command line arguments (do not prompt the user). Solution #include #include #define STATES 256 #define SYMBOLS 20 int N_symbols; int NFA_states; char *NFAtab[STATES][SYMBOLS]; int DFA_states; /* number of DFA states */ int DFAtab[STATES][SYMBOLS]; /*Print state-transition table.*/ 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(" ");
  • 2. } } /*Initialize NFA table.*/ void init_NFA_table() { 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 = '0'; if (*p) strcat(r, p); else if (*t) strcat(r, t); strcpy(s, temp); }
  • 3. /*Get next-state string for current-state string.*/ 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] = '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)++; } /* Convert NFA table to DFA table. Return value: number of DFA states. */ 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);
  • 4. dfa[i][j] = state_index(nextstate, statename, &n); } } return n; /* number of DFA states */ } void main() { init_NFA_table(); DFA_states = nfa_to_dfa(NFAtab, NFA_states, N_symbols, DFAtab); put_dfa_table(DFAtab, DFA_states, N_symbols); } #include