490 likes | 515 Views
Finite automate. digit [0-9] digits digit+ Letter [A –Z a-z] If if t hen ten e lse else Relop <|<=|=|<>|>|>= Id letter ( letter|digit )* Num digits + (. digits + )?( E(+|-)? digits + )? ws ( blank|tab|newline )+. Patterns for token.
E N D
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)+ Patterns for token
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
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 . Transition Diagram • Start state • Accept state • Any state
Edge : represent transitions from one state to another as caused by the input Transition Diagram
Regular expressions = specification Finite automata = implementation Finite automata: why?
State Diagram 1 0 0 q3 q1 q2 1 0, 1
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 Finite Automata
A finite automaton that accepts only “a” • Language of FS is the set of accepted states Exercise
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. Finite Automata
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 Nondetermistic Finite Automata: NFA
NFA 0,1 0,1 q3 1 q4 q1 q2 1 0,e
Construct an NFA to accept all strings terminating in 01 Test youself
Construct an NFA to accept those strings containing three consecutive zeroes Test yourself 2
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 DFA
1- Construct a DFA to accept a string containing a zero followed by a one Test yourself (DFA1)
1- Answer
2- Construct a DFA to accept a string containing two consecutive zeroes followed by two consecutive ones Test yourself (DFA2)
2- answer
3- Construct a DFA to accept a string containing even number of zeroes and any number of ones Test yourself (DFA3)
3 Answer
4- Construct a DFA to accept all strings which do not contain three consecutive zeroes Test yourself (DFA4)
4- Answer
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
-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 Construction of an NFA from regular Expression
Regular language Regular language NFA NFA Single final state Single final state
Union • NFA for Ԑ Ԑ Ԑ Ԑ
Example NFA for Ԑ Ԑ Ԑ Ԑ
Concatenation • NFA for Ԑ Ԑ
Example • NFA for Ԑ Ԑ
Star Operation Ԑ • NFA for Ԑ Ԑ Ԑ Ԑ
Example • NFA for Ԑ Ԑ Ԑ Ԑ
Construct the NFA for r= (a|b)*abb Test yourself
3.4 3.6 3.7.4