CS 317 Automata - PowerPoint PPT Presentation

1 / 93
About This Presentation
Title:

CS 317 Automata

Description:

Useful for string pattern matching ... there exists strings u, v, w such that. y = uvw, v e, and. for all i 0, the string xuviwz A ... – PowerPoint PPT presentation

Number of Views:296
Avg rating:3.0/5.0
Slides: 94
Provided by: Sar62
Category:
Tags: automata | string

less

Transcript and Presenter's Notes

Title: CS 317 Automata


1
CS 317 - Automata Formal Languages Exam 2
Review
  • Fall 2009Sarah Mocas

2
  • Exam 2 Tuesday 11/3
  • I expect you to know most basic definitions
  • I will look back at the homework problems
  • Always answer partial credit will be given
  • If unsure, ask
  • If still unsure, write down your assumptions
  • I will try to discern what you know based on what
    you write

3
Class Info
Read these links if you need additional sources
  • Formal languages (sets of strings) and there
    relationship to our theoretical machines
  • Regular languages 
  • Can be recognized with finite automata
  • Can be described with regular expressions
  • Useful for string pattern matching
  • Useful for lexical analysis (initial phase of a
    compiler that scans the input source)
  • Finite Automata (state machines)
  • Context-free languages 
  • Can be recognized with nondeterministic pushdown
    automata
  • Can be described with context-free grammars
  • Useful for describing the syntax of programming
    languages
  • Pushdown Automata (state machines with a stack)

Dont forget everything from the first section
4
Overview
  • Old Material
  • Chapters 1, 2, 3, 4, 5, 6, 7, 8, 9
  • Notes 1, 2, 3, 4
  • This material is still important especially
  • As it relates to the pumping lemma for Regular
    languages
  • Closure properties for Regular languages
  • Properties about NFAs and DFAs
  • I will not test on the methods in these sections

5
  • Definition 1 (Strings) Basic notions
  • An alphabet is a finite set S of symbols.
  • A string x over S is a finite-length sequence of
    elements of S. Concatenation xy or simply xy.
  • The set of all strings over S is denoted S.
  • A language over S is a subset of S.
  • The length of a string x is denoted x.
  • The empty string is denoted e and e 0.
  • x is a prefix of y if xz y for some z.
  • Concatenation
  • (xy)z x(yz)
  • xe ex
  • aman amn

Basics
6
  • Definition (Sets) Basic notions
  • Set membership x ? A
  • Set union A U B x x ? A or x ? B
  • Set intersection A n B x x ? A and x ? B
  • String set concatenation AB xy x ?A and y
    ?B
  • AU(BnC) (AUB)n (AUC) etc.
  • (A U B) A n B De Morgan laws
  • Set difference (complement) A x ? S x ? A
  • Powers An (recursively defined) A0 e and
    An1 AAn
  • A stared A U An for n 0
  • A (no 0) and
  • ? e
  • An is different from an
  • ?, e and e are different. What is ?, ? and
    e?

Know the definitions
7
Chapter 3 Finite Automata Regular Sets
  • Definition 3 (DFA)
  • A deterministic finite automaton is a structure
  • M (Q, S, d, s, F) where
  • Q is a finite set of states
  • S is an input alphabet
  • d Q X S ? Q - transition function
  • (Induces d-hat Q X S ? Q. See next slides)
  • s ? Q - initial state or start state
  • F ? Q - final states
  • An FA is specified by a set of transitions
    (function) or table or graph

8
Chapter 3 Finite Automata Regular Sets
  • M accepts x if d-hat(s, x) ? F.
  • (processing of string ends in a final state)
  • M rejects x if d-hat(s, x) ? F.
  • The language accepted by M is
  • L(M) x ? S M accepts x .
  • A language is called regular if it is accepted by
    some DFA.
  • A set is called regular if it is accepted by some
    DFA.

To show that a set is regular either give an FA
or give a regular expression for it.
9
Chapter 3 Finite Automata Regular Sets
  • An FA is specified by a set of transitions or
    table or graph
  • What tuples (qi, a) ? qj give the transition
    function?
  • What is the table for the transition function?

a
F q4 (q1, a) ? q2 (q1, b) ? q3 (q2, a) ? q1

b
a
q2
q4
q1
a, b
b
q3
a, b
a
q3 can be viewed as a dead state and can be left
out of the diagram without changing the set of
strings that are good.
b
a
q2
q4
q1
3 ways to specify and DFA, NFA
10
Closure properties of regular sets
  • Set union A U B x x ? A or x ? B
  • Set intersection
  • A n B x x ? A and x ? B
  • Set difference (complement)
  • A x ? S x ? A
  • Set concatenation
  • AB xy x ? A and y ? B
  • A stared A U An for n 0

This means that if A and B are regular than so
are the sets formed by union, intersection,
difference and the star operator
11
Chapter 4 Regular Sets
  • Closure properties of regular languages.
  • Regular languages are closed under complement.
  • Regular languages are closed under intersection.
  • Regular languages are closed under union.
  • Regular languages are closed under set
    concatenation.
  • Regular languages are closed under A.

