Automata Grammars and Languages Discourse 03 Finite Automata

  • Slides: 96
Download presentation
Automata, Grammars and Languages Discourse 03 Finite Automata C SC 473 Automata, Grammars &

Automata, Grammars and Languages Discourse 03 Finite Automata C SC 473 Automata, Grammars & Languages

Finite Automata / Switching Theory (CS) / (CE) • Boolean operators / Gates (Elem.

Finite Automata / Switching Theory (CS) / (CE) • Boolean operators / Gates (Elem. Switching Ops) x y x y 0 0 0 1 0 1 1 1 0 C SC 473 Automata, Grammars & Languages x 1 0 2

Boolean Functions / Combinatorial Circuits • Circuit H half adder H H F full

Boolean Functions / Combinatorial Circuits • Circuit H half adder H H F full adder C SC 473 Automata, Grammars & Languages 3

Boolean Functions / Comb. Circuits (cont’d) • Table representing F x 1 x 2

Boolean Functions / Comb. Circuits (cont’d) • Table representing F x 1 x 2 x 3 z 1 z 2 0 0 0 0 1 1 0 0 1 0 1 1 1 0 0 1 1 1 C SC 473 Automata, Grammars & Languages 4

Boolean Functions / Comb. Circuits (cont’d) • Equations representing F • General scheme (n

Boolean Functions / Comb. Circuits (cont’d) • Equations representing F • General scheme (n inputs, m outputs) C SC 473 Automata, Grammars & Languages 5

Finite Automata / Sequential Circuits • Add “memory” elements = delay elements f combinatorial

Finite Automata / Sequential Circuits • Add “memory” elements = delay elements f combinatorial circuit • Finite # of delay elements possible d C SC 473 Automata, Grammars & Languages 6

Finite Automata / Sequential Circuits • Ex: sequential adder: add 2 binary numbers; low

Finite Automata / Sequential Circuits • Ex: sequential adder: add 2 binary numbers; low order bits received first 1001 0101 1110 • (a) sequential net (circuit): full adder F carry C SC 473 Automata, Grammars & Languages 7

Finite Automata / Sequential Circuits • (b) Next-State & Output Equations: • (c) Transition

Finite Automata / Sequential Circuits • (b) Next-State & Output Equations: • (c) Transition Table: state space next state/output table C SC 473 Automata, Grammars & Languages 8

Finite Automata / Sequential Circuits • (d) State Diagram: C SC 473 Automata, Grammars

Finite Automata / Sequential Circuits • (d) State Diagram: C SC 473 Automata, Grammars & Languages 9

Finite Automata / Sequential Circuits • (e) Finite-State Transducer (Mealy Machine) n A 5

Finite Automata / Sequential Circuits • (e) Finite-State Transducer (Mealy Machine) n A 5 -tuple where finite set of states start state input alphabet output alphabet transition/output function C SC 473 Automata, Grammars & Languages 10

General Sequential Network is a boolean function C SC 473 Automata, Grammars & Languages

General Sequential Network is a boolean function C SC 473 Automata, Grammars & Languages 11

Three Types of Automata M time transducer M Yes(1) No(0) recognizer (acceptor) M Enumerator

Three Types of Automata M time transducer M Yes(1) No(0) recognizer (acceptor) M Enumerator (generator) C SC 473 Automata, Grammars & Languages 12

Machines that Recognize • • Detection of an “event”, i. e. , a pattern

Machines that Recognize • • Detection of an “event”, i. e. , a pattern in input Recognition of just those words in some language L Definition of a language Ex: detect abab –all non-overlapping occurrences a a b C SC 473 Automata, Grammars & Languages b b b a a 13

Ex: C Comments /* … */ • Filter in the lexical scanner (transducer) empty

Ex: C Comments /* … */ • Filter in the lexical scanner (transducer) empty notation in : out • Recognizer C SC 473 Automata, Grammars & Languages 14

Finite Automaton (Finite State Machine, FSA) • Defn 1. 5: A (deterministic) finite automaton

Finite Automaton (Finite State Machine, FSA) • Defn 1. 5: A (deterministic) finite automaton is a 5 -tuple • • • is a finite set, the states is a finite set, the alphabet is the transition function is the start state is the set of accepting (final) states Ex: C SC 473 Automata, Grammars & Languages 15

How FA Compute • FA is a finite structure—like a program—fixed and static •

How FA Compute • FA is a finite structure—like a program—fixed and static • Need to define the behavior of M on input w n n n Sequence of configurations Like trace of a program on given data Dynamic and input-dependent • Ex: start on input Look at sequence of “moves” determined by the transition function: Since in accepting state when input exhausted, w is recognized by C SC 473 Automata, Grammars & Languages 16

www. jflap. org JFLAP is a package of graphical tools which can be used

www. jflap. org JFLAP is a package of graphical tools which can be used as an aid in learning the basic concepts of Formal Languages and Automata Theory. C SC 473 Automata, Grammars & Languages

Info on JFLAP • Website http: //www. jflap. org/ n n Downloads Tutorial •

Info on JFLAP • Website http: //www. jflap. org/ n n Downloads Tutorial • Lectura (linux) install n n cd /usr/local/jflap java -jar JFLAP. jar • X 11 forwarding (graphics) n ssh -X lectura C SC 473 Automata, Grammars & Languages 18

How FA Compute (cont’d) • Given a FA • Defn: configuration of M is

How FA Compute (cont’d) • Given a FA • Defn: configuration of M is an element of • Defn: yields in one step (or moves) relation between configurations is defined by where n Notes: is a function, since is. Is undefined. • Defn: yields is the relation n Means “moves in zero or more steps to” n • Defn: A string w is recognized (accepted) by M C SC 473 Automata, Grammars & Languages 19

How FA Compute (cont’d) • Defn: The language recognized (accepted) by M is •

How FA Compute (cont’d) • Defn: The language recognized (accepted) by M is • Defn 1. 16: A language S is regular iff there is some FA that recognizes it, i. e. , • Ex: In FA C SC 473 Automata, Grammars & Languages 20

Example: i = make change for i – 30 ¢ & vend coffee •

Example: i = make change for i – 30 ¢ & vend coffee • Coin checker for 30¢ coffee. ={n, d, q} 0 d n 50 q q 10 q 45 d n 5 n q 40 15 q 35 C SC 473 Automata, Grammars & Languages n 25 d n 20 q 30 d d n d 21

Regular Operations & Regular Expressions • The regular operations on languages are: n union

Regular Operations & Regular Expressions • The regular operations on languages are: n union ( ), concatenation ( ) and Kleene star (*). • So called because the class of regular languages are closed under them—i. e. , applying these operators to regular languages results in a regular language. (We will prove these closure results later. ) • In fact, these three operations ( , , *) actually characterize what it means to be a regular language: any regular language can be built up from alphabet symbols and a finite number of these regular operations. • This motivates the notion of regular expression: a sequence of symbols, like an arithmetic expression, that defines a regular language using regular ops. C SC 473 Automata, Grammars & Languages 22

Regular Expressions • A syntax for describing sets of strings (languages) n n Terse

Regular Expressions • A syntax for describing sets of strings (languages) n n Terse Eliminates fussy “{ }” Reminiscent of arithmetic expressions Obeys some useful “algebra”, e. g. , (E*F*)* = (E+F)* • Syntax for regular expressions over , +, , *, (, ) n E (E+E) ( text uses not +; some authors use |) (EE) (usually suppress the in E E) n (E*) n (some authors use ) n n a for each a in • suppress (, ) where possible: (a+b)*a not ( ( (a + b)* ) a ) n C SC 473 Automata, Grammars & Languages 23

Regular Expressions (cont’d) • Meaning rules for the syntax • The meaning (denotation) of

Regular Expressions (cont’d) • Meaning rules for the syntax • The meaning (denotation) of an expression, L(E), is a set of strings (a language) • Rules expression E a (E+F) (E*) C SC 473 Automata, Grammars & Languages language L(E) {a} { } L(E) L(F) L(E)* 24

Reg. Expr. : Examples, Equivalence(=) • (a+b)*a L • (a*b*)* • =(a+b)* • (a+b)*a(a+b)*

Reg. Expr. : Examples, Equivalence(=) • (a+b)*a L • (a*b*)* • =(a+b)* • (a+b)*a(a+b)* • (b*ab*ab*ab*)* PASCAL unsigned numbers. d={0, 1, …, 9} • dd*( +. dd*)( +E(+ + – + )dd*) • a *a+b *b+a+b Defn: E=F L(E)= L(F) n n *= (E*F*)*=(E+F)* E = E= E(FG)=(EF)G E(F+G)=EF+EG E = E=E C SC 473 Automata, Grammars & Languages 25

Nondeterminism • Real computing devices are deterministic: the current configuration and instruction determines the

Nondeterminism • Real computing devices are deterministic: the current configuration and instruction determines the next configuration. The relation is a function. • Why the concept of nondeterminism? n n Provides powerful, economical descriptive ability Provides a way to specify languages without over-specifying and complex handling of cases Can be algorithmically converted to a deterministic description (at the sacrifice of some economy and with added complexity) Generalization of determinism • Ex: abab occurs somewhere in w: …abab… a b a, b C SC 473 Automata, Grammars & Languages a b a a, b 26

Nondeterminism (cont’d) • Ex: w * has penultimate symbol b: w = …b? b

Nondeterminism (cont’d) • Ex: w * has penultimate symbol b: w = …b? b b b a, b a a a, b • Ex: w * has 2 a’s: w = … a … a a, b C SC 473 Automata, Grammars & Languages a a, b 27

 -Moves Can Be Useful • SNOBOL arithmetic constants (no floating E) n Use

-Moves Can Be Useful • SNOBOL arithmetic constants (no floating E) n Use to specify “optional characters” like Unix command line [opt] d d=digit C SC 473 Automata, Grammars & Languages 28

Nondeterministic Finite Automaton • Defn 1. 5: A nondeterministic finite automaton is a 5

Nondeterministic Finite Automaton • Defn 1. 5: A nondeterministic finite automaton is a 5 -tuple n n is a finite set, the states is a finite set, the alphabet transition function n • is the start state is the set of accepting (final) states Ex: b a, b C SC 473 Automata, Grammars & Languages a, b 29

DFA vs NFA • DFA • a q NFA q a a b b

DFA vs NFA • DFA • a q NFA q a a b b b • • For each state q and input symbol a, there is exactly one choice of new state (or no transition is defined at all). Each transition “consumes” an input symbol Special case of NFA! C SC 473 Automata, Grammars & Languages • • There may be multiple choices for the same input symbol There may be -moves that do not “consume” an input character There can be “chains” of -moves can create even more choice for the next input character 30

How NFA Compute • Given a NFA • Defn: configuration – • Defn: yields

How NFA Compute • Given a NFA • Defn: configuration – • Defn: yields in one step (or moves) relation between configurations ( -move) • Defn: yields = n Means “COULD move in zero or more steps to” • Defn: w is recognized (accepted) by M n Same as before, but has the meaning “if there exists some sequence of moves from the start config to some accepting config” C SC 473 Automata, Grammars & Languages 31

How NFA Compute (cont’d) • Defn: The language recognized (accepted) by M is •

How NFA Compute (cont’d) • Defn: The language recognized (accepted) by M is • Ex: In NFA n This provides no “evidence” that aabbba is accepted (or not) However, also via a separate computation sequence: n And so aabbba is recognized! n C SC 473 Automata, Grammars & Languages 32

“Tree” of Computations • Ex: NFA M 1 = X null “evidence” C SC

“Tree” of Computations • Ex: NFA M 1 = X null “evidence” C SC 473 Automata, Grammars & Languages accepting Computation w L(M 1) 33

Computation Tree: Example a, b • Ex: L = {w: w begins & ends

Computation Tree: Example a, b • Ex: L = {w: w begins & ends same } a 2 b 4 a 3 1 b a, b X X 3 F accept ababa some path to F C SC 473 Automata, Grammars & Languages X reject abab path to F 34

Example with -Moves • String length a multiple of 2 or 3 -moves X

Example with -Moves • String length a multiple of 2 or 3 -moves X C SC 473 Automata, Grammars & Languages 2 0 a 4 a a 3 6 a a 5 4 F Accept aaa 35

Example with -Moves • a*b* 0 X a 1 b X 1 F Accept

Example with -Moves • a*b* 0 X a 1 b X 1 F Accept aab -moves “consume” no input symbols C SC 473 Automata, Grammars & Languages 36

Equivalence of NFA to DFA • There is an algorithm to convert any NFA

Equivalence of NFA to DFA • There is an algorithm to convert any NFA into a DFA n n We show basic idea assuming NFA has no -moves Then (later) modify the construction for NFAs with -moves • Ex: L = {x : last symbol of x appeared previously } ={a, b} N 0: a, b q a a, b a p s b r b a, b • Idea: given input string, keep track of all possible reached states after reading each letter. At end of input, see if a final state is among those reached C SC 473 Automata, Grammars & Languages 37

Equivalence of NFA and DFA (cont’d) • Computation paths through NFA N 0 on

Equivalence of NFA and DFA (cont’d) • Computation paths through NFA N 0 on w = abba a p b p a p a b b r b b a q b r r q a r s q a a C SC 473 Automata, Grammars & Languages q s 38

Equivalence of NFA and DFA (cont’d) • Idea: keep a list of all possible

Equivalence of NFA and DFA (cont’d) • Idea: keep a list of all possible states reachable by each prefix of w (“parallel worlds”). For NFA N 0: C SC 473 Automata, Grammars & Languages 39

Equivalence of NFA and DFA (cont’d) • Equivalent DFA M will have: n State

Equivalence of NFA and DFA (cont’d) • Equivalent DFA M will have: n State set P (Q) n Alphabet n Start state “set” {q 0} n Accepting states {X Q : X F } n Deterministic transition function : P (Q) • Ex: For NFA N 0: C SC 473 Automata, Grammars & Languages 40

Equivalence of NFA and DFA (cont’d) • Thm: [Rabin-Scott Construction]. Let L = L(N)

Equivalence of NFA and DFA (cont’d) • Thm: [Rabin-Scott Construction]. Let L = L(N) for some NFA N with no -moves. There is an algorithm to constuct a DFA M equivalent to N, i. e. with L(M) = L(N). Pf: Given N we construct a DFA M and then verify that it recognizes the same set as N. Construction: Given NFA construct where C SC 473 Automata, Grammars & Languages 41

Equivalence of NFA and DFA (cont’d) • Picture of a a a b b

Equivalence of NFA and DFA (cont’d) • Picture of a a a b b a C SC 473 Automata, Grammars & Languages 42

Equivalence of NFA and DFA (cont’d) • Verification: Show (1) M is a DFA

Equivalence of NFA and DFA (cont’d) • Verification: Show (1) M is a DFA and (2) L(M)=L(N) (1) is a function by the construction, and Q is finite: |Q | = 2|Q|. So M is a DFA. (2) To show equivalence we prove the • Lemma: Pf: By induction on the length of the input string w. Base |w|=0. Step Suppose (IH) the lemma is true Let C SC 473 Automata, Grammars & Languages To show: 43

Equivalence of NFA and DFA (cont’d) . Assume Then state r with and By

Equivalence of NFA and DFA (cont’d) . Assume Then state r with and By (IH) (*) Let By construction of M Then Using this with (*) results in: So C SC 473 Automata, Grammars & Languages 44

Equivalence of NFA and DFA (cont’d) . Assume Then state R with So (1)

Equivalence of NFA and DFA (cont’d) . Assume Then state R with So (1) By construction and (2) Since we have from (IH) (3) Combining (2) & (3): So C SC 473 Automata, Grammars & Languages 45

Equivalence of NFA and DFA (cont’d) We now finish the verification proof. Let From

Equivalence of NFA and DFA (cont’d) We now finish the verification proof. Let From the Lemma That is, C SC 473 Automata, Grammars & Languages for some 46

Example: -Free NFA DFA • Consider the previous NFA a {p, q, s} a

Example: -Free NFA DFA • Consider the previous NFA a {p, q, s} a a b b {p, q} {p} a, b b a {p, r} a {p, q, r} {p, q, r, s} b a b {p, r, s} b C SC 473 Automata, Grammars & Languages 47

NFA with -Moves � • -closure(R) = E(R) for a set of states R

NFA with -Moves � • -closure(R) = E(R) for a set of states R 9 a 2 1 4 3 5 6 7 10 c b d b 14 8 a a 11 12 13 15 For R Q the -closure, E(R) of R is: C SC 473 Automata, Grammars & Languages 48

 -closure of a set of states • Coalesce all nodes reachable from {4,

-closure of a set of states • Coalesce all nodes reachable from {4, 5} by -moves: a 9 d {1, 2, 3, 4, 5, 6, 7, 8} b a 15 10 c a 14 11 b 12 13 Note: still an NFA a E({4, 5}) a C SC 473 Automata, Grammars & Languages E({9}) a Etc. E({13}) 49

Conversion: NFA DFA • Thm: There is an algorithm to convert any NFA to

Conversion: NFA DFA • Thm: There is an algorithm to convert any NFA to an equivalent DFA�. • Pf: Construction: Given NFA construct new NFA where Verification. Pf: By induction on |w| C SC 473 Automata, Grammars & Languages 50

Conversion: NFA DFA • Thm 1. 39: [Rabin-Scott Theorem]: There is an algorithm to

Conversion: NFA DFA • Thm 1. 39: [Rabin-Scott Theorem]: There is an algorithm to convert any NFA into an equivalent DFA. • Corollary 1. 40: A language is regular some NFA recognizes it. • Ex: Start with an NFA N 1 as follows: b 1 b 2 b 3 d 4 C SC 473 Automata, Grammars & Languages 51

Conversion: NFA DFA b • Ex: 1 b 2 b 3 d 4 Useful

Conversion: NFA DFA b • Ex: 1 b 2 b 3 d 4 Useful summary C SC 473 Automata, Grammars & Languages 52

Conversion: NFA DFA b • Ex: 1 b 2 b 3 d 4 C

Conversion: NFA DFA b • Ex: 1 b 2 b 3 d 4 C SC 473 Automata, Grammars & Languages 53

Ex: NFA DFA (cont’d) b • Ex: 1 b 2 b 3 d 4

Ex: NFA DFA (cont’d) b • Ex: 1 b 2 b 3 d 4 C SC 473 Automata, Grammars & Languages 54

Conversion: NFA DFA (cont’d) b b 1 123 13 b b 1234 d C

Conversion: NFA DFA (cont’d) b b 1 123 13 b b 1234 d C SC 473 Automata, Grammars & Languages 55

Regular Expression NFA • Thm 1. 55: There is an algorithm that, given a

Regular Expression NFA • Thm 1. 55: There is an algorithm that, given a regular expression E, constructs a NFA N such that L(E) = L(M). Pf: Induction on the # of operator symbols in E. Base: E = a a Step: Assume (IH) the result is true of all expressions with operator symbols (+, , *). Let E have k+1 ops. Three cases: Case E = (E 1+E 2). By IH, FA M 1 , M 2 with L(E 1) = L(M 1) and L(E 2) = L(M 2). Construct the following NFA M. C SC 473 Automata, Grammars & Languages 56

Case + C SC 473 Automata, Grammars & Languages 57

Case + C SC 473 Automata, Grammars & Languages 57

Regular Expression NFA (cont”d) • Case E = (E 1 E 2). By IH,

Regular Expression NFA (cont”d) • Case E = (E 1 E 2). By IH, FA M 1 , M 2 with L(E 1) = L(M 1) and L(E 2) = L(M 2). Construct the following NFA M. C SC 473 Automata, Grammars & Languages 58

Case Unmark final states in M 1 C SC 473 Automata, Grammars & Languages

Case Unmark final states in M 1 C SC 473 Automata, Grammars & Languages 59

Regular Expression NFA (cont”d) • Case E = (E 1)*. By IH, FA M

Regular Expression NFA (cont”d) • Case E = (E 1)*. By IH, FA M 1 with L(E 1) = L(M 1). Construct the following NFA M. C SC 473 Automata, Grammars & Languages 60

Case * s QED C SC 473 Automata, Grammars & Languages 61

Case * s QED C SC 473 Automata, Grammars & Languages 61

Example: Reg. Exp. NFA • (b+aa)* b a b a Not very economical a

Example: Reg. Exp. NFA • (b+aa)* b a b a Not very economical a a C SC 473 Automata, Grammars & Languages 62

Regular Expressions—Applications • Regexp used in various development tools n qed – interactive text

Regular Expressions—Applications • Regexp used in various development tools n qed – interactive text editor. 1 st version Lampson & Deutsch 1967 u Regexp added by Ken Thompson, Bell Labs, ca. 1968 s s s u u n n n Regexp compiled into NFA in machine code Rabin-Scott idea used to scan “on the fly” One of the first software patents Offspring ed by Ken for Unix Many others followed: em, vi / ex, sam, qedx, … grep, egrep - pattern search in a file shell – command line interpreter lex – lexical analyzer generator sed – non-interactive stream editor awk – pattern scanning and processing language perl – pattern-driven programming language C SC 473 Automata, Grammars & Languages 63

Applications (cont’d) • Regular expressions = “patterns” meaning matches >=1 r matches >=0 r

Applications (cont’d) • Regular expressions = “patterns” meaning matches >=1 r matches >=0 r matches 0 or 1 r matches r then s matches r or s match literal “c” match begin/end line match any char group exprs character list negated char list C SC 473 Automata, Grammars & Languages awk regexp r+ r* r? rs r|s c ^ $ . (s) [abc…] [^abc…] 64

Applications--Examples -? [0 -9]+ [^0 -9] [. *] g/^[ ]*$/d g/[ ]+/d nonempty digit

Applications--Examples -? [0 -9]+ [^0 -9] [. *] g/^[ ]*$/d g/[ ]+/d nonempty digit strings, optional sign any char except digit reference citations in a paper delete blank lines delete lines with a blank Ex: match is always (1) leftmost and (2) longest file: abcddddef vi: s/d*/x/ xabcddddef s/d+/x/ abcxef Ex: csh: sort roll[1 -5] | egrep “C SC|MATH” | pr C SC 473 Automata, Grammars & Languages 65

Applications--Examples Ex: traditional spelling mnemonic • “i before e, except after c, or when

Applications--Examples Ex: traditional spelling mnemonic • “i before e, except after c, or when pronounced ‘a’, as in ‘neighbor’ and ‘weigh’ --except for ‘weird’ examples. ” • grep “[^c]ei” /usr/share/dict/words > foo cat foo abseil Aeneid ageing Alamein albeit atheist Boeing Budweiser caffein canoeist deice deictic dilettanteism dreidl. . . • if you think this spelling rule is sufficient, you will be deficient, inefficient, unscientific and far from omniscient C SC 473 Automata, Grammars & Languages 66

Applications--Examples Ex: lex – generates a lexical analyzer yylex(). Example: wordcount (wc) %{ int

Applications--Examples Ex: lex – generates a lexical analyzer yylex(). Example: wordcount (wc) %{ int nchar, nword, nline; %} %% n { nline++; nchar++; } [^ tn]+ { nword++, nchar += yyleng; } // yyleng = length of matched string. { nchar++; } %% int main(void) { yylex(); // invoke generated lexer printf("%dt%dn", nchar, nword, nline); return 0; } C SC 473 Automata, Grammars & Languages 67

L Regular L Denoted by a Reg. Expr. • We’ve defined “regular” as meaning:

L Regular L Denoted by a Reg. Expr. • We’ve defined “regular” as meaning: recognized by a DFA (equiv. to rec. by an NFA) • This equivalence result is known as “Kleene’s Theorem” • We’ve already shown the direction—we constructed an NFA from a regular expression (Using Rabin-Scott we could convert this NFA to a DFA. ) • Now we show the direction: given a DFA M construct a regular expr. E with L(M) = L(E). • Thm (Kleene): There is an algorithm that, given a DFA M , computes a regular expression E such that L(M) = L(E). • Pf: Given the graph of the DFA, use the “node elimination algorithm” to gradually eliminate all nodes in favor of expressions on the edges of the graph. C SC 473 Automata, Grammars & Languages 68

Kleene’s Thm: use “Generalized NFA” 0 B 1 A 0 0 1 0 0

Kleene’s Thm: use “Generalized NFA” 0 B 1 A 0 0 1 0 0 A a 01 00 1 S 1 B 1 S 1 Add init. S, & 2. final a 0 1. C 1(1+01)*00+0 4. Elim. A 3. Elim. B a B A C 2. Elim. C S 1 a A (1(1+01)*00+0)* a S Order: CBA E =(1(1+01)*00+0)* C SC 473 Automata, Grammars & Languages 69

Ex: Node Elimination Algorithm b B C b b a Add -moves: S b

Ex: Node Elimination Algorithm b B C b b a Add -moves: S b C B a b b a A C SC 473 Automata, Grammars & Languages a 70

Ex: Node Elimination Algorithm Elim. A: S b C B (ACB) a A b

Ex: Node Elimination Algorithm Elim. A: S b C B (ACB) a A b ba*a Elim. C: bb S b B a AC bba*a Elim. B: S (bb+bba*a)*b C SC 473 Automata, Grammars & Languages a ACB 71

Ex: Node Elimination: other orders S b C B a b b a A

Ex: Node Elimination: other orders S b C B a b b a A C SC 473 Automata, Grammars & Languages a 72

Ex: Other elimination orders (CAB) Elim. C: bb S b B a C a

Ex: Other elimination orders (CAB) Elim. C: bb S b B a C a bb A a Elim. A: bb S b B CA= AC (above) a bba*a Elim. B: S (bb+bba*a)*b C SC 473 Automata, Grammars & Languages a CAB=ACB 73

Ex: Other elimination orders (CBA) Elim. C: bb S b B a C a

Ex: Other elimination orders (CBA) Elim. C: bb S b B a C a bb A a Elim. B: (bb)*b S CB a (bb)*bb Elim. A: A a(bb)*b a S (bb)*b+(bb)*bba*a(bb)*b C SC 473 Automata, Grammars & Languages a CBA 74

Ex: Other elimination orders (BAC) bb Elim. B: b C B S ab a

Ex: Other elimination orders (BAC) bb Elim. B: b C B S ab a b A a Elim. A: b S bb C BA =AB a ba*ab Elim. C: S b(bb+ba*ab)* C SC 473 Automata, Grammars & Languages a BAC=ABC 75

Ex: All elimination orders equiv (BAC = ACB) BAC b(bb+ba*ab)* ACB (bb+bba*a)*b Easy to

Ex: All elimination orders equiv (BAC = ACB) BAC b(bb+ba*ab)* ACB (bb+bba*a)*b Easy to prove by induction: for any expression E, b(Eb)*=(b. E)*b. Using this identity: b(bb+ba*ab)* = b[ (b+ba*a) b]* = [b (b+ba*a) ]*b = (bb+bba*a)*b Further regular expression simplication is possible: b(bb+ba*ab)*=b[b(b+a*ab)]*=b[b( +a*a)b]* =b[ba*b]* Good exercise: show results of all other elimination orders are equivalent to these, using regular expression algebra C SC 473 Automata, Grammars & Languages 76

Algebra of Regular Expressions • an algebra for symplifying regular expressions • Can use

Algebra of Regular Expressions • an algebra for symplifying regular expressions • Can use this algebra to construct Reg. Exps from FSA r+s = s+r (r+s)+t=r+(s+t) r+r=r r+ =r (rs)t = r(st) r = r r = r(s+t) = rs + rt (r+s)t = rt + st * = r* = r + r* (r*)* = r* r* = + rr* C SC 473 Automata, Grammars & Languages (r*s*)* = (r+s)* 77

Solving Regular Expr Equations • Can solve “linear equations” with regexp variables X =

Solving Regular Expr Equations • Can solve “linear equations” with regexp variables X = a. X + b =a(a. X + b) + b = a 2 X + ab + b = a 2 (a. X + b) + ab + b = a 3 X + a 2 b + ab + b X = a*b Check: a[a*b] + b = aa*b + b = (aa*+ )b = a*b Ex: X = a. X + b. Y Y = X = a*b C SC 473 Automata, Grammars & Languages a X b Y 78

Solving Reg. Exp Equations (cont’d) • Ex: NFA Reg. Exp 0 B 1 A

Solving Reg. Exp Equations (cont’d) • Ex: NFA Reg. Exp 0 B 1 A 0 1 C Gauss-Jordan elimination & back-substitution C SC 473 Automata, Grammars & Languages 79

Ex: Node Elimination Example via Algebra b B C b b a A a

Ex: Node Elimination Example via Algebra b B C b b a A a • Want B. C is accept state. • Elim. A: • Elim B: • Elim C: • C SC 473 Automata, Grammars & Languages 80

Closure Properties • A class of languages is said to be closed under an

Closure Properties • A class of languages is said to be closed under an operation if applying that operation to members of the class results in a language that is again a member of the class. Example: the regular languages are closed under the operations of union, concatenation and Kleene star. • Thm: The regular languages are closed under intersection and complementation. Pf: Complementation. Let L = L(M) where is a DFA. Then the FA is also deterministic, and So w leads to a nonaccepting state in w leads to an accepting state in So C SC 473 Automata, Grammars & Languages 81

Closure Properties (cont’d) Intersection. Let be regular. By De. Morgan’s law Since the regular

Closure Properties (cont’d) Intersection. Let be regular. By De. Morgan’s law Since the regular languages are closed under complementation and union, the result follows. C SC 473 Automata, Grammars & Languages 82

Closure Properties (cont’d) Another proof of closure under illustrates the technique called “cross-product construction”.

Closure Properties (cont’d) Another proof of closure under illustrates the technique called “cross-product construction”. See Sipser text, Theorem 1. 25. • Thm: The class of regular languages is closed under the intersection operation. Pf: Assume and , where the automata are deterministic. Construction. Construct a “cross-product machine” M as follows: where the transition function is defined by: • Machine M simulates the two given machines “in parallel”, keeping each machine state in one component of 83 C SC 473 Automata, Grammars & Languages

Closure Properties (cont’d) Verification By an easy induction on |x|, can show that Therefore,

Closure Properties (cont’d) Verification By an easy induction on |x|, can show that Therefore, for a pair of final states This says that i. e. , that C SC 473 Automata, Grammars & Languages 84

Closure Properties (cont’d) • Defn: A homomorphism h is a function that maps each

Closure Properties (cont’d) • Defn: A homomorphism h is a function that maps each symbol of to a string over some alphabet , i. e. , • The homomorphism is extended to operate on strings character-by-character, i. e. , • It is further extended to languages element-wise, i. e. , • Thm: If L is regular and h is a homomorphism, then h(L) is regular. Pf: Assume L is recognized by a DFA C SC 473 Automata, Grammars & Languages 85

Closure Properties (cont’d) • Construction: Construct the machine where for each transition in M:

Closure Properties (cont’d) • Construction: Construct the machine where for each transition in M: Note: GNFA a p q h(a) p q put into Mh the transition An easy induction establishes that from which it follows that C SC 473 Automata, Grammars & Languages 86

What is Not Regular? • FA have a very limited computing ability. They cannot,

What is Not Regular? • FA have a very limited computing ability. They cannot, for example, recognized strings of well-nested parentheses, or well-formed arithmetic expressions, or even the language of strings of the form w#w, having two copies of the same substring. • How can we show some languages are not regular? We will give a property that all regular languages must have (called the pumping property). Then, to show that a language L is not regular, we argue that it lacks this pumping property. C SC 473 Automata, Grammars & Languages 87

Pumping Lemma • Thm [Pumping Lemma for Regular Languages]. Suppose that L is an

Pumping Lemma • Thm [Pumping Lemma for Regular Languages]. Suppose that L is an infinite¹ regular language. Then ¹All finite languages are regular, so only infinite languages are of interest. C SC 473 Automata, Grammars & Languages 88

Pumping Lemma (English) • Thm [Pumping Lemma for Regular Languages]. Suppose that L is

Pumping Lemma (English) • Thm [Pumping Lemma for Regular Languages]. Suppose that L is an infinite¹ regular language. Then there is some number p (called the pumping length) such that: if w is any string in L with |w| p, then w can be factored into 3 substrings, w = xyz, that satisfy the following 3 conditions: (i) y [y is not empty] (ii) |xy| p [the prefix and pumped part are short] (iii) for every i 0, xyiz L [“pumped up” and “pumped down” (i = 0) versions of the string must also be in L] ¹All finite languages are regular, so only infinite languages are of interest. C SC 473 Automata, Grammars & Languages 89

Pumping Lemma (cont’d) • Pf: Let be a DFA recognizing L and let p

Pumping Lemma (cont’d) • Pf: Let be a DFA recognizing L and let p be the number of its states. Let be an input string of length n where n p. Let be the sequence of states that M enters while processing w so that for This state sequence has length n+1 p+1. Among the first p+1 states of this sequence, at least 2 must be the same state [“pigeonhole principle”]. Call the first of these 2 and the second. Because occurs among the first p+1 places in the sequence we have that k p+1. Define the following substrings of w: C SC 473 Automata, Grammars & Languages 90

Pumping Lemma (cont’d) • Picture: rk =rj r 1 a rn+1 • From the

Pumping Lemma (cont’d) • Picture: rk =rj r 1 a rn+1 • From the picture, we see that there is an accepting path from to a final state for all the strings of the form. Also, since it must be that Furthermore, so. C SC 473 Automata, Grammars & Languages 91

Non-regular Examples • Ex: is not regular. Pf: By contradiction. Suppose L is regular.

Non-regular Examples • Ex: is not regular. Pf: By contradiction. Suppose L is regular. Then by the Pumping Lemma, Then it follows that is a perfect square. This is impossible. For suppose for a so large that Then Hence perfect squares—a contradiction. C SC 473 Automata, Grammars & Languages falls “in between” 92

Non-regular Examples (cont’d) • Ex: is not regular. Pf: By contradiction. Suppose L is

Non-regular Examples (cont’d) • Ex: is not regular. Pf: By contradiction. Suppose L is regular. Then by closure properties of the regular languages is regular. Now We show cannot be regular, which provides a contradiction. If is regular, then there are substrings with such that Case 1. y is entirely in the a’s. Assume it is in the a’s before the 2 b’s (The other subcase is symmetric). Then and where C SC 473 Automata, Grammars & Languages But then This is a contradiction. 93

Non-regular Examples (cont’d) • Case 2. y contains a b. Then has more than

Non-regular Examples (cont’d) • Case 2. y contains a b. Then has more than 2 b’s, and so This is a contradiction. Contradictions in all cases contradiction to the assumption that is regular. So is not regular. • Ex: is not regular. See Text, Example 1. 73. • Ex: is not regular. Pf: Suppose B is regular. Then so is as is its homomorphic image Contradiction. C SC 473 Automata, Grammars & Languages 94

Decision Problems • For a property/predicate P the decision problem for P is: n

Decision Problems • For a property/predicate P the decision problem for P is: n n • • Given: x Question: Is P(x) true? Ex: Given DFA M, is it true that L(M) = ? Thm: For DFA M all the following decision problems are solvable, i. e. there exists an algorithm to decide the question for any input: Given Question M, w w L(M) ? M L(M) = * ? M, M L(M) L(M ) ? M, M L(M) = L(M ) ? C SC 473 Automata, Grammars & Languages 95

Decision Problems (cont’d) Pf: Assume given DFAs for inputs. (1) Trace w through M.

Decision Problems (cont’d) Pf: Assume given DFAs for inputs. (1) Trace w through M. “Yes” if leads from s to some q F (2) “Yes” if there is some q F reachable from s (3) Convert (4) (5) Use (4) twice C SC 473 Automata, Grammars & Languages 96