Professional Documents
Culture Documents
Break a program up into tokens - these tokens can usually be described using deterministic nite automata pay = salary + ( overtimerate overtime ) ;
Can be used a model for what happens during lexical analysis scan program from beginning to end and divide it into tokens. Finite automata are used to specify tokens of programming languages. Also used in model checking, reasoning about systems with objective of proving they satisfy useful properties. Also used in statistical models for analysing biological and textual sequences.
Paul Goldberg
1 / 23
Paul Goldberg
2 / 23
We shall see that nite automata can be used to describe any nite set of strings various innite sets of strings, e.g.
finite memory
strings having exactly 2 occurrences of the letter a strings having more than 6 letters strings in which letter b never comes before letter a
Gives method of recognising words belonging to certain languages (must accept or fail to accept at end of string)
Paul Goldberg
3 / 23
Paul Goldberg
4 / 23
Textbook denition
A deterministic nite automaton (DFA) has 5 components: Furthermore we shall see that nite automata cannot be used to describe certain languages such as the set of strings containing more as than bs all words that remain the same if you read them back to front well-formed arithmetic expressions, if there is no limit on nesting of parentheses.
1
Q is a nite nonempty set whose members are called states of the automaton; A is a nite nonempty set called the alphabet of the automaton; is a map from Q A to Q called the transition function of the automaton; i is a member of Q and is called the initial state; T is a nonempty subset of Q whose members are called terminal states or accepting states.
4 5
Paul Goldberg
5 / 23
Paul Goldberg
6 / 23
quintuple any DFA can be divided into these 5 components state of a machine tells you something about the prex that has been read so far. If the string is a member of the language of interest, the state reached when the whole string has been scanned will be an accepting state (a member of T ). There is only one empty string so there is only one initial state (denoted i ) Transition function tells you how state should change when an additional letter is read by the DFA A DFA is often depicted as a labelled directed graph. (called transition diagram)
r 0 i 1 t
0,1
0,1
Example computation
r 0 i 1 t
0,1
0,1
i
1 t 0,1
0,1
Input word 110100
symbol state
1 1 0 1 0 0 i t t t t t
COMP 218; slides 2, 2012-13 10 / 23
Paul Goldberg
A simplication
If is a partial function (not dened for some state/letter pairs), then the DFA rejects an input if it ever encounters such a pair. This convention often simplies the denition of a DFA. In the previous example we could use transition table 0 i t t 1 t t
i t r
Paul Goldberg
11 / 23
Paul Goldberg
12 / 23
Example
Give a DFA that accepts the words cat and dog (using the 6-letter alphabet a, c, d, g, o, t).
Example
The DFA can be stripped down if we understand an undened transition to mean: reject the whole string.
a,g,o,t
c,
d,
g,
o,
, ,c
d,
g,
a,c,d,g,o
, ,d g, t o,
a,c,d,o,t
a, c, d,
t
a
,c
g,
g
o, t
a,c,d,g,o,t
Paul Goldberg
13 / 23
Paul Goldberg
14 / 23
An important observation
Exercises
Any DFA that uses the convention that an undened transition leads to a rejection, can be converted to a DFA that uses a total transition function (that is, one that is dened for all combinations of input symbols and states). The convention is useful, but it does not add extra expressive power.
Draw a diagram for a DFA that only accepts the words: fun, fair, unfair, funfair Note. this language can be represented by simple enumeration: in set-theoretic notation {fun, fair , unfair , funfair } How about the following innite language. Can you give a DFA that accepts the words: bad, baad, baaad, baaaad, ...?
Paul Goldberg
15 / 23
Paul Goldberg
16 / 23
General observations
Any nite language (and various innite languages) have DFAs that recognise them. Question: What sort of languages can be recognised by DFAs? Note: although nite languages can be enumerated, it may be advantageous to describe them using a DFA (e.g. words of length 10)
General rule: i is the initial state. For each prex p of a word in the list, include state sp with the idea that the machine should be in state sp after p has been read. If p is equal to one of the given words, make sp an accepting state. For prexes p and q with q = p a (a A), let (sp , a) = sq If p a is not a prex of any word, (sp , a) = f (failure state) For all a A, (i , a) = sa . Try using this rule to construct a DFA that accepts a, cat, cats, dog, deer
Paul Goldberg
17 / 23
Paul Goldberg
18 / 23
Example
More examples
Construct a DFA that accepts words over the alphabet {a, b} which contain an odd number of as and an even number of bs. State should keep track of parity of number of occurrences of each letter seen so far. So: this suggests using 4 states, called E /E , E /O , O /E , O /O where i = E /E . (E /E , a) = O /E (E /E , b) = E /O etc. T = {O /E }
Write down DFAs which recognise the following languages over the alphabet {a, b}: L1 is set of all words containing exactly three occurrences of a L2 is set of all words containing at least three occurrences of a L3 is set of words containing the substring aaa
Paul Goldberg
19 / 23
Paul Goldberg
20 / 23
Initially the state is i and if the input word is w = a1 a2 . . . an then, as each letter is read, the state changes and we get q1 , q2 , . . . , qn dened by q1 = (i , a1 ) q2 = (q1 , a2 ) q3 = (q2 , a3 ) . . . . . . . . . qn = (qn1 , an )
Paul Goldberg
21 / 23
Paul Goldberg
22 / 23
Language dened by a DFA Suppose we have a DFA A. A word w A is said to be accepted or recognised by A if (i , w ) T , otherwise it is said to be rejected. The set of all words accepted by A is called the language accepted by A and will be denoted by L(A). Thus L(A) = {w A : (i , w ) T }.
Paul Goldberg
23 / 23