A finite state machine is a convenient way to model the beha

A finite state machine is a convenient way to model the behavior of various systems. To do this, it allows the system to be in various states, and uses set rules to determine when to switch to other ones. As a simple example, imagine a vending machine that sells candy for 10 phi and accepts nickels and dimes. The machine could have three states- no money has been inserted, 5 hpi has been, and 10 phi has been. A nickel would move it from 0 to 5 or 5 to 10, a dime would move it from any state to 10 (sorry, no change), and pushing the dispense button would set the machine back to 0 and, more importantly, give you candy! For this program, you will use a finite state machine to help tokenize a string, splitting it every time a space shows up, unless that space is between quotation marks. Thankfully, your TAs have been gracious enough to do most of the work for you. All that\'s left in the project is implementing the function: void transition (char c, int *state); This function will take the latest character and the current state to figure out which state it transitions to and outputs the result to the same variable. To actually determine the next state, use these rules: If *state equals 0, we\'re waiting for something that isn\'t a space. Stay in state 0 if c is a space, switch to state 1 if c is a quotation mark, or switch to state 2 if c is anything else. If *state equals, we\'ve already seen a quotation mark and are waiting for its match. Switch to state 2 if we find it (i.e. c is a quotation mark), or stay in state 1 if c is anything else. If *state equals 2, we\'re in a word and waiting for a space to end it. Switch to state 0 if c is a space, switch to state 1 if c is a quotation mark, or stay in state 2 if c is anything else. You will create a function called void transition(char c, int *state); and place its definition in a file called hw4p6.c and place the function prototype in a file called hw4p6.h and hand in both files

Solution

Hope this will help-

#include <stdio.h>

static const char initial_state = \'0\';
static const char accepting_state = \'3\';
int main(int argc, char* argv[])
{
int x;
char current_state;

current_state = initial_state;
printf(\"Enter a series of characters (0 or 1)\ \");
printf(\"Press <ctrl-d> to finish.\ \");
printf(\"> \");
while(scanf(\"%d\", &x)!=EOF)
{
     x += \'0\';
         switch(current_state)
       {
      case \'0\': /*initial state*/
       switch(x)
       {
       case\'0\':current_state=\'1\'; break;
       case\'1\':current_state=\'0\'; break;
       default: goto fail;
       }
       break;
     case \'1\': /*Last input was 0*/
      switch(x)
      {
      case\'0\':current_state=\'1\'; break;
      case\'1\':current_state=\'2\'; break;
      default: goto fail;
      }
      break;
     case \'2\': /*Last input was 1*/
      switch(x)
      {
      case\'0\':current_state=\'3\'; break;
      case\'1\':current_state=\'0\'; break;
      default: goto fail;
      }
      break;
     case \'3\': /*Last input was 0*/
      switch(x)
      {
      case\'0\':current_state=\'3\'; break;
      case\'1\':current_state=\'3\'; break;
      default: goto fail;
      }
      break;
     default: goto fail;
     }
     printf(\"> \");
}

if (current_state == accepting_state) {
    printf(\"Language accepts!\ \");
} else {
    printf(\"Language rejects.\ \");
}
return 0;

fail:
printf(\"Invalid input\ \");
return 1;
}

 A finite state machine is a convenient way to model the behavior of various systems. To do this, it allows the system to be in various states, and uses set rul
 A finite state machine is a convenient way to model the behavior of various systems. To do this, it allows the system to be in various states, and uses set rul

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site