Formal Languages and Automata http mail im tku

  • Slides: 28
Download presentation
形式語言 Formal Languages and Automata 淡江大學 資訊管理系 侯 永 昌 http: //mail. im. tku.

形式語言 Formal Languages and Automata 淡江大學 資訊管理系 侯 永 昌 http: //mail. im. tku. edu. tw/~ychou ftp: //mail. im. tku. edu. tw/Prof_Hou

教科書:An Introduction to Formal Languages and Automata, 5 th Edition, by Peter Linz, Jones

教科書:An Introduction to Formal Languages and Automata, 5 th Edition, by Peter Linz, Jones & Bartlett Learning 出版,開發圖書總經銷 淡江大學資訊管理系侯永昌 1

第三章 規律語言與規律文法 (Regular Languages and Regular Grammars)

第三章 規律語言與規律文法 (Regular Languages and Regular Grammars)

Regular Expression § Let be a given alphabet. Then l l l , ,

Regular Expression § Let be a given alphabet. Then l l l , , and a are all regular expressions. These are called primitive regular expressions If r 1 and r 2 are regular expressions, so are r 1 + r 2, r 1*, and (r 1), where + denotes union, denotes concatenation, * denotes star-closure A string is a regular expression if and only if it can be derived from the primitive regular expression by a finite number of applications of the previous rule 淡江大學資訊管理系侯永昌 3

Regular Expression § 例: = {a, b, c},則 l l (a + b c)*

Regular Expression § 例: = {a, b, c},則 l l (a + b c)* (c + ) is a regular expression (a + b + ) is not a regular expression 淡江大學資訊管理系侯永昌 4

Regular Language § The language L(r) denoted by any regular expression r is defined

Regular Language § The language L(r) denoted by any regular expression r is defined by the following rules l l l is a regular expression denoting the empty set is a regular expression denoting { } For every a , a is a regular expression denoting {a} § If r 1 and r 2 are regular expressions, then l l L(r 1 + r 2) = L(r 1) L(r 2) L(r 1 r 2) = L(r 1) L(r 2) L((r 1)) = L(r 1) L(r 1*) = L(r 1)* 淡江大學資訊管理系侯永昌 5

Connection Between Regular Expressions and Regular Languages § 定理 3. 1:假設 r 是一個regular expression,

Connection Between Regular Expressions and Regular Languages § 定理 3. 1:假設 r 是一個regular expression, 則一定存在一個NFA可以接受這個 regular language L(r) (a) nfa accepts ∅ (b) nfa accepts {λ} (c) nfa accepts {a} 淡江大學資訊管理系侯永昌 8

Connection Between Regular Expressions and Regular Languages Schematic representation of an nfa accepting L(r).

Connection Between Regular Expressions and Regular Languages Schematic representation of an nfa accepting L(r). 淡江大學資訊管理系侯永昌 9

Connection Between Regular Expressions and Regular Languages Automaton for L(r 1 + r 2).

Connection Between Regular Expressions and Regular Languages Automaton for L(r 1 + r 2). 淡江大學資訊管理系侯永昌 10

Connection Between Regular Expressions and Regular Languages Automaton for L(r 1 r 2). 淡江大學資訊管理系侯永昌

Connection Between Regular Expressions and Regular Languages Automaton for L(r 1 r 2). 淡江大學資訊管理系侯永昌 11

Connection Between Regular Expressions and Regular Languages Automaton for L(r 1∗). 淡江大學資訊管理系侯永昌 12

Connection Between Regular Expressions and Regular Languages Automaton for L(r 1∗). 淡江大學資訊管理系侯永昌 12