Know
12
Chapter 5 Nondeterministic FA
  • Nondeterminism is strange stuff.
  • NFA can have more than one start state
  • For each symbol in S a NFA can have zero, one ,
    or more than one transition out of any state
  • There is no mechanical way to determine the
    action of a NFA
  • A NFA, M accepts a string x if there exists at
    least one computation path that accepts.
  • A NFA, M rejects a string x if all computation
    paths do not accepts.
  • Model Guess and Verify

Know this definition
13
Chapter 6 Nondeterministic FA
  • Definition (NFA)
  • A nondeterministic finite automaton is a
    structure M (Q, S, ?, S, F) where
  • Q is a finite set of states.
  • S is an input alphabet.
  • ? Q X S ? 2Q - transition function.
  • (2Q A A ? Q it is the power set of Q this
    means it is the set of all subset of Q)
  • S ? Q - initial states (set of states)
  • F ? Q - final states.

14
Chapter 6 Nondeterministic FA
  • A NFA, N accepts x ? S if there exists an accept
    state q (so q ? F) such that q is reachable from
    a start state on input string x
  • so x ? S and ?-HAT(S, x) n F? ?
  • L(N) x ? S N accepts x
  • L(N) x ? S ? an accepting computation
    path for N on input x
  • Note a NFA for a language L must accept all
    strings in L and can not accept stings in L. But
    if x is in L there may also be some computation
    paths that do not end in a final state.

15
Chapter 5 Nondeterministic FA
  • Nondeterminism
  • Each symbol in S can have any number of
    transition out of a state
  • No mechanical way to determine the action of a
    NFA

b
a
q2
q1
a, e
a
q3
a
16
Chapter 5 Nondeterministic FA
  • Theorem The languages accepted by NFAs are
    exactly the regular languages.
  • Every DFA is a NFA and every NFA can be turned
    into a DFA (by subset construction).

Important Theorem - Know
17
Chapter 8 Pattern Matching Regular Expressions
  • Regular Expressions are all patterns made with
    just
  • Atoms a ? S, e, ?
  • operators , ,
  • precedence of operators highest to lowest
  • , (concatenation), Also use ( and )
  • Theorem Let A ? S. The following statements are
    equivalent
  • A is regular so A L(M) for some finite
    automata M
  • A L(a) for some regular expression a

18
Chapter 9 Regular Expressions and Finite
Automata
  • Laws to simplify regular expressions
  • a (ß ?) (a ß) ?
  • a(ß?) (aß)?
  • a ß ß a
  • a ? a
  • a? ?a ?
  • a a a
  • ea ae a
  • a(ß ?) aß a?
  • (a ß)? a? ß?
  • e aa a
  • e aa a

19
Chapter 9 Regular Expressions and Finite
Automata
L(ß)
  • Definition subset order
  • a ß ? L(a) ? L(ß)
  • ? L(a ß) L(ß)
  • ? a ß ß
  • Two rules for a, ß where is subset order
  • ß a? ? ? aß ?
  • ß ?a ? ? ßa ?
  • Useful equations
  • (aß)a a(ßa)
  • (aß)a (ß a)
  • a(ßa) (ß a)
  • (e a) a
  • aa aa

L(a)
or L(a) L(ß)
20
Overview
  • New Material
  • Chapters 11, 12, 13, 14, 19, 20, 21, 23, 24, 25,
    27 (pages 195 197)
  • Notes 5, 6, 7, 9

21
Chapter 11 Limitations of Finite Automata
  • Nonregular set
  • C ancbn n 0 e, acb, aacbb, aaacbbb,
  • B anbn n 0 e, ab, aabb, aaabbb,
  • D a2n n 0 a, a2, a4, a8, a16,
  • PAL w w x rev_x
  • Regular Set
  • A ambn m, n 0 e, a, b, aa, ab, bb,
    aaa, aab,
  • Think of other examples for the exam (S, empty
    set)

I will give some languages and ask you to say
which are regular and which are not.
22
Chapter 12 Using the Pumping Lemma
  • Which of these is regular?
  • A anb 2m m, n 0
  • B anbm n ? m
  • C xcx x ? a, b
  • D anb n 481 n 0
  • E anb m n m and m 481

I will give some languages and ask you to say
which are regular and which are not.
23
Chapter 11 Limitations of Finite Automata
  • Pumping Lemma Let A be a regular set. Then the
    following property holds of A
  • (P) There exists k 0 such that
  • for any x, y, z with xyz ? A and
  • y k,
  • there exists strings u, v, w such that
  • y uvw, v ? e, and
  • for all i 0, the string xuviwz ? A

Know the statement of the Pumping Lemma
24
Chapter 11 Limitations of Finite Automata
  • Pumping Lemma (contra positive) Let A be a set
    of strings. Suppose that the following property
    holds of A
  • (P) For all k 0
  • there exists strings x, y, z such that xyz ? A,
  • y k and
  • for all strings u, v, w with
  • y uvw and v ? e
  • There exists i 0, such that xuviwz not in A
  • Then A is not regular

Know how to use this to do proofs
25
Chapter 11 Limitations of Finite Automata
  • To show that a set is regular either give an FA
    or give a regular expression for it.
  • To show that a set is nonregular use Pumping
    Lemma (contrapositive).

