Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sahar Mosleh California State University San MarcosPage 1 Finite State Machine.

Similar presentations


Presentation on theme: "Sahar Mosleh California State University San MarcosPage 1 Finite State Machine."— Presentation transcript:

1 Sahar Mosleh California State University San MarcosPage 1 Finite State Machine

2 Sahar Mosleh California State University San MarcosPage 2 FSM A finite state machine ( FSM ) is a machine or a program that changes state based on some input. It is fairly simple to use a graph to represent a FSM, which contains squares (circles) called nodes and lines with arrows between the circles called edges. The following is simple example of FSM. Each node represents a program state and each edge represents a transition from one state to another. One node is designed as the start state, shown with incoming arrow. The remaining state can be labeled with numbers or letters.   B Start

3 Sahar Mosleh California State University San MarcosPage 3 One or more state are designed as terminal states, notated by a thick border around the square. A terminal state represent a state in which the program might stop without producing an error. A finite state machine is a specific instance of more general type of structure called direct graph.

4 Sahar Mosleh California State University San MarcosPage 4 Validating an input string The program that read input stream often must validate their input by performing a certain amount of error checking. A programming language compiler for instance can use a finite- state machine to scan source program and convert words and symbols into objects of keyword, arithmetic operation and identifier. When using a finite state machine to check the validity of an input string, you usually read the input character by character. Each character is represented by an edge in the diagram. A FSM detects illegal input squares in one of two ways The next input character does not correspond to any transition from the current state. The end of input is reached and the current state is a nonterminal state.

5 Sahar Mosleh California State University San MarcosPage 5 Character string example. Lets check the validity of an input string according to the following two rules: The string must begin with the letter “x” and end with letter “z”. Between the first and last character, there can be zero or more letters within the range {‘a’…’y’}. The FSM diagram describe this syntax is: Each transition is identified with a particular type of input. The transition from state A to state B can only be accomplished if the letter x is read from the input. A C B ‘a’..’y’ ‘z’ start‘x’

6 Sahar Mosleh California State University San MarcosPage 6 A transition from B to itself is accomplished by the input of any alphabet letter except z. A transition from state B to state C occurred only when the letter z is read from input stream. if the end of stream is reached while the program is in state A or B, an error condition results because only state C is marked as a terminal state. The following input string, for example, would be recognized by this FSM: xaabcdefgz xz xyyqqrrstuvz

7 Sahar Mosleh California State University San MarcosPage 7 Validating a singe integer. A finite- state machine for parsing a signed integer is shown in following diagram. Input consists of an optional leading sign followed by a sequence of digits. FSM are very easy to translate into assembly language code. Each state in the diagram ( A,B,C,…) is represented in the program by label. The following actions are performed at each label: A call to an input procedure reads the next character from input If the state is a terminal state, check to see if the user has pressed the Enter Key to end the input. One or more computer instruction check for each possible transition leading away from the sate. Each comparison is followed by a conditional jump instruction A C B digits digit start digit +, -


Download ppt "Sahar Mosleh California State University San MarcosPage 1 Finite State Machine."

Similar presentations


Ads by Google