Connection Between Regular Expressions and Regular Languages § 例:畫出能接受 r = (a + bb)*(ba*

Connection Between Regular Expressions and Regular Languages § 例:畫出能接受 r = (a + bb)*(ba* + ) 的 nfa 淡江大學資訊管理系侯永昌 13

Connection Between Regular Expressions and Regular Languages § 例:L(a* + (a + b)c*) §

Connection Between Regular Expressions and Regular Languages § 例:L(a* + (a + b)c*) § 例:L(r 1*r 2(r 4 + r 3 r 1*r 2)*) 淡江大學資訊管理系侯永昌 14

Connection Between Regular Expressions and Regular Languages § A complete generalized transition graph (GTG)

Connection Between Regular Expressions and Regular Languages § A complete generalized transition graph (GTG) is a graph in which all edges are present § A complete GTG with |V| vertices has |V|2 edges 淡江大學資訊管理系侯永昌 15

NFA to Regular Expression § 例:L = {w {a, b}* : na(w) is even

NFA to Regular Expression § 例:L = {w {a, b}* : na(w) is even and nb(w) is odd} 淡江大學資訊管理系侯永昌 18

NFA to Regular Expression § r 11 = + a *a = aa r

NFA to Regular Expression § r 11 = + a *a = aa r 33 = + b *b = bb r 13 = ab + * = ab r 31 = ba + * = ba r 12 = b + a * = b r 21 = b + *a = b r 32 = a + b * = a r 23 = a + *b = a r 22 = + * = 1 3 2 淡江大學資訊管理系侯永昌 19

NFA to Regular Expression 2 1 r = r 11*r 12(r 22 + r

NFA to Regular Expression 2 1 r = r 11*r 12(r 22 + r 21 r 11*r 12)* § 定理 3. 2:假設 L 是一個regular language,則 一定存在一個 regular expression 使得 L = L(r) 淡江大學資訊管理系侯永昌 20

Regular Grammars § A grammar G = (V, T, S, P) is said to

Regular Grammars § A grammar G = (V, T, S, P) is said to be rightlinear (left-linear) if all productions are of the form l l A x. B A x A Bx A x where A, B V and x T* § A regular grammar is one that is either rightlinear or left-linear § In a regular grammar, at most one variable appears on the right side of any production 淡江大學資訊管理系侯永昌 21

Regular Grammars § 例:G 1 = ({S}, {a, b}, S, P 1) where P

Regular Grammars § 例:G 1 = ({S}, {a, b}, S, P 1) where P 1 given as S ab. S | a is right-linear § 例:G 2 = ({S, S 1, S 2}, {a, b}, S, P 2) with productions S S 1 ab, S 1 ab | S 2, S 2 a is left-linear § Both G 1 and G 2 are regular grammars § In G 1, S ababa r = (ab)*a § In G 2, S S 1 ab … r = aab(ab)* 淡江大學資訊管理系侯永昌 22

Regular Grammars 例:G = ({S, A, B}, {a, b}, S, P) with productions S

Regular Grammars 例:G = ({S, A, B}, {a, b}, S, P) with productions S A, A a. B | , B Ab is not regular,因為他不滿足所有的 production rule 都要是 right-linear 或 left-linear 的要求,他被 稱為 linear grammar § 定理 3. 3:Let G = (V, T, S, P) be a right-linear grammar. Then L(G) is a regular language § 淡江大學資訊管理系侯永昌 23

Regular Grammars § 定理 3. 4:If L is a regular language on the alphabet

Regular Grammars § 定理 3. 4:If L is a regular language on the alphabet , then there exists a right-linear grammar G = (V, T, S, P) such that L = L(G) § 例:Construct right-linear grammar for L(aab*a) 解:q 0 aq 1 aaq 2 aabq 2 aabaqf aaba q 0 a b q 1 a qf 淡江大學資訊管理系侯永昌 q 2 a 25

Regular Grammars § 定理 3. 5:A language L is regular if and only if

Regular Grammars § 定理 3. 5:A language L is regular if and only if there exists a left-linear grammar G such that L = L(G) § 定理 3. 6:A language L is regular if and only if there exists a regular grammar G such that L = L(G) 淡江大學資訊管理系侯永昌 26