Important Facts
26
  • Pumping Lemma (contrapositive) Let A be a set of
    strings. Suppose that the following property
    holds of A
  • For all k 0
  • there exists strings x, y, z such that xyz ? A,
  • y k and
  • for all strings u, v, w with
  • y uvw and v ? e
  • There exists i 0, such that xuviwz not in A
  • To use Pumping Lemma
  • k is pre picked
  • YOU pick x, y, z, such that xyz ? A and y k
  • This must divide up into any u, v, w with y uvw
    and v ? e
  • You pick i 0
  • You prove the language is not regular if xuviwz ?
    A

27
  • Theorem A anbn n 0 is not regular.
  • Proof Assume A is regular.
  • k is pre picked and YOU pick x, y, z, such that
    xyz ? A and y k
  • pick s akbk where x ak, y bk and z e
  • This must divide up into any u, v, w with y uvw
    and v ? e
  • Suppose that k jmn and so u bj, v bm, w
    bn, where m gt 0.
  • You pick i 0
  • Let i2.
  • You prove the language is not regular if xuviwz ?
    A
  • Now, the string is xuv2w ak bjbmbmbn ak bkm
  • this is not in A since there are more bs than
    as so A is not regular.

28
  • Theorem A anbn n 0 is not regular.
  • Proof Assume A is regular. So by the Pumping
    Lemma there exists k 0 such that
  • for any x, y, z with xyz ? A and y k,
  • there exists strings u, v, w such that
  • y uvw, v ? e, and
  • for all i 0, the string xuviwz ? A
  • So pick s akbk and divide s into xyz so
  • x ak, y bk and z e.
  • Now y k and y is picked to be all bs.
  • This y must divide up into any u, v, w with y
    uvw and v ? e. So, suppose that k jmn and
    that
  • u bj, v bm, w bn, where m gt 0.
  • Let i2.
  • Now, the string is xuv2w ak bjbmbmbn ak bkm
  • but this is not in A since there are more bs
    than as (because k lt km) so A is not regular.

Use as a model for your proofs
29
  • Theorem B anbm n m is not regular.
  • Proof Assume B is regular. So by the Pumping
    Lemma there exists k 0 such that
  • for any x, y, z with xyz ? A and y k,
  • there exists strings u, v, w such that
  • y uvw, v ? e, and
  • for all i 0, the string xuviwz ? A
  • So pick s akbkb and divide s into xyz so
  • x e, y ak and z bkb.
  • Now y k and y is picked to have all as.
  • This y must divide up into any u, v, w with y
    uvw and v ? e. So, suppose that k jmn and so
  • u aj, v am, w an, where m gt 0.
  • Let i3. Now, the string is
  • xuv3w ajamamaman bkb ak2m bkb
  • but this is not in B since there are fewer bs
    than as (because k2m lt k1) so B is not regular.

Use as a model for your proofs
30
Chapter 12 Using the Pumping Lemma
  • A anbn n 0
  • B anbm n gt m
  • C ancbn n 0
  • D a2n n 0 a, a2, a4, a8, a16,
  • E an! n 0
  • PAL w w x rev_x

I will give you variations on languages like
these and the ones that you had for homework
31
Chapter 12 Using the Pumping Lemma
  • Trick using properties about regular languages to
    prove a L is not regular.
  • Example
  • Prove that D x ? a,b a(x) b(x) is
    not regular
  • Proof Suppose that D is regular. Consider the
    set D n ab. This set will be regular too since
    regular languages are closed under intersection.
    But D n ab anbn which is not regular so D can
    not be regular.

Know how to do this type of proof
32
Chapter 12 Using the Pumping Lemma
  • Trick using properties about regular languages
  • Example
  • Prove that B anbm n m is not regular
  • Proof Suppose that B is regular.
  • Fact If B is regular than so are sets
  • revB bman n m and B ambn n m.
  • Consider B n B. This set will be regular too
    since regular languages are closed under
    intersection. But B n B anbn which is not
    regular so B can not be regular.

Know how to do this type of proof
33
Chapter 12
  • Ultimate Periodicity
  • Periodic behavior - In mathematics, a periodic
    function is a function that repeats its values
    after some definite period p has been added to
    its independent variable n
  • Definition Let U be a subset of the natural
    numbers N. The set U is Ultimately Periodic if
    there exists numbers n 0 and p gt 0 so that for
    all m n, m is in U if and only if mp is in U.
  • After m n then strings have period p

34
Chapter 12 Using the Pumping Lemma
  • Ultimate Periodicity
  • Example U 0, 1, 5, 8, 10, 13, 16, 19, 22
    here n 10, p 3
  • Theorem Let A ? a (any one symbol alphabet).
    Then A is regular if and only if the set m am
    ? A, the set of lengths of strings in A, is
    ultimately periodic. (Proof idea a loop is the
    period)
  • Corollary Let A be any regular set over any
    finite alphabet, Then the set length A x
    x ? A, of lengths of strings in A is ultimately
    periodic.

Know these results What regular sets are
periodic?
35
Chapter 13 DFA State Minimization
  • Simplifying a DFA
  • Collapse equivalent states to make the smallest
    DFA possible minimum DFA
  • Mechanical method to make a minimal DFA
  • Get rid of unreachable states
  • Collapse equivalent states

I main idea is there is a smallest DFA for a
regular language and we can always make
it. Contrast this with trying to guarantee that
you have the smallest C program for a problem
(not possible).
36
Chapter 13 DFA State Minimization
  • Mechanical method to make a minimal DFA
  • Get rid of unreachable states
  • Any q where there is no string x such that
  • d-HAT(s,x) q
  • Use depth first search on the transition graph
    of the DFA
  • Mark every state that can be reached from s
  • Delete all unmarked states

