Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ue date: 6/5/2017 (Fri) 11:59 PM http://uml.umassonline.net Submission: C source

ID: 3850968 • Letter: U

Question

ue date: 6/5/2017 (Fri) 11:59 PM http://uml.umassonline.net Submission: C source file, Assgn7.c, on Blackboard Description: 13. A finite state machine (FSM) consists of a set of states, a set of transitions, and a string of data. In the FSM of Fig. 6.19, the named ovals repre- sent states, and the arrows connecting the states represent transitions. The FSM is designed to recognize a list of C identifiers and nonnegative integers, assuming that the items are ended by one or more blanks and that a period marks the end of all the data. The following table traces how the diagrammed machine would process a string composed of one blank, the digits 9 and 5, two the K, the digit 9, one blank, and a period. The machine begins in the start state. FIGURE 6.19 output identifier Message Blank (8) identifier Finite State Machine for Numbers and Letter (4) build id Identifiers Underscore (7) llltart Blank (3) Letter (5) Digit (6) Digit (1) Period (2) build num Blank (10) stop number Digit (9) Output Number Message

Explanation / Answer

Here is the code and output for the question.

Please note : The input should be well formed (i.e. valid without errors) and can only use spaces in between the words/ numbers. This requirement of being well formatted is specified in the question itself. So the program does not check for invalid inputs. Use period ( .) at the end of input before pressing enter key.

In case of any issues, please post a comment and I shall respond to it. Please don't forget to rate the answer if it helped. Thank you very much.

#include <stdio.h>

#include <ctype.h>

/*define the states of FSM*/

int start = 0, stop =1, build_num = 2, build_id = 3, number = 4, identifier = 5;

int transition(int current_state, char transition_char);

int main()

{

  

int current_state;

char transition_char;

printf(" Enter a line of data to be scanned: ");

  

current_state = start;

do{

if(current_state == identifier)

{

printf(" - Identifier ");

current_state = start;

}

else if (current_state == number)

{

printf(" - Number ");

current_state = start;

}

scanf("%c", &transition_char);

  

if(transition_char != ' ')

printf("%c", transition_char);

current_state = transition(current_state, transition_char);

}while(current_state != stop);

}

int transition(int current_state, char transition_char)

{

int next_state = current_state;

if(current_state == start)

{

if(isdigit(transition_char)) //Digit

next_state = build_num;

else if(transition_char == '.') //Period

next_state = stop;

else if(transition_char == ' ') //Blank

next_state = start;

else if(isalpha(transition_char)) //Letter

next_state = build_id;

}

else if(current_state == stop)

{

next_state = stop;

}

else if(current_state == build_num)

{

if(isdigit(transition_char) || transition_char == '.') //decimal point or digit

next_state = build_num;

else if(transition_char == ' ')

next_state = number;

}

else if(current_state == build_id)

{

if(isalpha(transition_char) || isdigit(transition_char) || transition_char == '_')

next_state = build_id;

else if(transition_char == ' ')

next_state = identifier;

}

  

return next_state;

}

output

Enter a line of data to be scanned:
rate R2D2 48 2 time 555666 .
rate - Identifier
R2D2 - Identifier
48 - Number
2 - Number
time - Identifier
555666