Download presentation

Presentation is loading. Please wait.

Published byAbbie Thewes Modified over 2 years ago

1
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 4 Mälardalen University 2010

2
2 Regular Expressions and Regular Languages NFA DFA Subset Construction (Delmängdskonstruktion) FA RE State Elimination (Tillståndseliminering) Minimizing DFA by Set Partitioning (Särskiljandealgoritmen) Grammar: Linear grammar. Regular grammar. Regular Lng.. Content

3
3 Two Basic Theorems

4
4 1: KleeneTheorem NFA DFA Let M be NFA accepting L. Then there exists DFA M´ that accepts L as well.

5
5 II: Finite Language Theorem Any finite language is FSA-acceptable. Example L = {abba, abb, abab} FSA = Finite State Automaton = DFA/NFA

6
6 Regular Expressions and Regular Languages

7
7 Theorem - Part 1 Languages Generated by Regular Expressions Regular Languages 1. For any regular expression the language is regular

8
8 Theorem - Part 2 Languages Generated by Regular Expressions Regular Languages 2. For any regular language there is a regular expression with

9
9 Proof - Part 1 For any regular expression the language is regular Proof by induction on the size of

10
10 Induction Basis Primitive Regular Expressions: NFAs Regular Languages (where )

11
11 Inductive Hypothesis Assume for regular expressions and that and are regular languages

12
12 Inductive Step We will prove: regular languages

13
13 By definition of regular expressions:

14
14 By inductive hypothesis we know: and are regular languages Regular languages are closed under union concatenation star It is known, and we will prove in this lecture:

15
15 Therefore regular languages And trivially is a regular language

16
16 Proof – Part 2 For any regular language there is a regular expression with Proof by construction of regular expression

17
17 Single final state Since is regular take the NFA that accepts it

18
18 From construct the equivalent Generalized Transition Graph transition labels are regular expressions Example

19
19 Reverse of a Regular Language

20
20 Theorem The reverse of a regular language is a regular language Proof idea Construct NFA that accepts : invert the transitions of the NFA that accepts

21
21 Proof Since is regular, there is NFA that accepts Example:

22
22 Invert Transitions

23
23 Make old initial state a final state and vice versa

24
24 Add a new initial state

25
25 Resulting machine accepts is regular

26
26 Some Properties of Regular Languages, Summary

27
27 Properties Concatenation: Star: Union: Are Regular Languages For regular languages and we will prove that:

28
28 Regular languages are closed under Concatenation:Star: Union:

29
29 Regular language Single final state NFA Regular language Single final state NFA

30
30 Example

31
31 Union (Thompson’s construction) NFA for

32
32 NFA for Example

33
33 Concatenation (Thompson’s construction) NFA for

34
34 Example NFA for

35
35 Star Operation (Thompson’s construction) NFA for

36
36 Example NFA for

37
37 Summary: Operations on Regular Expressions RE Regular language description a+b {a,b} (a+b)(a+b) {aa, ab, ba, bb} a* {, a, aa, aaa, …} a*b {b,ab, aab, aaab, …} (a+b)* {, a, b, aa, ab, ba, aaa, bbb…}

38
38 Algebraic Properties

39
39 Operator Precedence 1. Kleene star 2. Concatenation 3. Union allows dropping unnecessary parentheses.

40
40 Converting Regular Expression to a DFA

41
41 Example: From a Regular Expression to an NFA Example : (a+b)*abb step by step construction 2 a 45 b 6 1 3 (a+b)

42
42 Example: From a Regular Expression to an NFA Example : (a+b)*abb 2 a 45 b 1 6 3 0 7 (a+b)*

43
43 Example: From a Regular Expression to an NFA Example : (a+b)*abb 2 a 45 b 1 0 10 a b b 7 6 3 8 9 (a+b)*abb

44
44 Converting FA to a Regular Expression State Elimination http://www.math.uu.se/~salling/Movies/FA_to_RegExpression.mov

45
45 Add a new start (s) and a new final (f) state: From s to the ex-starting state (1) From each ex-final state (1,3) to f Example, 1 2 3

46
46, 1 2 3 s f Let’s remove state 1! Each combination of input/output to 1 will generate a new path once state 1 is gone ;, ; ;,; The original:

47
47 When state 1 is gone we must be able to make all those transitions!, 1 2 3 s f () ;,, Previous:

48
48 ;, 1 2 3 s f ()

49
49, 1 2 3 s f ;, () ()

50
50 A common mistake: having several arrows between the same pair of states. Join the two arrows (by union of their regular expressions) before going on to the next step., 1 2 3 s f () ()

51
51, 1 2 3 s f ; () ()

52
52 Union again.., 1 2 3 s f () ()

53
53 2 3 s f () () Without state 1...

