1.02k likes | 1.19k Views
Discrete Maths. 241-303 , Semester 1 2014-2015. Recognising input using: automata : a graph-based technique regular expressions : an algebraic technique equivalent to automata. 7 . Automata and Regular Expressions. Overview. 1. Introduction to Automata 2. Representing Automata
E N D
Discrete Maths 241-303, Semester 12014-2015 • Recognising input using: • automata: a graph-based technique • regular expressions: an algebraic technique • equivalent to automata 7. Automata andRegular Expressions
Overview 1. Introduction to Automata 2. Representing Automata 3. The ‘aeiou’ Automaton 4. Generating Output 5. Bounce Filter Example 6. Deterministic and Nondeterministic Automata continued
7. ‘washington’ Partial Anagrams 8. Regular Expressions 9. UNIX Regular Expressions 10. From REs to Automata 11. More Information
1. Introduction to Automata • A finite state automaton represents a problem as a series of states and transitions between the states • the automaton starts in an initial state • input causes a transition from the current state to another; • a state may be accepting • the automaton can terminate successfully when it enters an accepting state (if it wants to)
1.1. An Example The ‘even-odd’ Automaton b b • The states are the ovals. • The transitions are the arrows • labelled with the input that ‘trigger’ them • The ‘oddA’ state is accepting. start a evenA oddA a continued
Execution Sequence b a b a a evenA initial state • Input Move to State b a b a a evenA the automaton could choose to terminate here b a b a a oddA b a b a a oddA b a b a a evenA stops since no more input b a b a a oddA
1.2. Why are Automata Useful? • Automata are a very good way of modeling finite-state systems which change state due to input. Examples: • text editors, compilers, UNIX tools like grep • communications protocols • digital hardware components • e.g. adders, RAM very different applications
2. Representing Automata • Automata have a mathematical basis which allows them to be analysed, e.g.: • prove that they accept correct input • prove that they do not accept incorrect input • Automata can be manipulated to simplify them, and they can be automatically converted into code.
2.1. A Mathematical Coding • We can represent an automaton in terms of sets and mathematical functions. • The ‘even-odd’ automaton is: startSet = { evenA } acceptSet = { oddA } nextState(evenA, b) => evenAnextState(evenA, a) => oddAnextState(oddA, b) => oddAnextState(oddA, a) => evenA continued
Analysis of the mathematical form can show that the ‘even-odd’ automaton only accepts strings which: • contain an odd number of ‘a’s • e.g. • babaa abb abaab aabba aaaaba …
2.2. Automaton in Code • It is easy to (automatically) translate an automaton into code, but ... • an automaton graph does not contain all the details needed for a program • The main extra coding issues: • what to do when we enter an accepting state? • what to do when the input cannot be processed? • e.g. abzz is entered
Encoding the ‘even-odd’ Automaton enum state {evenA, oddA}; // possible statesenum state currState = evenA; // start stateint isAccepting = 0; // falseint ch;while ((ch = getchar()) != EOF)) { currState = nextState(currState, ch); isAccepting = acceptable(currState);}if (isAccepting) printf(“accepted\n);else printf(“not accepted\n”); accepting state only used at end of input continued
enum state nextState(enum state s, int ch){ if ((s == evenA) && (ch == ‘b’)) return evenA; if ((s == evenA) && (ch == ‘a’)) return oddA; if ((s == oddA) && (ch == ‘b’)) return oddA; if ((s == oddA) && (ch == ‘a’)) return evenA; printf(“Illegal Input”); exit(1);} simple handling of incorrect input continued
int acceptable(enum state s){ if (s == oddA) return 1; // oddA is an accepting state return 0;}
3. The ‘aeiou’ Automaton • What English words contain the five vowels (a, e, i, o, u) in order? • Some words that match: • abstemious • facetious • sacrilegious
3.1. Automaton Graph L = all letters L - a L - e L - i L - o L - u a e i o u start 0 1 2 3 4 5
3.2. Execution Sequence (1) • Input Move to State f a c e t i o u s 0 f a c e t i o u s 0 1 f a c e t i o u s f a c e t i o u s 1 continued
f a c e t i o u s 2 • Input Move to State f a c e t i o u s 2 f a c e t i o u s 3 f a c e t i o u s 4 the automaton can terminate here; no need to process more input f a c e t i o u s 5
Execution Sequence (2) • Input Move to State a n d r e w 0 a n d r e w 1 a n d r e w 1 1 a n d r e w continued
Input Move to State a n d r e w 1 a n d r e w 2 a n d r e w 2, and end of inputmeans failure
3.3. Translation to Code enum state {0, 1, 2, 3, 4, 5}; // poss. states enum state currState = 0; // start stateint isAccepting = 0; // falseint ch;while ((ch = getchar()) != EOF) && !isAccepting) { currState = nextState(currState, ch); isAccepting = acceptable(currState);}if (isAccepting) printf(“accepted\n);else printf(“not accepted\n”); stop processing when the accepting state is entered continued
enum state nextState(enum state s, int ch){ if (s == 0) { if (ch == ‘a’) return 1; else return 0; // input is L-a } if (s == 1) { if (ch == ‘e’) return 2; else return 1; // input is L-e } if (s == 2) { if (ch == ‘i’) return 3; else return 2; // input is L-i } : continued
: if (s == 3) { if (ch == ‘o’) return 4; else return 3; // input is L-o } if (s == 4) { if (ch == ‘u’) return 5; else return 4; // input is L-u } printf(“Illegal Input”); exit(1);} // end of nextState() simple handling of incorrect input
int acceptable(enum state s){ if (s == 5) return 1; // 5 is an accepting state return 0;}
4. Generating Output • One possible extension to the basic automaton idea is to allow output: • when a transition is ‘triggered’ there can be optional output as well • Automata which generate output are sometimes called Finite State Machines (FSMs).
4.1. ‘even-odd’ with Output b • When the ‘a’ transition is triggered out of the evenA state, then a ‘1’ is output. b a/1 start evenA oddA a
4.2. Mathematical Coding • Add an ‘output’ mathematical function to the automaton representation: output( evenA, a ) => 1
4.3. Extending the C Coding • The while loop for ‘even-odd’ will become: :while ((ch = getchar()) != EOF)) {output(currState, ch); currState = nextState(currState, ch); isAccepting = acceptable(currState);} : continued
The output() C function: void output(enum state s, int ch){ if ((s == evenA) && (ch == ‘a’)) putchar(‘1’);}
5. Bounce Filter Example • A signal processing problem: • a stream of 1’s and 0’s are ‘smoothed’ by the filter so that: • a single 0 surrounded by 1’s becomes a 1: ...111101111... => ...111111111... • a single 1 surrounded by 0’s becomes a 0 ...000010000... => ...000000000... • This kind of filtering is used in image processing to reduce ‘noise’.
5.1. The ‘bounce’ Automaton b d 1/1 0/0 smoothing 0/0 1/0 1/1 0/1 start a c 1/1 0/0
Notes • There is no accepting state • the code will simply terminate at EOF • The ‘a’ and ‘b’ states (left side) mostly have transitions that output ‘0’s. • The ‘c’ and ‘d’ states (right side) mostly have transitions that output ‘1’s.
5.2. Execution Sequence • Input Move to State Output 0 1 0 1 1 0 1 a 0 1 0 1 1 0 1 0 a 0 b 0 1 0 1 1 0 1 0 a 0 1 0 1 1 0 1 continued
Input Move to State Output 0 0 1 0 1 1 0 1 b 1 c 0 1 0 1 1 0 1 1 d 0 1 0 1 1 0 1 1 c 0 1 0 1 1 0 1 moved to righthand side
5.3. I/O Behaviour smoothed awayin the output • Input: 0 1 0 1 1 0 1Output: 0 0 0 0 1 1 1 • It takes 2 bits of the same type before the automaton realises that it has a new bit sequence rather than a ‘noise’ bit.
6. Deterministic and Nondeterministic Automata a • We have been writing deterministic automata so far: • for an input read by a state there is at most one transition that can be fired • state ‘s’ can process input ‘a’ and ‘w’, and fails for anything else S w
Nondeterministic Automata V a • A nondeterministic (ND) automaton can have 2 or more transitions with the same label leaving a state. • Problem: if state S sees input ‘x’, then which transition should it use? x T S x U
6.1. The ‘man’ Automaton • Accept all strings that contain “man” • this is hard to write as a deterministic automaton. The following has bugs: L - m WRONG start m a n 0 1 2 3 L - a L - n continued
The input string commandwill get stuck at state 0: 0 0 0 0 0 0 1 0 n m a d c o m the problem starts here
6.2. A ND Automaton Solution L • It is nondeterministic because an ‘m’ input in state 0 can be dealt with by two transitions: • a transition back to state 0, or • a transition to state 1 start m a n 0 1 2 3 continued
Processing command input: 0 0 0 0 0 0 0 0 n a d c o m m 2 1 3 acceptingstate n a fail: reject the input 1 m
6.3. Executing a ND Automata • It is difficult to code ND automata in conventional languages, such as C. • Two different coding approaches: • 1. When an input arrives, execute all transitions in parallel. See which succeeds. • 2. When an input arrives,try one transition. If it leads to failure then backtrack and try another transition.
Approach (1) in Parlog • A concurrent logic programming language. state0([X|Rest]) :- state0(Rest) : true.state0([m|Rest]) :- state1(Rest) : true.state1([a|Rest]) :- state2(Rest).state2([n|Rest]). concurrent testing Call:?- state0([c,o,m,m,a,n,d]).
Approach (2) in Prolog a sequential logic programming language nextState(0, _, 0).nextState(0, ‘m’, 1).nextState(1, ‘a’, 2).nextState(2, ‘n’, 3).nda(State, [Ch|Input]) :- nextState(State, Ch, NewState), nda(NewState, Input).nda(3, []). // accepting state the nondeterministic part Call:?- nda(0, [c,o,m,m,a,n,d]).
6.4. Why use ND Automata? • With nondeterminism, some problems are easier to solve/model. • Nondeterminism is common in some application areas, such as AI, graph search, and compilers. continued
It is possible to translate a ND automaton into a (larger, complex) deterministic one. • In mathematical terms, ND automata and determinstic automata are equivalent • they can be used to model all the same problems
7. ‘washington’ Partial Anagrams • Find all the words which can be made from the letters in “washington”. • There are nearly 400 words. Some of the 7-letter words: • agonist • goatish • showing • washing
7.1. A Two Stage Process • 1. Select all the words from a dictionary (e.g. /usr/share/dict/words on takasila) which use the letters in “washington” • use a deterministic automaton • 2. Delete the words which use the “washington” letters too many times (e.g. “hash”) • use a nondeterministic automaton
7.2. Stage 1: Deterministic Automaton • Send each word in the dictionary through the automaton: S = {w,a,s,h,i,n,g,t,o} newline start 0 1 • If state 1 is reached, then the word is passed to stage 2.
For example, “hash\n” is accepted: 1 0 0 0 0 0 s \n h a h