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

## Presentation on theme: "1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 4 Mälardalen University 2010."— Presentation transcript:

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

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 Two Basic Theorems

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

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

6 Regular Expressions and Regular Languages

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

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

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

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

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

12 Inductive Step We will prove: regular languages

13 By definition of regular expressions:

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 Therefore regular languages And trivially is a regular language

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

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

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

19 Reverse of a Regular Language

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 Proof Since is regular, there is NFA that accepts Example:

22 Invert Transitions

23 Make old initial state a final state and vice versa

24 Add a new initial state

25 Resulting machine accepts is regular

26 Some Properties of Regular Languages, Summary

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

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

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

30 Example

31 Union (Thompson’s construction) NFA for

32 NFA for Example

33 Concatenation (Thompson’s construction) NFA for

34 Example NFA for

35 Star Operation (Thompson’s construction) NFA for

36 Example NFA for

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 Algebraic Properties

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

40 Converting Regular Expression to a DFA

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 Example: From a Regular Expression to an NFA Example : (a+b)*abb 2 a 45 b 1 6 3 0 7 (a+b)*

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 Converting FA to a Regular Expression State Elimination http://www.math.uu.se/~salling/Movies/FA_to_RegExpression.mov

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, 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 When state 1 is gone we must be able to make all those transitions!, 1 2 3 s f  () ;,, Previous:

48 ;, 1 2 3 s f  ()

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

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, 1 2 3 s f ;  ()  () 

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

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

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

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 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 3 s f This is how the FA looks like without state 2:   ()  (  ()   )

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

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

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 So union the arrows... s f...and we are done!

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

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 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 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 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 Example 1 342 1 34 1 4

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

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

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 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 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 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 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 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 {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 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 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 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 A set of strings is distinguished by language if each pair of strings is distinguished.

81

82

83

84

85

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

87

88

89

90

91

92

93

94

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 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 The minimal automaton has four states:

98 The Chomsky Hierarchy

99 Regular Languages Context-Free Languages Non-regular languages

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

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

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

104 Another Example of State Elimination

105 Another Example of State Elimination

106 Another Example of State Elimination Resulting Regular Expression

107 In General Removing states

108 Obtaining the final regular expression

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 Example: From an NFA to a DFA states ab ABA BBD DBE EBA ABD E a b b b a a a b

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 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 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 Grammars

115 Grammars Grammars express languages Example: the English language

116 barksverb singsverb   dognoun birdnoun   thearticle a  

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

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

119 The language of the grammar:

120 Notation Non-terminal (Variable) Terminal Production rule

121 Example Derivation of sentence: Grammar:

122 Grammar: Derivation of sentence

123 Other derivations

124 The language of the grammar

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

126 Example Grammar

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

129 In general we write if By default ()

130 Example Grammar Derivations

131 baaaaaSbbbbaaSbb   S   Grammar Example Derivations

132 Another Grammar Example Derivations Grammar

133 More Derivations

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

135 Example For grammar Since

136 Notation

137 Linear Grammars

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

139 A Non-Linear Grammar Grammar

140 Another Linear Grammar Grammar

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

142 Left-Linear Grammars All productions have form or Example

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

144 Regular Grammars Generate Regular Languages

145 Theorem Languages Generated by Regular Grammars Regular Languages

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

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

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

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 Grammar is right-linear Example

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

152 Add edges for each production:

153

154

155

156

157

158 Grammar NFA

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

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

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 Example

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 Since is left-linear grammar the productions look like:

165 Construct right-linear grammar In :

166 Construct right-linear grammar In :

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

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

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 Example

171 Convert to a right-linear grammar

172 In General For any transition: Add production: variableterminalvariable

173 For any final state: Add production:

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

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

176 Observation Regular grammars generate regular languages Examples

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

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

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 What is a compiler? class foo { int bar;... } struct foo { int bar;... } Java program compiler does this C program

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

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

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 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 Lexical Analyzer Input is a stream of characters Produces a stream of names, keywords & punctuation marks Discards white space and comments

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 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 RENFADFAMinimal DFA Thompson’s Contruction Subset Contruction Hopcroft Minimization

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

Similar presentations