Finite automate
Patterns for token digit [0-9] digits digit+ Letter [A –Z a-z] If if then ten else else Relop <|<=|=|<>|>|>= Idletter(letter|digit)* Numdigits+ (.digits+)?(E(+|-)?digits+)? ws (blank|tab|newline)+
Transition Diagram A transition diagram is a stylized flowchart. Transition diagram is used to keep track of information about characters that are seen as the forward pointer scans the input. We do so by moving from position to position in the diagrams as characters are read.
Transition Diagram Start state Accept state Any state Have a collection of circles called states . Each state represents a condition that could occur during the process of scanning the input looking for a lexeme that matches one of several patterns . Start state Accept state Any state
Transition Diagram Edge : represent transitions from one state to another as caused by the input
Finite automata: why? Regular expressions = specification Finite automata = implementation
Definition : Finite Automaton
Finite Automata
State Diagram 1 q3 q1 q2 1 0, 1
Finite Automata Transition s1 a s2 Is read In state s1 on input a go to state s2 •If end of input and in accepting state => accept •Otherwise => reject
Data Representation
Exercise A finite automaton that accepts only “a” Language of FS is the set of accepted states stat input 1 a 2 Is accepted b ? a 1 2
Finite Automata Two types – both describe what are called regular languages Deterministic (DFA) – There is a fixed number of states and we can only be in one state at a time Nondeterministic (NFA) –There is a fixed number of states but we can be in multiple states at one time NFA and DFA stand for nondeterministic finite automaton and deterministic finite automaton, respectively.
NFA
Nondetermistic Finite Automata: NFA A nondeterministic finite automaton can be different from a deterministic one in that for any input symbol, nondeterministic one can transit to more than one states. epsilon transition
NFA 0,1 0,1 q3 1 q4 q1 q2 1 0,e
NFA
Test youself Construct an NFA to accept all strings terminating in 01
Answer
Test yourself 2 Construct an NFA to accept those strings containing three consecutive zeroes
Answer 2
DFA
DFA Is special case of NFA where: There are no moves on input Ԑ For each state s and input symbol a , there exactly one edge out of a labeled a.
DFA Deterministic Finite Automata (DFA) One transition per input per state No Ԑ-moves Nondeterministic Finite Automata (NFA) Can have multiple transitions for one input in a given state Can have -moves
Test yourself (DFA1) 1- Construct a DFA to accept a string containing a zero followed by a one
Answer 1-
Test yourself (DFA2) 2- Construct a DFA to accept a string containing two consecutive zeroes followed by two consecutive ones
answer 2-
Test yourself (DFA3) 3- Construct a DFA to accept a string containing even number of zeroes and any number of ones
Answer 3
Test yourself (DFA4) 4- Construct a DFA to accept all strings which do not contain three consecutive zeroes
Answer 4-
Construction of an NFA from regular Expression
Construction of an NFA from regular Expression Recursive construction Base cases follow base case definitions of regular expressions : -NFA that accepts the empty string – a single state that is the start and end state a: -NFA that accepts {a} – two-state machine (start and final state) with an a-transition Note: technically, we also need an -NFA for the empty language {} – easy
Construction of an NFA from regular Expression -NFA that accepts the empty string – a single state that is the start and end state a: -NFA that accepts {a} – two-state machine (start and final state) with an a-transition
Regular language Single final state Regular language NFA Single final state NFA
Example
Union NFA for Ԑ Ԑ Ԑ Ԑ
Example NFA for Ԑ Ԑ Ԑ Ԑ
Concatenation NFA for Ԑ Ԑ
Example NFA for Ԑ Ԑ
Star Operation Ԑ NFA for Ԑ Ԑ Ԑ Ԑ
Example NFA for Ԑ Ԑ Ԑ Ԑ
Test yourself Construct the NFA for r= (a|b)*abb
Steps 1,2
Step 3,4
Answer Construct the NFA for r= (a|b)*abb
3.4 3.6 3.7.4