Download presentation
Presentation is loading. Please wait.
Published byLily Hensley Modified over 8 years ago
1
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2005
2
2 - Applications: Compiler Lexical Analysis - Regular Expressions and Regular Languages - NFA DFA (Subset Construction -Delmängdskonstruktion) - FA RE (State Elimination - Tillståndseliminering) - Minimizing DFA by Set Partitioning (Särskiljandealgoritmen) Content
3
3 Application: Compiler Lexical Analysis (Lexikalisk analys i Kompilatorteori)
4
4 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
5
5 What is a compiler? class foo { int bar;... } struct foo { int bar;... } Java program compiler does this C program
6
6 What is a compiler? class foo { int bar;... }......................... Java program compiler does this Java virtual machine program
7
7 Phases of a compiler Lexical Analyzer Scanner Parser Semantic Analyzer Source Program Syntax Analyzer Tokens Parse Tree Abstract Syntax Tree with attributes
8
8 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 ; ;
9
9 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
10
10 Lexical Analyzer Input is a stream of characters Produces a stream of names, keywords & punctuation marks Discards white space and comments
11
11 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
12
12 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
13
13 RENFADFAMinimal DFA Thompson’s Contruction Subset Contruction Hopcroft Minimization
14
14 Two Basic Theorems
15
15 Theorem NFA DFA (Kleene) Let M be NFA accepting L. Then there exists DFA M´ that accepts L as well.
16
16 Finite Language Theorem Any finite language is FSA-acceptable. Example L = {abba, abb, abab} FSA = Finite State Automaton = DFA/NFA
17
17 Regular Expressions and Regular Languages
18
18 Theorem - Part 1 Languages Generated by Regular Expressions Regular Languages 1. For any regular expression the language is regular
19
19 Theorem - Part 2 Languages Generated by Regular Expressions Regular Languages 2. For any regular language there is a regular expression with
20
20 Proof - Part 1 For any regular expression the language is regular Proof by induction on the size of
21
21 Induction Basis Primitive Regular Expressions: NFAs Regular Languages (where )
22
22 Inductive Hypothesis Assume for regular expressions and that and are regular languages
23
23 Inductive Step We will prove: regular languages
24
24 By definition of regular expressions:
25
25 By inductive hypothesis we know: and are regular languages Regular languages are closed under union concatenation star We also know:
26
26 Therefore regular languages And trivially is a regular language
27
27 Proof – Part 2 For any regular language there is a regular expression with Proof by construction of regular expression
28
28 Single final state Since is regular take the NFA that accepts it
29
29 From construct the equivalent Generalized Transition Graph transition labels are regular expressions Example
30
30 Reverse of a Regular Language
31
31 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
32
32 Proof Since is regular, there is NFA that accepts Example:
33
33 Invert Transitions
34
34 Make old initial state a final state and vice versa
35
35 Add a new initial state
36
36 Resulting machine accepts is regular
37
37 Some Properties of Regular Languages, Summary
38
38 Properties Concatenation: Star: Union: Are Regular Languages For regular languages and we will prove that:
39
39 Regular languages are closed under Concatenation:Star: Union:
40
40 Regular language Single final state NFA Regular language Single final state NFA
41
41 Example
42
42 Union (Thompson’s construction) NFA for
43
43 NFA for Example
44
44 Concatenation (Thompson’s construction) NFA for
45
45 Example NFA for
46
46 Star Operation (Thompson’s construction) NFA for
47
47 Example NFA for
48
48 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…}
49
49 Algebraic Properties
50
50 Operator Precedence 1. Kleene star 2. Concatenation 3. Union allows dropping unnecessary parentheses.
51
51 Converting Regular Expression to a DFA
52
52 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)
53
53 Example: From a Regular Expression to an NFA Example : (a+b)*abb 2 a 45 b 1 6 3 0 7 (a+b)*
54
54 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
55
55 Converting FA to a Regular Expression
56
56 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
57
57, 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:
58
58 When state 1 is gone we must be able to make all those transitions!, 1 2 3 s f () ;,, Previous:
59
59 ;, 1 2 3 s f ()
60
60, 1 2 3 s f ;, () ()
61
61 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 () ()
62
62, 1 2 3 s f ; () ()
63
63 Union again.., 1 2 3 s f () ()
64
64 2 3 s f () () Without state 1...
65
65 2 3 s f () ( Now we repeat the same procedure for the state 2...
66
66 Following the path s-2-3 we concatenate all strings on our way…don’t forget a* in 2! 2 3 s f () () ()
67
67 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 () ( () ( () )
68
68 3 s f This is how the FA looks like without state 2: () ( () )
69
69 3 s f Finally we remove state 3… () ( () )
70
70 3 s f...so we concatenate strings s-3, loop in 3 and 3-f
71
71 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
72
72 So union the arrows... s f...and we are done!
73
73 Converting FA to a Regular Expression- An Algorithm
74
74 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.
75
75 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.
76
76 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.
77
77 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
78
78 Example 1 342 1 34 1 4
79
79 N.B. common mistake! means See example on s 46 and 47 in Sallings book i.e. NOT
80
80 Minimizing DFA
81
81 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.
82
82 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.
83
83 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
84
84 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
85
85 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
86
86 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}
87
87 {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
88
88 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!
89
89 Minimering och särskiljande (Salling) Exempel 2.38 En minimal automat Sats 2.4 En DFA med N tillstånd är minimal om dess språk särskiljer N strängar Automaten ovan är minimal, för att den har sex tillstånd och särskiljer sex strängar:
90
90 Två strängar x,y särskiljs av språket (DFA) om det finns någon (särskiljande) sträng sådan att enbart en av strängarna xz, yz hamnar i accepterande tillstånd (tillhör språket).
91
91 En mängd av strängar särskiljs av språket om varje par av strängar särskiljs.
92
92
93
93
94
94
95
95
96
96
97
97 Varför ? Vi kan testa kortare strängar. Duger ? Nej, för att både aba och aa accepteras! Osv. Testa själv!
98
98
99
99
100
100
101
101
102
102
103
103
104
104
105
105
106
106 Stegvisa särskiljandealgoritmen (Salling) Exempel 2.39 Vi tillämpar stegvisa särskiljandealgoritmen på följande slösaktiga DFA: Två strängar x,y särskiljs av språket (DFA) om det finns någon (särskiljande) sträng z sådan att enbart en av strängarna xz, yz hamnar i accepterande tillstånd (tillhör språket).
107
107 Icke-accepterande:Accepterande: Vad händer när man läser ett ? Från 1 och 6 på ett a når man 3 som är accepterande. De bildar egen grupp. Vad händer när man läser ett ? Nu letar vi strängar som särskiljs! Särkiljande strängar: eller Från 5 hamnar man i 6 på ett b. Lämnar sin grupp.
108
108 Den minimala automaten har fyra tillstånd:
109
109 The Chomsky Hierarchy
110
110 Regular Languages Context-Free Languages Non-regular languages
111
111 Some Additional Examples
112
112 NFA N for (a+b)*abb Example of subset construction for NFA DFA
113
113 Translation table for DFA STATE INPUT SYMBOL ab ABCDEABCDE BBBBBBBBBB CDCECCDCEC Example of subset construction for NFA DFA
114
114 Result of applying the subset construction of NFA for (a+b)*abb. Example of subset construction for NFA DFA
115
115 Another Example of State Elimination
116
116 Another Example of State Elimination
117
117 Another Example of State Elimination Resulting Regular Expression
118
118 In General Removing states
119
119 Obtaining the final regular expression
120
120 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
121
121 Example: From an NFA to a DFA states ab ABA BBD DBE EBA ABD E a b b b a a a b
122
122 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
123
123 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
124
124 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.