Know how to do - this is the easier part to do
37
Graph Depth-first Search
GO DEEP The first time a node is visited it is
processed.
8
6
3
1
2
7
5
16
8
4
9
0
9
11
14
  • Graph Depth-first Search
  • Visit node v then recursively visit each
    unvisited node connected to v
  • void traverse(int k)
  • link t
  • visit(k)
  • visitedk 1
  • for (t adjk t ! NULL t t-gtnext)
  • if (!visitedt-gtv) traverse(t-gtv)

38
Graph Depth-first Search
39
Chapter 14 DFA State Minimization
  • Mechanical method to make a minimal DFA
  • Collapse equivalent states
  • Use the Quotient Construction
  • Rule If there exists a string x so that
  • d-HAT(p,x) ? F and d-HAT(q,x) ? F
  • then p and q can not be collapsed, otherwise
    they can be collapsed

Know how to do - this is the harder part to do
40
Chapter 14 A Minimization Algorithm
  • Algorithm for computing minimum DFA
  • Write a table of all pairs p,q of states of M
  • Mark p,q if p ? F and q ? F or vise versa
  • Repeat until no more occur
  • If there exists an unmarked pair p,q such that
    d(p,a),d(q,a) is marked for some a ? S then
    mark p,q
  • q p iff p,q is not marked.

Know how to do - this is the harder part to do
41
Chapter 14 A Minimization Algorithm
  • Example
  • 0
  • U 1
  • U U 2
  • U U U 3
  • U U U U 4
  • U U U U U 5
  • After Step 2
  • 0
  • M 1
  • M U 2
  • U M M 3
  • U M M U 4
  • M U U M M 5

Unmarked pairs after Step 2 1,2 stays
unmarked 0,3 stays unmarked 0,4 stays
unmarked 3,4 stays unmarked 1,5 mark 2,5
mark After first pass Step 3 0 M 1 M U 2 U M M
3 U M M U 4 M M M M M 5
d(q1,a),d(q5,a) 3,5 marked d(q2,b),d(q5,b)
3,5 marked
If there exists an unmarked pair p,q such
that d(p,a),d(q,a) is marked for some a ? S
then mark p,q
Know how to do - this is the harder part to do
42
Chapter 14 A Minimization Algorithm
Unmarked pairs after Step 2 1,2 stays
unmarked 0,3 stays unmarked 0,4 stays
unmarked 3,4 stays unmarked 1,5 mark 2,5
mark After first pass Step 3 0 M 1 M U 2 U M M
3 U M M U 4 M M M M M 5
  • After second pass Step 3
  • 0
  • M 1
  • M U 2
  • M M M 3
  • M M M U 4
  • M M M M M 5
  • Unmarked pairs after
  • Step 2 Second pass
  • 1,2 stays unmarked
  • 0,3 mark
  • 0,4 mark
  • 3,4 stays unmarked so 1 2 and 3 4

43
Chapter 14 A Minimization Algorithm
Unmarked pairs after Step 2 1,2 stays
unmarked 0,3 stays unmarked 0,4 stays
unmarked 3,4 stays unmarked 1,5 mark 2,5
mark After first pass Step 3 0 M 1 M U 2 U M M
3 U M M U 4 M M M M M 5
  • After second pass Step 3
  • 0
  • M 1
  • M U 2
  • M M M 3
  • M M M U 4
  • M M M M M 5
  • Unmarked pairs after
  • Step 2 Second pass
  • 1,2 stays unmarked
  • 0,3 mark
  • 0,4 mark
  • 3,4 stays unmarked
  • so 1 2 and 3 4

q p iff p,q is not marked
44
(No Transcript)
45
Chapter 19 Context-Free Grammars Languages
  • A Context Free Grammar (CFG) is a set of
    recursive rewriting rules used to generate
    strings.
  • A CFG (N, S, P,S) where
  • N is a finite set of non-terminal symbols
  • S is a finite set of terminal symbols disjoint
    from N
  • P is a finite set of productions, N X (N ? S)
  • rules for replacing (or rewriting) non-terminal
    symbols (on the left side of the production) in a
    string with other non-terminal or terminal
    symbols (on the right side of the production).
  • S in N is a start symbol

Know the definition
46
  • Capital symbols for non-terminal A, B,
  • Non-Caps for terminal a, b, c,
  • Production rules are written A ? a and is
    shorthand for OR
  • A ? a1, A ? a2 can be written A ? a1 a2
  • If a, ß are in (N U S) we say a ? ß means ß is
    derivable from a (also derived in n steps from G)
  • A string in (N U S) derivable from the start
    symbol S is in sentential form. It is a sentence
    if there are no nonterminals in it.
  • A leftmost derivation is one where the
    productions are always applied to the left most
    nonterminal in the sentential form.

Know the definitions above
47
  • Example L anbn n 0
  • CFG (S, S a, b, P S ? aSb, S ? e, S)
  • non-terminal S
  • terminal a, b
  • Production rules S ? aSb e
  • If a, ß are in (N U S) we say a ? ß means ß is
    derivable from a
  • aaSbb ? aaabbb means aaabbb derivable from aaSbb
  • A string in (N U S) derivable from the start
    symbol S is in sentential form.

