Download presentation
Presentation is loading. Please wait.
1
CS314 – Section 5 Recitation 3
Long Zhao DFA, NFA Context Free Grammars Slides available at
2
Finite-State Automaton
A Finite-State Automaton is a quadruple: <S, s, F, T> S is a set of states s is the start state F is a set of final states T is a set of labeled transitions, of the form (state, input) => state
3
DFA & NFA DFA - Determistic Finite Automaton: At most one transition for a state and an input symbol. (Recognizers should be a DFA.) NFA - Nondeterministic Finite Automaton: More than one transition possible for a state and an input symbol.
4
Constructing a DFA from a RE
Regular Expression (RE) →NFA with ϵ moves NFA with ϵ moves to NFA NFA→DFA
5
NFA with ϵ moves (5 rules)
B AB
6
NFA with ϵ moves (5 rules)
A|B B ϵ ϵ ϵ ϵ A ϵ A* ϵ
7
NFA with ϵ moves (Examples)
(A|B)C A ϵ ϵ ϵ C B ϵ ϵ
8
NFA with ϵ moves (Examples)
A|BC A ϵ ϵ ϵ B C ϵ ϵ
9
NFA with ϵ moves (Examples)
((A|B)C)* ϵ A ϵ ϵ ϵ ϵ C ϵ ϵ B ϵ ϵ
10
NFA with ϵ moves (Examples)
((A|B)C)* ϵ A C B ϵ
11
NFA with ϵ moves to NFA ((A|B)C)* 1 2 3 A B C ϵ* (->)1 2 - 1,3 3
12
NFA with ϵ moves to NFA ((A|B)C)* 1 2 3 ϵ*Aϵ* ϵ*Bϵ* ϵ*Cϵ* (->|O)1 2
Since ϵ*(1) is {1, 3} which contains the final state 3, state 1 should be a final state in the new NFA as well. ((A|B)C)* ϵ*Aϵ* ϵ*Bϵ* ϵ*Cϵ* (->|O)1 2 - 1,3 (O)3 ϵ A,B C 1 2 3 ϵ
13
NFA with ϵ moves to NFA ((A|B)C)* 1 2 3 ϵ*Aϵ* ϵ*Bϵ* ϵ*Cϵ* (->|O)1 2
1,3 (O)3 C A,B A,B 1 2 3 C
14
NFA to DFA ϵ*Aϵ* ϵ*Bϵ* ϵ*Cϵ* 2 - 1,3 (O)3 A B C 2 - 1,3 (O)1,3
15
NFA to DFA ((A|B)C)* A B C (->|O)1 2 - 1,3 (O)1,3 A,B A,B 1 2 1,3 C
16
NFA to DFA All strings contain {a, b} and end with an ‘a’. RE: (a|b)*a
1 2 a,b
17
NFA to DFA (a|b)*a a b (->)1 1,2 1 (O)2 - a 1 2 a,b
18
NFA to DFA (a|b)*a a b (->)1 1,2 1 (O)2 - a b (->)1 1,2 1 (O)1,2
19
NFA to DFA (a|b)*a a b (->)1 1,2 1 (O)1,2 a 1 1,2 b b a
20
NFA to DFA (Example) 2 ϵ a a b NFA without ϵ move? DFA? 1 3 c c 4 ϵ
21
NFA to DFA (Example) 2 1 3 4 a b c ϵ* (->)1 2 - 4 1 3 1,2 (O)3 3,4
22
NFA to DFA (Example) 2 1 3 4 ϵ*aϵ* ϵ*bϵ* ϵ*cϵ* (->)1 1,2 - 3,4 2 3
23
NFA to DFA (Example) 2 1 3 4 a ϵ*aϵ* ϵ*bϵ* ϵ*cϵ* (->)1 1,2 - 3,4 2
NFA without ϵ move NFA to DFA (Example) a 2 ϵ*aϵ* ϵ*bϵ* ϵ*cϵ* (->)1 1,2 - 3,4 2 3 (O)3 (O)4 a a b,c c a a c 1 3 a a a c c 4
24
NFA to DFA (Example) a b c (->)1 1,2 - 3,4 2 3 (O)3 (O)4 a b c
25
NFA to DFA (Example) 1 3 a b c (->)1 1,2 - 3,4 3 (O)3,4 (O)3 a a a
26
Context-Free Grammars
A formalism for describing languages CFGs are a quadruple < T,N,P,S >: A set T of terminal symbols (tokens) A set N of nonterminal symbols A set P production rules A special start symbol S BNF is a notation for describing CFGs.
27
Simple BNF Grammar Terminals letters, digits, :=
Nonterminals <letter> <digit> <stmt> <identifier> Productions <letter> ::= A | B | C | ...| Z <digit> ::= 0 | 1 | 2 | ...| 9 <identifier> ::= <letter> |<identifier> <letter> |<identifier> <digit> <stmt> ::= <identifier> := 0 Start Symbol <stmt>
28
Regular Grammars CFGs with restrictions on the shapes of production rules. Left-linear: <N> ::= <X> a b <X> ::= a | <X> b Right-linear: N ::= b | b <Y> Y ::= a b | a b <Y>
29
Context-Free Grammars
Give the context-free grammar in BNF notation that generates the following language: 0 𝑛 1 𝑛 𝑛≥0}, with alphabet {0, 1} 𝑎 𝑚 𝑏 𝑐 𝑛 𝑚,𝑛≥0}, with alphabet {a, b, c} All strings of the form 0 𝑎 1 𝑏 0 𝑐 such that a + c = b.
30
Context-Free Grammars
0 𝑛 1 𝑛 𝑛≥0}, with alphabet {0, 1} Terminals 0, 1, ϵ Nonterminals <S> Productions <S> ::= 0<S>1 <S> ::= ϵ Start Symbol <S>
31
Context-Free Grammars
𝑎 𝑚 𝑏 𝑐 𝑛 𝑚,𝑛≥0}, with alphabet {a, b, c} The regular expression a*bc* Terminals a, b, c, ϵ Nonterminals <S> <A> <C> Productions <S> ::= <A> <A> ::= a<A>|b<C> <C> ::= c<C>|ϵ Start Symbol <S>
32
Context-Free Grammars
All strings of the form 0 𝑎 1 𝑏 0 𝑐 such that a + c = b. Terminals 0, 1, ϵ Nonterminals <S> <T> <U> Productions <S> ::= <T><U> <T> ::= 0<T>1|ϵ <U> ::= 1<U>0|ϵ Start Symbol <S>
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.