54
54 2 3 s f () ( Now we repeat the same procedure for the state 2...

55
55 Following the path s-2-3 we concatenate all strings on our way…don’t forget a* in 2! 2 3 s f () () ()

56
56 When 2 is removed, the path 3 - 2 –3 has also to be preserved, so we concatenate 3-2 string, take a* loop and go back 2-3 with string b! 2 3 s f () ( () ( () )

57
57 3 s f This is how the FA looks like without state 2: () ( () )

58
58 3 s f Finally we remove state 3… () ( () )

59
59 3 s f...so we concatenate strings s-3, loop in 3 and 3-f

60
60 Now we can omit state 3! s f From s we have two choices, empty string or the long expression OR is represented by union , as usually

61
61 So union the arrows... s f...and we are done!

62
62 Converting FA to a Regular Expression- An Algorithm for State Elimination

63
63 We expand our notion of NFA- to allow transitions on arbitrary regular expressions, not simply single symbols or. Successively eliminate states, replacing transitions that enter and leave a state with a more complicated regular expression, until eventually there are only two states left: a start state, an accepting state, and a single transition connecting them, labeled with a regular expression. The resulting regular expression is then our answer.

64
64 To begin with, the automaton should have a start state that has no transitions into it (including self-loops), and which is not accepting. If your automaton does not obey this property, add a new, non-accepting start state s, and add a -transition from s to the original start state. The automaton should also have a single accepting final state with no transitions leaving it, and no self- loops. If your automaton does not have it, add a new final state q, change all other accepting states to non- accepting, and add -transitions from them to q. This change clearly doesn't change the language accepted by the automaton.

65
65 Repeat the following steps, which eliminate a state: 1. Pick a non-start, non-accepting state q to eliminate. The state q will have i transitions in and j transitions out. Each will be labeled with a regular expression. For each of the ij combinations of transitions into and out of q, replace: pq r with p r And delete state q.

66
66 2. If several transitions go between a pair of states, replace them with a single transition that is the union of the individual transitions. E.g. replace: p r with p r

67
67 Example 1 342 1 34 1 4

68
68 N.B. common mistake! means See example on s 46 and 47 in Sallings book i.e. NOT

69
69 Minimizing DFA by Set Partitioning http://www.math.uu.se/~salling/Movies/SubsetConstruction.mov

70
70 Minimization of DFA The deterministic finite automata are not always the smallest possible accepting the source language. There may be states with the same "acceptance behavior". This applies to states p and q, if for all input words, the automaton always or never moves to a final state from p and q.

71
71 State Reduction by Set Partitioning (Särskiljandealgoritmen) The set partitioning technique is similar to one used for partitioning people into groups based on their responses to questionnaire. The following slides show the detailed steps for computing equivalent state sets of the starting DFA and constructing the corresponding reduced DFA.

72
72 b ba b b a a b a b a 4 3 1 2 a 5 0 b a a, b 3 1,2 a b 4,5 0 a, b Starting DFA Reduced DFA

73
73 Step 0: Partition the states into two groups accepting and non-accepting. State Reduction by Set Partitioning { 3, 4, 5 }{ 0, 1, 2 } P1P2 b ba bb a a b a b a 4 3 1 2 a 5 0

74
74 Step 1: Get the response of each state for each input symbol. Notice that States 3 and 0 show different responses from the ones of the other states in the same set. P 1 P 2 p 1 p 1 p 1 p 2 p 1 p 1 a a {3, 4, 5 } {0,1,2 } b b p 2 p 1 p 1 p 2 p 1 p 1 State Reduction by Set Partitioning b ba bb a a b a b a 4 3 1 2 a 5 0 P1P2

75
75 P 11 P 12 P 21 P 22 p 11 p 11 p 12 p 12 a a {4, 5} {3} {1, 2} {0} b b p 11 p 11 Step 2: Partition the sets according to the responses, and go to Step 1 until no partition occurs. No further partition is possible for the sets P11 and P21. So the final partition results are as follows: {4, 5} {3} {1, 2} {0}

76
76 {4, 5} {3} {1, 2} {0} Minimized DFA consists of four states of the final partition, and the transitions are the one corresponding to the starting DFA. b a a, b 3 1,2 a b 4,5 0 a, b b ba bb a a b a b a 4 3 1 2 a 5 0 b a 3 1,2 a b 4,5 0 a, b Minimized DFA Starting DFA

77
77 DFA Minimization Algorithm The algorithm P { F, {Q-F}} while ( P is still changing) T { } for each set s P for each partition s by into s 1, s 2, …, s k T T s 1, s 2, …, s k if T P then P T Why does this work? Partition P 2 Q (power set) Start off with 2 subsets of Q {F} and {Q-F} While loop takes P i P i+1 by splitting one or more sets P i+1 is at least one step closer to the partition with |Q | sets Maximum of |Q | splits Partitions are never combined Initial partition ensures that final states are intact This is a fixed-point algorithm!

78
78 Minimization and partitioning (Salling) Example 2.38 A minimal automaton Theorem 2.4 A DFA with N states is minimal if its language distinguishes N strings The automaton above is minimal, as it has 6 states and distinguishes 6 strings:

79
79 Two strings x,y are distinguished by a language (DFA) if there is a distinguishing string such that only one of strings xz, yz ends in accepting state (belongs to language).

80
80 A set of strings is distinguished by language if each pair of strings is distinguished.

81
81

82
82

83
83

84
84

85
85

86
86 Why ? We can try shorter strings. Will do? No, as both aba and aa get accept! Check!

87
87

88
88

89
89

90
90

91
91

92
92

93
93

94
94

95
95 Set Partition Algorithm (Salling book) Example 2.39 We apply step by step set partition algorithm on the following DFA: Two strings x,y are distinguished by language (DFA) if there is a (distinguishing) string z such as that only one of strings xz, yz ends in accepting state (belongs to language).

96
96 Non-accepting:Accepting: What happens when we read ? From 1 and 6 by a we reach 3 which is accepting. They form a special group. What happens when we read ? We search for strings that are distinguished by DFA! Distinguishing string: or From 5 we end in 6 by b, leaving its group.

97
97 The minimal automaton has four states:

98
98 The Chomsky Hierarchy

99
99 Regular Languages Context-Free Languages Non-regular languages

100
100 Some Additional Examples for your excersize

101
101 NFA N for (a+b)*abb Example of subset construction for NFA DFA

102
102 Translation table for DFA STATE INPUT SYMBOL ab ABCDEABCDE BBBBBBBBBB CDCECCDCEC Example of subset construction for NFA DFA

103
103 Result of applying the subset construction of NFA for (a+b)*abb. Example of subset construction for NFA DFA

104
104 Another Example of State Elimination

105
105 Another Example of State Elimination

106
106 Another Example of State Elimination Resulting Regular Expression

107
107 In General Removing states

108
108 Obtaining the final regular expression

109
109 Example: From an NFA to a DFA states ab ABC BBD CBC DBE EBC AB C D E ab b a bb b a a a

110
110 Example: From an NFA to a DFA states ab ABA BBD DBE EBA ABD E a b b b a a a b

111
111 Example: DFA Minimization s0s0 a s1s1 b s3s3 b s4s4 s2s2 a b b a a a b s 0, s 2 a s1s1 b s3s3 b s4s4 b a a a b

112
112 Example: DFA Minimization What about a ( b + c ) * ? First, the subset construction: q0q0 q1q1 a q4q4 q5q5 b q6q6 q7q7 c q3q3 q8q8 q2q2 q9q9 s3s3 s2s2 s0s0 s1s1 c b a b b c c Final states

113
113 Example: DFA Minimization Then, apply the minimization algorithm To produce the minimal DFA s3s3 s2s2 s0s0 s1s1 c b a b b c c s0s0 s1s1 a b, c final states

114
114 Grammars

115
115 Grammars Grammars express languages Example: the English language

116
116 barksverb singsverb dognoun birdnoun thearticle a

117
117 A derivation of “the bird sings”: birdthe verbbirdthe verbnounthe verbnounarticle predicatenounarticle predicatephrasenounsentence sings _

118
118 A derivation of “a dog barks”: barksdoga verbdoga verbnouna verbnounarticle verbphrasenoun predicatephrasenounsentence _ _

119
119 The language of the grammar:

120
120 Notation Non-terminal (Variable) Terminal Production rule

121
121 Example Derivation of sentence: Grammar:

122
122 Grammar: Derivation of sentence

123
123 Other derivations

124
124 The language of the grammar

125
125 Formal Definition Grammar Set of variables Set of terminal symbols Start variable Set of production rules

126
126 Example Grammar

127
127 Sentential Form A sentence that contains variables and terminals Example sentential formsSentence (sats)

128
128 We write: Instead of:

129
129 In general we write if By default ()

130
130 Example Grammar Derivations

131
131 baaaaaSbbbbaaSbb S Grammar Example Derivations

132
132 Another Grammar Example Derivations Grammar

133
133 More Derivations

134
134 The Language of a Grammar For a grammar with start variable String of terminals

135
135 Example For grammar Since

136
136 Notation

137
137 Linear Grammars

138
138 Linear Grammars Grammars with at most one variable (non-terminal) at the right side of a production Examples:

139
139 A Non-Linear Grammar Grammar

140
140 Another Linear Grammar Grammar

141
141 Right-Linear Grammars All productions have form: or Example

142
142 Left-Linear Grammars All productions have form or Example

143
143 Regular Grammars A grammar is a regular grammar if and only if it is right-linear or left-linear.

144
144 Regular Grammars Generate Regular Languages

145
145 Theorem Languages Generated by Regular Grammars Regular Languages

146
146 Theorem - Part 1 Languages Generated by Regular Grammars Regular Languages Any regular grammar generates a regular language

147
147 Theorem - Part 2 Any regular language is generated by a regular grammar Languages Generated by Regular Grammars Regular Languages

148
148 Proof – Part 1 The language generated by any regular grammar is regular Languages Generated by Regular Grammars Regular Languages

149
149 The case of Right-Linear Grammars Let be a right-linear grammar We will prove: is regular Proof idea We will construct NFA with

150
150 Grammar is right-linear Example

151
151 Construct NFA such that every state is a grammar variable: special final state

152
152 Add edges for each production:

153
153

154
154

155
155

156
156

157
157

158
158 Grammar NFA

159
159 In General A right-linear grammar has variables: and productions: or

160
160 We construct the NFA such that: each variable corresponds to a node: special final state ….

161
161 For each production: we add transitions and intermediate nodes ……… Example: http://www.cs.duke.edu/csed/jflap/tutorial/grammar/toFA/index.html Convert Right-Linear Grammar to FA by JFLAP

162
162 Example

163
163 The case of Left-Linear Grammars Let be a left-linear grammar We will prove: is regular Proof idea We will construct a right-linear grammar with

164
164 Since is left-linear grammar the productions look like:

165
165 Construct right-linear grammar In :

166
166 Construct right-linear grammar In :

167
167 It is easy to see that: Since is right-linear, we have: Regular Language Regular Language Regular Language

168
168 Proof - Part 2 Any regular language is generated by some regular grammar Languages Generated by Regular Grammars Regular Languages

169
169 Proof idea Any regular language is generated by some regular grammar Construct from a regular grammar such that Since is regular there is an NFA such that

170
170 Example

171
171 Convert to a right-linear grammar

172
172 In General For any transition: Add production: variableterminalvariable

173
173 For any final state: Add production:

174
174 Since is right-linear grammar is also a regular grammar with

175
175 Regular Grammars A regular grammar is any right-linear or left-linear grammar Examples

176
176 Observation Regular grammars generate regular languages Examples

177
177 Regular Languages Chomsky’s Language Hierarchy Non-regular languages

178
178 Application: Compiler Lexical Analysis (Lexikalisk analys i Kompilatorteori)

179
179 What is a compiler? A compiler is program that translates a source language into an equivalent target language while (i > 3) { a[i] = b[i]; i ++ } mov eax, ebx add eax, 1 cmp eax, 3 jcc eax, edx C program assembly program compiler does this

180
180 What is a compiler? class foo { int bar;... } struct foo { int bar;... } Java program compiler does this C program

181
181 What is a compiler? class foo { int bar;... }......................... Java program compiler does this Java virtual machine program

182
182 Phases of a compiler Lexical Analyzer Scanner Parser Semantic Analyzer Source Program Syntax Analyzer Tokens Parse Tree Abstract Syntax Tree with attributes

183
183 Compilation in a Nutshell Source code (character stream) Lexical analysis Parsing Token stream Abstract syntax tree (AST) Semantic Analysis if (b == 0) a = b; if(b)a=b;0== if == b0 = ab if == int b int 0 = int a lvalue int b boolean Decorated AST int ; ;

184
184 Stages of Analysis Lexical Analysis – breaking the input up into individual words/tokens Syntax Analysis – parsing the phrase structure of the program Semantic Analysis – calculating the program’s meaning

185
185 Lexical Analyzer Input is a stream of characters Produces a stream of names, keywords & punctuation marks Discards white space and comments

186
186 Recognize “ tokens ” in a program source code. The tokens can be variable names, reserved words, operators, numbers, … etc. Each kind of token can be specified as an RE, e.g., a variable name is of the form [A-Za- z][A-Za-z0-9]*. We can then construct an -NFA to recognize it automatically. Lexical Analyzer

187
187 By putting all these -NFA ’ s together, we obtain one which can recognize all different kinds of tokens in the input string. We can then convert this -NFA to NFA and then to DFA, and implement this DFA as a deterministic program - the lexical analyzer. Lexical Analyzer

188
188 RENFADFAMinimal DFA Thompson’s Contruction Subset Contruction Hopcroft Minimization

Similar presentations

OK

1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.

1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Work we do ppt online Ppt on art of war by sun Ppt on nitrogen cycle and nitrogen fixation equation Ppt on marketing mix product Ppt on world diabetes day facebook Ppt on product advertising campaign Ppt on rainwater harvesting free download Ppt on servant leadership Ppt on airbag working principle Ppt on green revolution