48
Chapter 19 Context-Free Grammars Languages
  • To generate a string of terminal symbols from a
    CFG
  • Begin with a start symbol
  • Apply one of the productions with the start
    symbol on the left hand side, replacing the start
    symbol with the right hand side of the
    production
  • Repeat the process of selecting non-terminal
    symbols in the string, and replacing them with
    the right hand side of some corresponding
    production, until all non-terminals have been
    replaced by terminal symbols.

Know how to do this
49
  • Example L anbn n 0
  • (S, S a, b, P S ? aSb, S ? e, S)
  • Production rules S ? aSb e
  • Derivation of aaaabbbb
  • S ? aSb
  • ? aaSbb
  • ? aaaSbbb
  • ? aaaabbbb

sentential form
50
  • Example
  • L x x ? a,b and the number of as is
    even
  • (S, S a, b, P listed below, S)
  • Production rules
  • S ? AB A B
  • B ? bB bA e
  • A ? aBa aaB aaA aAa e
  • Derivation of bbaabbbaba
  • S ? B
  • ? bB
  • ? bbA
  • ? bbaaB
  • ? bbaabB

? bbaabB ? bbaabbB ? bbaabbbA ? bbaabbbaBa ?
bbaabbbabBa ? bbaabbbaba
51
  • The language generated by a context-free grammar
    G, denoted L(G), is the set of all strings
    derived from G and containing only terminals.
  • A language is Context-Free if it is generated by
    a context-free grammar G.
  • All regular set are context-free.
  • L(G) x in S x is derivable from G
    starting from S
  • L is a CFL if it is L(G) for a context-free
    grammar G.

Know these - Important
52
To prove this use the pumping lemma
  • Some CFL are not regular.
  • L1 anbn n 0
  • L2 x ? a, b x revx palindromes
  • L3 balanced string of parentheses
  • Some languages are not CFL.
  • L1 anbncn n 0
  • Definition A grammar is unambiguous if there is
    only one derivation for each string in the
    language of the grammar.

Fact not yet proved
53
  • Some CFL are not regular.
  • L1 anbn n 0
  • S ? aSb e
  • L2 x in a, b x rev_x palindromes
  • S ? aSa bSb a b e
  • L3 balanced string of parentheses
  • S ? (S) SS e
  • To prove a language is regular give a regular
    expression or FA
  • To prove a language is not regular use the
    pumping lemma
  • To prove a language is CF give a CFG or PDA

54
Chapter 19 Context-Free Grammars Languages
  • The set of real numbers in Pascal
  • ltrealgt ? ltdigitgt ltdigitsgt ltdecimal partgt ltexpgt
  • ltdigitsgt ? ltdigitgt ltdigitsgt e
  • ltdecimal partgt ? '.' ltdigitgt ltdigitsgt e
  • ltexpgt ? 'E' ltsigngt ltdigitgt ltdigitsgt e
  • ltsigngt ? '' '-' e
  • ltdigitgt ? 0 1 2 3 4 5 6 7 8 9
  • What is the derivation of 435.39E3

the letter E or e represents times ten raised to
the power -7.443E3 means X103
55
Chapter 19 Context-Free Grammars Languages
  • L(G) x in S S ? x
  • G
  • This means the string x is derivable from G via a
    series of moves (application of the production
    rules to the sentential forms)
  • A subset B of S is a context-free language if B
    L(G) for some context-free grammar.

56
Chapter 21 Normal Forms
  • Know how to create CNF from a CFG
  • Know how to read/identify GNF
  • Special form easy for proofs
  • CNF Chomsky Normal Form
  • All productions are of the forms
  • A ? BC or A ? a
  • where A, B, C are in N and a is in S
  • GNF Greibach Normal Form
  • All productions are of the forms
  • A ? aB1B2 Bk if k0 then A ? a
  • where A, B1, B2 Bk are in N and a is in S

57
  • Examples for S ? S SS e
  • CNF S ? AB AC SS
  • C ? SB
  • A ?
  • B ?
  • GNF S ? B SB BS SBS
  • B ?
  • Theorem For any CFG G there is a CNF G and a
    GNF G so that
  • L(G) L(G) L(G) e

58
  • Theorem For any CFG G there is a CNF G and a
    GNF G so that L(G) L(G) L(G) e
  • Proof Get rid of A ? e and unit productions A ?
    B
  • Lemma For any CFG G there is a CNG G with no e
    or unit productions so that L(G) L(G) e
  • Method to remove
  • If A ? aBß and B ? e then replace with A ? aß
  • If A ? B and B ? ? then replace with A ? ?
  • Know how to remove
  • unit productions
  • e productions

59
Chapter 21 Normal Forms
Know how to do
  • CNF Chomsky Normal Form
  • After removing e and unit productions
  • For each terminal a in S make a production A ? a
    and replace every a on right hand side with A
  • Now all productions are A ? B1B2 Bk and A ?
    a
  • For A ? B1B2 Bk with k gt 2 replace this with A
    ? B1C and C ? B2 Bk. Repeat until all
    productions have length at most 2

