csci-2200 foundations of computer sciencepattes3/csci2200/slides/april12.pdf8 languages accepted by...
TRANSCRIPT
-
CSCI-2200FOUNDATIONS OF COMPUTER SCIENCE
1
-
Announcements• Homework 7 is due April 12, 2019 at 11:59pm.
2
-
Warm UpConstruct a DFA that accepts all strings with an even number of 0s and an odd number of 1s. The alphabet is {0,1}.
3
-
Last Time - NFAs• NFA generalize FA by adding nondeterminism• Allows several alternative computations for the same input string.• Alternative computations execute in parallel on different copies of the
machine.• Three changes:• Allow δ(q, a) to specify more than one successor state
• Add λ-transitions: transitions made for “free” without consuming any input.
• Do not need to specify a transition for every symbol in the alphabet
4
a
a
qiqj
qk
λqi qj
-
Example NFA
5
-
6
Theorem:
Languages acceptedby NFAs
RegularLanguages=
NFAs and FAs have the same computational power
Languages acceptedby FAs
-
7
Languages acceptedby NFAs
RegularLanguages
Languages acceptedby NFAs
RegularLanguages
We can show:
⊆
⊇
-
8
Languages acceptedby NFAs
RegularLanguages
Proof-Step 1
Proof: Every DFA is trivially an NFA
Any language accepted by a DFAis also accepted by an NFA
L
⊇
-
9
Languages acceptedby NFAs
RegularLanguages
Proof-Step 2
Proof: Any NFA can be converted to anequivalent DFA
Any language accepted by an NFAis also accepted by a DFA
L
⊆
-
CONTEXT-FREE LANGUAGES
10
-
11
Regular Languages
}{ nnba }{ RwwContext-Free Languages
-
Context-Free Languages• A context free language is a language that can be
recognized by a new type of computational model –a pushdown automaton
12
stack
automaton
Inputstring Output
accept or reject
-
Pushdown Automata (PDA)• Similar to an FA, but with different transition specification
• If a is on input tape and b is on top of stack then:• 1. read a from tape• 2. pop b from stack• 3. push c on stack
13
!, # → %qi qj
-
Pushdown Automata• Similar to an NFA, a PDA does not need a transition for
every symbol in the tape alphabet.• If there is no transition, the machine hangs and the
string is rejected.
• A string is accepted by a PDA if the machine is an accept state and all input has been consumed.
14
-
15
-
Context Free Grammars
• A grammar gives rules for expressing a language.• A context-free language can be described by a context-free grammar• Theorem: A language is context-free if and only if some
(nondeterministic) pushdown automaton recognizes it.
• Context-free grammars can be used to describe:• Programming languages• Used by compilers
• The English language• Used in Natural Language Processing, Grammar checkers
16
-
Example of a Context-Free Grammar17
sentence → noun_ phrase predicate
noun_ phrase → article noun
predicate → verb
article → a
article → the
noun → cat
noun → dog
verb → runs
verb →walks
terminal
variable
start variable
production rule
-
18
sentence → noun_ phrase predicate
noun_ phrase → article noun
predicate → verb
article → a
article → the
noun → cat
noun → dog
verb → runs
verb →walks
-
Formal definition of a CFG
• A context-free grammar (CFG) is a 4-tuple G = (V,Σ,R,S) where• V is a finite set called the variables• Σ is a finite set, disjoint from V, called the terminals• R is a finite set of productions (rules)• S ∈ V is the start variable
19
l®®
SaSbS Variables are capital letters.
Terminals are lower-case letters
-
Language of a Grammar• The sequence of substitutions used to obtain a particular string
is called a derivation.• We write
• The language of a grammar, denoted L(G), is the set of all strings generated by derivations.
20
l®®
SaSbS
S ⇒ w*
-
Another Example Grammar21
l®®®
AaAbAAbS
-
Convenient Notation• We can combine rules that have the same variable on the
left-hand side.• The “|” symbol acts as an “or”
22
-
Designing a CFGDesign a context-free grammar G such that
23
=)(GL }*},{:{ bawwwR Î