Deterministic Finite Automata Section 2 1 Mon Sep

  • Slides: 20
Download presentation
Deterministic Finite Automata Section 2. 1 Mon, Sep 19, 2005

Deterministic Finite Automata Section 2. 1 Mon, Sep 19, 2005

Intuitive View of an Automaton An automaton is a machine that has an input

Intuitive View of an Automaton An automaton is a machine that has an input tape and can be put into any of several states. p A string of symbols is written on the tape before execution. p The automaton begins by reading the symbols on the tape, from left to right. p Upon reading a symbol from the tape, the machine (possibly) changes state and advances the tape. p After reading the last symbol, the machine halts. p The last state tells the result of the processing (accept or reject). p

More Precise View of an Automaton Divide the states into two groups: final (accepting)

More Precise View of an Automaton Divide the states into two groups: final (accepting) an nonfinal (rejecting). p Begin in the designated start state. p Upon reading a symbol on the tape, make a transition to the appropriate state. p

Transition Diagrams p In a transition diagram, n n Each state is represented by

Transition Diagrams p In a transition diagram, n n Each state is represented by a circle. Each final state is represented by a circle within a circle. Transitions are represented by arrows from one state to another state. Transitions are labeled with an input symbol. Final states are indicated by a circle within a circle. p The following diagram represents the transition from state p to state q upon reading the symbol a. p p a q

Example of an Automaton Describe an automaton that reports whether the input string begins

Example of an Automaton Describe an automaton that reports whether the input string begins with a 0. p Describe an automaton that reports whether the input string ends with a 0. p Describe an automaton that would read the input tape and report whether the tape contained an even number or an odd number of symbols. p Describe an automaton that would read a binary string and report whether the string contained an even number of 0 s and an odd number of 1 s. p

Some Special DFAs Let = {0, 1}. p Design a DFA that accepts no

Some Special DFAs Let = {0, 1}. p Design a DFA that accepts no string. p Design a DFA that accepts every string. p Design a DFA that accepts only strings of length 1. p Design a DFA that accepts only strings of length 2. p Design a DFA that accepts only the empty string. p

Definition of a Deterministic Finite Automaton p A deterministic finite automaton (DFA) is a

Definition of a Deterministic Finite Automaton p A deterministic finite automaton (DFA) is a quintuple (K, , , s, F) where n n n K is a finite set of states. is a finite input alphabet. s K is the initial (start) state. F K is the set of final states. is the transition function from K to K, i. e. , K K.

The Functioning of a DFA The DFA begins in the initial state s. p

The Functioning of a DFA The DFA begins in the initial state s. p Upon reading an input symbol, the DFA changes state according to the rule expressed by the transition function . p After reading the last symbol on the tape, the DFA halts. p The input is accepted if the final state is in F. Otherwise, the input is rejected. p

Configurations Given a DFA M, a configuration is a pair (q, w) where q

Configurations Given a DFA M, a configuration is a pair (q, w) where q is the current state of M and w is the remainder of the input string. p Given configuration (q, w), if the next transition produces the configuration (q , w ), then we say that (q, w) yields (q , w ) in one step. p That is, w = aw' for some a and there is a transition (q, a) = q'. p This is denoted (q, w) M (q , w ). p

Configurations The configuration (q, w) yields the configuration (q , w ) if there

Configurations The configuration (q, w) yields the configuration (q , w ) if there is a sequence of configurations (q 1, w 1), (q 2, w 2), …, (qn, wn) such that (qi, wi) yields (qi + 1, wi + 1) in one step, for i = 1, …, n – 1, and (q 1, w 1) = (q, w) and (qn, wn) = (q , w ). p This is denoted (q, w) M* (q , w ). p The relation M* is the reflexive, transitive closure of the relation M. p

The Language of a DFA The string w is accepted by M if (s,

The Language of a DFA The string w is accepted by M if (s, w) M* (q, e) for some state q F. p The language of a DFA M, denoted L(M), is L(M) = {w * | M accepts w} or L(M) = {w * | q F, (s, w) M* (q, e)}. p

Examples of DFAs Let = {0, 1}. p Design a DFA whose language is

Examples of DFAs Let = {0, 1}. p Design a DFA whose language is the set of all strings containing 00 or 11. p Design a DFA whose language is the set of all strings that do not contain 00. p Design a DFA whose language is the set of all strings that contain 00, but not 11. p

C++ Tokens: Identifiers A C++ identifier is a string of letters, digits, and underscores

C++ Tokens: Identifiers A C++ identifier is a string of letters, digits, and underscores that begins with a letter. p Design a DFA that will accept C++ identifiers. p

C++ Tokens: Integers p p p A C++ integer may be expressed in decimal,

C++ Tokens: Integers p p p A C++ integer may be expressed in decimal, octal, or hexadecimal. In each base, the integer may begin with an optional + or – sign and end with an optional L or l. Decimal n p Octal n p One or more decimal digits (0 – 9), first digit nonzero. An initial 0, followed by one or more octal digits (0 – 7). Hexadecimal n An initial 0 x, followed by one or more hexadecimal digits (0 – 9, a – f, A – F).

C++ Tokens: Integers p Design a DFA that accepts C++ integers.

C++ Tokens: Integers p Design a DFA that accepts C++ integers.

C++ Tokens: Floating-Point Numbers A C++ floating-point number consists of the following. p The

C++ Tokens: Floating-Point Numbers A C++ floating-point number consists of the following. p The mantissa p n p The exponent n p A + or – sign, followed by zero or more digits, followed by a decimal point, followed by zero or more digits. An e or E, followed by a + or – sign, followed by one or more digits. The final F or f.

C++ Tokens: Floating-Point Numbers p The mantissa and exponent + or – signs are

C++ Tokens: Floating-Point Numbers p The mantissa and exponent + or – signs are optional. (-1. 23, 1. 23 e-4) p Digits before the decimal point are optional, provided there is at least one digit after the decimal point, and vice versa. (. 123 and 123. ) p The decimal point, the exponential part, and the final F are optional, provided at least one is used. (1230 f or 123 e 4 or 1230. )

C++ Tokens: Floating-Point Numbers p Design a DFA that accepts C++ floating-point numbers.

C++ Tokens: Floating-Point Numbers p Design a DFA that accepts C++ floating-point numbers.

Programming DFAs in C++ I have written a program that will read a description

Programming DFAs in C++ I have written a program that will read a description of a DFA from a file, and then simulate that DFA. p The project is named Universal DFA. mcp. p p The input file contains n n p A list of transitions. A list of final states. States n n Each state is represented as a nonnegative integer. 0 is the start state.

Simulating DFAs in C++ p Input symbols n p Transitions n p Each input

Simulating DFAs in C++ p Input symbols n p Transitions n p Each input symbol is a character. Each transition is of the form (state, symbol; state). Example n n = {0, 1}. The following DFA will accept all strings of even length. {(0, 0, 1), (0, 1, 1), (1, 0, 0), (1, 1, 0)} {0}