Thread: Finite state machine

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    1

    Question Finite state machine

    hello i'm wildan from indonesia,
    i just begin to studying C language then i have a home work. i can't solved it..

    A finite state machine (FSM) consists of a set of states, a set of transitions,
    and a string of input data. In the FSM of Fig. 6.19 , the named ovals represent
    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
    blanks, the letter K, the digit 9, one blank, and a period. The machine begins



    in the start state.
    Trace of Fig. 6.19 FSM on data “ 95 K9 .”
    State Next Character Transition
    start ' ' 3
    start '9' 1
    build_num '5' 9
    build_num ' ' 10
    number Output number message
    start ' ' 3
    start 'K' 4
    build_id '9' 6
    build_id ' ' 8
    identifier Output identifier message
    start ' . ' 2
    stop
    Write a program that uses an enumerated type to represent the names
    of the states. Your program should process a correctly formatted line of data,
    identifying each data item. Here is a sample of correct input and output.
    Input :
    rate R2D2 48 2 time 555666
    Output :
    rate – Identifier
    R2D2 – Identifier
    48 – Number
    2 – Number
    time – Identifier
    555666 – Number
    Use the following code fragment in main , and design function transition
    to return the next state for all the numbered transitions of the finite state
    machine. If you include the header file ctype.h , you can use the library
    function isdigit which returns 1 if called with a digit character, 0 otherwise.
    Similarly, the function isalpha checks whether a character is a letter.
    When your program correctly models the behavior of the FSM shown,
    extend the FSM and your program to allow optional signs and optional
    fractional parts (i.e., a decimal point followed by zero or more digits)
    in numbers.

    Code:
    current_state = start;
    do {
    if (current_state == identifier) {
    printf(" - Identifier\n");
    current_state = start;
    } else if (current_state == number) {
    printf(" - Number\n");
    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);

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest asking a question about what you are having a problem with.
    I suggest posting code that is complete enough to compile.
    I suggest indenting your code so it is more readable.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FSM(Finite state machine) help
    By sed_y in forum C++ Programming
    Replies: 4
    Last Post: 11-07-2011, 04:40 PM
  2. Main loop of the Finite State Machine
    By makonikor in forum C Programming
    Replies: 3
    Last Post: 11-18-2010, 11:54 PM
  3. spell checker using finite state machine
    By DurellP in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2010, 02:00 AM
  4. Finite State Machine
    By ArlexBee-871RBO in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 11:59 AM
  5. Finite State Machine Project Help
    By ryanbradley in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2004, 10:23 AM