60
  • A context-free grammar for the language of all
    strings over a,b containing an unequal number
    of a's and b's
  • Context-free grammar - Wikipedia, the free
    encyclopedia
  • S ? U V
  • U ? TaU TaT
  • V ? TbV TbT
  • T ? aTbT bTaT e
  • Eliminate e-productions to get
  • S ? U V
  • U ? TaU TaT aU aT Ta a
  • V ? TbV TbT bV bT Tb b
  • T ? aTbT bTaT abT aTb baT bTa ab ba

nonterminal T can generate all strings with the
same number of a's as b's, nonterminal U
generates all strings with more a's than b's
nonterminal V generates all strings with fewer
a's than b's.
61
  • S ? TaU TaT aU aT Ta a TbV TbT bV
    bT Tb b
  • U ? TaU TaT aU aT Ta a
  • V ? TbV TbT bV bT Tb b
  • T ? aTbT bTaT abT aTb baT bTa ab ba
  • CNF step 1, replace terminals
  • S ? TAU TAT AU AT TA a
  • TBV TBT BV BT TB b
  • U ? TAU TAT AU AT TA a
  • V ? TBV TBT BV BT TB b
  • T ? ATBT BTAT ABT ATB BAT BTA AB BA
  • A ? a
  • B ? b

62
  • S ? TAU TAT AU AT TA a TBV TBT BV
    BT TB b
  • U ? TAU TAT AU AT TA a
  • V ? TBV TBT BV BT TB b
  • T ? ATBT BTAT ABT ATB BAT BTA AB BA
  • A ? a
  • B ? b
  • CNF step 2, divide up long rules
  • S ? TC TD AU AT TA TE TF BV BT
    TB a b
  • U ? TC TD AU AT TA a
  • V ? TE TF BV BT TB b
  • T ? DF FD AF DB BD FA AB BA
  • C ? AU
  • D ? AT
  • E ? BV

F ? BT A ? a B ? b
63
Chapter 21 Normal Forms
  • CNF try these
  • L1 anbn n 0
  • S ? aSb e
  • L2 x in a, b x rev_x
  • S ? aSa bSb a b e
  • One more
  • S? aSa bSb aAb bAa
  • A ? aAa bAb a b e

64
Chapter 21 Normal Forms
  • Good example
  • CS-311 HANDOUT Greibach Normal Form (GNF)
  • GNF Greibach Normal Form
  • All productions are of the forms
  • A ? aB1B2 Bk if k0 then A ? a
  • where A, B1, B2 Bk are in N and a is in S
  • Every CFG can be rewritten in Greibach Normal Form

Know how to read/understand GNF but you will not
need to convert a CFG to GNF
65
  • Example 2 G (S a, b, NS, A, B, P, S)
  • S ? a A S a
  • A ? S b A S S ba
  • B ? SA a
  • Start S, Terminal a, b
  • Nonterminals S, A, B rename to A1, A2, A3
  • A1 ? a A2 A1 a
  • A2 ? A1 b A2 A1 A1 ba
  • A3 ? A1 A2 a

66
A1 ? a A2 A1 a A2 ? A1 b A2 A1 A1 ba A3 ?
A1 A2 a
  • Next
  • A2 ? A1 b A2 A1 A1 ba
  • A1 ? a A2 A1 a
  • A2 ? a A2 A1 b A2
  • a A2 A1 A1
  • a b A2
  • a A1 ba
  • A3 ? a A2 A1 A2
  • a A2 a
  • Step 1 loop starts
  • A3 ? A1 A2
  • G is now
  • A1 ? a A2 A1 a
  • A2 ? A1 b A2
  • A1 A1 ba
  • A3 ? a A2 A1 A2
  • a A2 a

67
  • Last
  • Replaced b a by B2
  • A1 ? a A2 A1 a
  • A2 ? a A2 A1 B1
  • a A2 A1 A1 a B1
  • a A1 b B2
  • A3 ? a A2 A1 A2
  • a A2 a
  • B1 ? b A2
  • B2 ? a
  • Next
  • Replaced b A2 by B1
  • A1 ? a A2 A1 a
  • A2 ? a A2 A1 B1
  • a A2 A1 A1 a B1
  • a A1 ba
  • A3 ? a A2 A1 A2
  • a A2 a
  • B1 ? b A2

A1 ? a A2 A1 a A2 ? A1 b A2 A1 A1 ba A3 ?
A1 A2 a
68
Chapter 23 Pushdown Automata
  • Pushdown Automata (PDAs)
  • Like an FA but has a pushdown stack
  • Stack is unbounded memory
  • Stack is LIFO
  • Stack operations are PUSH and POP
  • Input head still read only, and moves right

69
Chapter 23 Pushdown Automata
  • Definition A nondeterministic PDA (Pushdown
    Automata) is
  • M (Q, S, G, d, s, -, F) where
  • Q a finite set of states
  • S a finite set - input alphabet
  • G a finite set - stack alphabet
  • d ? (Q x (S U e) X G) X (Q X G) transitions
  • S start state in Q
  • - initial stack symbol to mark bottom
  • F ? Q the final states

Know the definition
70
Chapter 23 Pushdown Automata
  • NPDA M (Q, S, G, d, s, -, F)

Input Tape left to right, read only
Push/pop
Top of the Stack
Q Finite Control d
Stack
-
LIFO stack
71
  • Example L anbn n 0
  • (S, S a, b, P S ? aSb, S ? e, S)
  • Production rules S ? aSb e
  • Process aaaabbbb
  • Read a and Push A
  • Read a and Push A
  • Read a and Push A
  • Read a and Push A
  • Read b and Pop A
  • Read b and Pop A

Top of Stack
72
  • NPDA M (Q, S, G, d, s, -, F)
  • Two operations PUSH and POP the top symbol
  • ((p,a,A), (q, B1B2 Bk)) ? d
  • In state p, reading a on the tape, pop A from the
    top of the stack, push B1B2 Bk with Bk first
    and B1 last, go to state q, move the tapes head
    left 1 symbol
  • e-transitions allowed a PUSH or POP can happen
    but the read head does not move to the next input
    symbol
  • ((p,e,A), (q, B1B2 Bk)) ? d
  • In state p, ignore the tape, pop A from the top
    of the stack, push B1B2 Bk and go to state q

Know the definition
73
Chapter 23 Pushdown Automata
  • Configuration Gives the complete state of the
    PDA
  • (p, baaabba, ABAC -)
  • (current state, tape input string, stack)
  • Left of the tape head can be not included
  • Start config. (s,w,-)
  • Nondeterministic so more than one possible move
    from a given
  • (state, tape symbol, stack top)

Know the definition
74
Chapter 23 Pushdown Automata
  • Definition Since the PDAs are nondeterministic,
    the machine accepts its input string if there
    exists an accepting sequence of transitions.
  • Two Definitions of accept
  • Final State The PDA halts after having read the
    entire input string and is in a final state
  • Empty Stack The PDA halts after having read the
    entire input and after applying the last
    transition the stack is empty - only has -

Know the definition
75
Chapter 23 Pushdown Automata
  • Example NPDA for PARENS
  • M (Q q, S ,, G -, , d, s q, -,
    F)
  • d is
  • ((q, , - ), (q, - )) push
  • ((q, , ), (q, )) push
  • ((q, , ), (q, e)) pop
  • ((q, e , - ), (q, e)) stop with empty stack

Know how to make a PDA for a language or CFG
76
Chapter 23 Pushdown Automata
  • Examples L ww w ? a,b is not CF
  • The complement L a,b - ww w ? a,b
    is CF
  • Idea Guess a point in the first half that does
    not match the same point in the last half
  • suppose the input is aabbaaabaa
  • Then the 4th symbol in the 1st half does not
    match the 4th symbol in the second half.

77
Chapter 23 Pushdown Automata
  • Example L x x wcwr and w in 0,1
  • M (q1, q2, 0, 1, c, -, B, G, d, q1, -, Ø)
  • ((q1, 0, -), (q1, B-)) ((q1, 1, -), (q1, G-))
  • ((q1, 0, B), (q1, BB)) ((q1, 1, B), (q1, GB))
  • ((q1, 0, G), (q1, BG)) ((q1, 1, G), (q1, GG))
  • ((q1, c, G), (q2, G)) ((q1, c, B), (q2, B))
  • ((q1, c, -), (q2, -))
  • ((q2, 0, B), (q2, e))
  • ((q2, 1, G), (q2, e))
  • ((q2, e, -), (q2, e))

Know how to make a PDA for a language or CFG
78
Chapter 23 Pushdown Automata
  • Extra Rules
  • The symbol - can be pushed and popped
  • Transition ((p,a,A), (q, B1 Bk)) and ((p,e,A),
    (q, B1 Bk)) do not work if A is not on the top
    of the stack.
  • If the top of the stack is empty then the PDA
    breaks
  • Everything must be popped to accept with an empty
    stack.

79
  • L a,b - ww w ? a,b is CF
  • ((q1, a, - ), (q1, A- )) push a
  • ((q1, b, - ), (q1, B- )) push b
  • ((q1, a, ß), (q1, Aß)) push a ß is the top of
    the stack
  • ((q1, b, ß), (q1, Bß)) push b ß is the top of
    the stack
  • ((q1, a, ß), (q2, CACß)) push CAC go to q2 guess
    mismatch
  • ((q1, b, ß), (q2, CBCß)) push CBC go to q2 guess
    mismatch
  • ((q2, a, ß), (q2, Aß)) push a
  • ((q2, b, ß), (q2, Bß)) push b
  • ((q2, a, ß), (q3, e)) pop the top symbol ß go
    to q3 - guess middle
  • ((q2, b, ß), (q3, e)) pop the top symbol ß go
    to q3 - guess middle
  • ((q3, a, ß), (q3, e)) pop the top symbol ß
  • ((q3, b, ß), (q3, e)) pop the top symbol ß
  • ((q3, a, CAC), (q4, e)) pop CAC go to q4 reject
  • ((q3, b, CBC), (q4, e)) pop CBC go to q4 reject
  • ((q3, a, CBC), (q5, e)) pop CBC go to q5
    accept on empty stack
  • ((q3, b, CAC), (q5, e)) pop CAC go to q5
    accept on empty stack

80
Chapter 27 very end, pages 195 - 197
Important - know
  • Closure Properties of CFLs
  • The Context-free languages are closed under union
  • If L(G1) L1 and L(G2) L2 then L1U L2 L3
    will also be a CFL
  • Proof Idea S3 ? S1 S2
  • The Context-free languages are closed under
    concatenation
  • If L(G1) L1 and L(G2) L2 then L3 will also
    be CFL where L3 xy x ? L1 and y ? L2
  • Proof Idea S3 ? S1S2

81
Chapter 27 very end, pages 195 - 197
Important - know
  • Closure Properties of CFLs
  • The Context-free languages are closed under
    astrate
  • If L(G1) L1 then L1 will also be a CFL
  • Proof Idea S2 ? S1S2 e
  • The Context-free languages are closed under
    intersection with Regular sets
  • If L(G1) L1 is CFL and L(G2) L2 is a Regular
    language then L1 n L2 L3 will also be a CFL
  • Proof Idea (harder) Intersection of the PDA and
    DFA

82
Chapter 27 very end, pages 195 - 197
Important - know
  • Closure Properties of CFLs
  • The Context-free languages are NOT closed under
    intersection
  • If L(G1) L1 is CFL and L(G2) L2 is CFL then
    L1 n L2 L3 may not be CFL
  • Proof
  • anbncm ngt0 n ambncn ngt0 anbncn
    ngt0
  • is not a CFL

83
Chapter 27 very end, pages 195 - 197
Important - know
  • Closure Properties of CFLs
  • The Context-free languages are NOT closed under
    complementation
  • If L(G1) L1 is CFL then L1 may not be.
  • Proof
  • L ww w is in a, b is not a CFL but
    its complement is a CLF

84
Important - know
  • Closure Properties of CFLs
  • The Context-free languages are NOT closed under
    complementation
  • If L(G1) L1 is CFL then L1 may not be.
  • Second Proof (by contradiction)
  • Assume that the CLF are closed under
    complementation. Then if L1and L2 are CFL so are
    L1 and L2. Fact the Context-free languages are
    closed under union. Now, by properties of sets L1
    n L2 will be a CFL since
  • (L1 n L2) L1U L2 (De Morgans law)
  • and L1 n L2 ( L1U L2)
  • But the Context-free languages are NOT closed
    under intersection so the assumption is wrong.

85
Chapter 27 very end, pages 195 - 197
Important - know
  • Closure Properties of CFLs
  • The context-free language have a proper subset of
    languages accepted by deterministic PDAs.
  • These PDAs can not use nondeterministic moves.
  • L a, b - ww w is in a, b
  • is a CFL but is NOT deterministic CLF

86
The Language/Machine World so far

CFL NPDA
Deterministic CFL DPDA
REGULAR FA NFA
Have examples And know subset relationships
87
The Language/Machine World so far
  • If L1 and L2 are both regular (or CF) what about
    L3?

Important - know
88
Chapter 24 PDAs and CFG
  • The languages accepted by nondeterministic
    pushdown automata are the context-free languages

Important fact, dont worry about the proof.
89
Chapter 24 PDAs and CFG
  • Start with CFG (N, S, P,S) and construct NPDA M
    so that L(M) L(G)
  • Assume that all productions are in GNF so
  • A ? aB1B2 Bk if k0 then A ? a with a ? S U e
  • Construct M (Qq, S, N, d, q, S, ?)
  • q is the only state
  • S the set of terminals in G is the input
    alphabet to M
  • N the set of nonterminals in G is the stack
    alphabet
  • q is the start state
  • S the start symbol of G is the initial stack
    symbol
  • F ? because M accepts by empty stack
  • d for each production A ? cB1B2 Bk add to the
    transitions of M ((q,c,A),(q,B1B2 Bk ))

Know that a PDA can be made from a CFG in GNF but
I will give you the rules above if needed.
90
For each production A ? cB1B2 Bk add to the
transitions of the PDA ((q,c,A),(q,B1B2 Bk
))
  • Example Set PAREN is generated by CFG
  • S ? BS ((q, , S), (q, BS))
  • S ? B ((q, , S), (q, B))
  • S ? SB ((q, , S), (q, SB))
  • S ? SBS ((q, , S), (q, SBS))
  • B ? ((q, , B), (q, e))

Know that a PDA can be made from a CFG in GNF but
I will give you the rules above is needed.
91
  • Example
  • S ? BS ((q, , S), (q, BS))
  • ? S ((q, , B), (q, e))
  • ? SBS ((q, , S), (q, SBS))
  • ? BBS ((q, , S), (q, B))
  • ? BS ((q, , B), (q, e))
  • ? S ((q, , B), (q, e))
  • ? SB ((q, , S), (q, SB))
  • ? BSB
  • ? BSB
  • ? SB
  • ? BB
  • ? B
  • ?

S ? BS B SB SBS B ? ((q, , S),
(q, BS)) ((q, , S), (q, B)) ((q, , S), (q,
SB)) ((q, , S), (q, SBS)) ((q, , B), (q,
e)) List of sentential forms in a leftmost
derivation of w is the list of the PDAs
configurations for a computation on input
w. Very nondeterministic!
92
  • Very nondeterministic! Why?
  • A language L is Context-Free if it is generated
    (derivable) by a context-free grammar G.
  • This means that
  • Every string in the language L is derivable from
    the grammar G (there exists at least one
    derivation of the string)
  • No string in L is derivable from G

93
For the Test and the Future
  • Goals
  • For me to help you understand the material
  • For you to indicate to me that you understand
  • How does this work?
  • I give problems, you give solutions
  • I want to reward good skills, even if minor
    errors occur
  • Show your work, and your thought process!
Write a Comment
User Comments (0)
About PowerShow.com