Title: AUBER F4
1 CD5560 FABER Formal Languages, Automata and
Models of Computation Lecture 4 Mälardalen
University 2010
2Regular Expressions and Regular
LanguagesNFA?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
3Two Basic Theorems
41 KleeneTheoremNFA?DFA Let M be NFA accepting
L. Then there exists DFA M that accepts L as
well.
5II Finite Language TheoremAny finite language
is FSA-acceptable.
Example L abba, abb, abab
FSA Finite State Automaton DFA/NFA
6Regular ExpressionsandRegular Languages
7Theorem - Part 1
Languages Generated by Regular Expressions
Regular Languages
8Theorem - Part 2
Languages Generated by Regular Expressions
Regular Languages
9Proof - Part 1
For any regular expression the
language is regular
Proof by induction on the size of
10Induction Basis
- Primitive Regular Expressions
(where )
Regular Languages
11Inductive Hypothesis
that and are regular languages
12Inductive Step
regular languages
13- By definition of regular expressions
14It is known, and we will prove in this lecture
Regular languages are closed under
union
concatenation
star
15regular languages
is a regular language
And trivially
16Proof Part 2
For any regular language there is
a regular expression with
Proof by construction of regular expression
17Since is regular take the NFA that
accepts it
Single final state
18- From construct the equivalent
- Generalized Transition Graph
- transition labels are regular expressions
Example
19Reverse of a Regular Language
20Theorem
The reverse of a regular language is a
regular language
Proof idea
Construct NFA that accepts
invert the transitions of the NFA that accepts
21Proof
Since is regular, there is NFA that accepts
Example
22Invert Transitions
23Make old initial state a final state and vice
versa
24Add a new initial state
25Resulting machine accepts
26Some Properties of Regular Languages, Summary
27Properties
For regular languages and we will
prove that
28Regular languages are closed under
29(No Transcript)
30Example
31Union (Thompsons construction)
32Example
33Concatenation (Thompsons construction)
34Example
35Star Operation (Thompsons construction)
36Example
37Summary Operations on Regular Expressions
- RE Regular language description
- ab a,b
- (ab)(ab) aa, ab, ba, bb
- a ?, a, aa, aaa,
- ab b,ab, aab, aaab,
- (ab) ?, a, b, aa, ab, ba, aaa,
bbb
38Algebraic Properties
39Operator Precedence
- 1. Kleene star
- 2. Concatenation
- 3. Union
- allows dropping unnecessary parentheses.
40Converting Regular Expression to a DFA
41Example From a Regular Expression to an NFA
Example (ab)abb step by step construction
(ab)
a
?
3
2
?
1
?
?
4
5
b
42Example From a Regular Expression to an NFA
Example (ab)abb
(ab)
?
a
2
3
?
?
?
?
0
1
6
?
?
4
5
b
?
43Example From a Regular Expression to an NFA
Example (ab)abb
(ab)abb
?
a
2
3
?
?
?
0
?
1
a
6
7
8
?
?
4
5
b
b
9
b
?
10
44Converting FA to a Regular ExpressionState
Eliminationhttp//www.math.uu.se/salling/Movies/
FA_to_RegExpression.mov
45Example
- 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
46,
2
1
The original
3
f
Lets remove state 1! Each combination of
input/output to 1 will generate a new path once
state 1 is gone
47When state 1 is gone we must be able to make all
those transitions!
,
2
1
Previous
3
,
f
48,
2
1
3
f
49,
2
1
3
f
50A 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.
,
2
1
3
f
51,
2
1
3
f
52Union again..
?
(
)
,
2
1
3
?
f
53Without state 1...
?
(
)
s
2
3
?
f
54Now we repeat the same procedure for the state
2...
?
(
)
2
?
(
?
3
?
f
55Following the path s-2-3 we concatenate all
strings on our waydont forget a in 2!
56When 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!
57This is how the FA looks like without state 2
3
f
58Finally we remove state 3
3
f
59...so we concatenate strings s-3, loop in 3 and
3-f
3
f
60Now we can omit state 3!
f
From s we have two choices, empty string or the
long expression OR is represented by union ?, as
usually
61So union the arrows...
...and we are done!
62Converting 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.
65Repeat 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
r
p
q
with
r
p
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
r
p
67Example
3
4
1
68means
i.e.
NOT
See example on s 46 and 47 in Sallings book
69Minimizing DFAby Set Partitioninghttp//www.math
.uu.se/salling/Movies/SubsetConstruction.mov
70Minimization 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.
71State 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.
72Starting DFA
Reduced DFA
73State Reduction by Set Partitioning
- Step 0 Partition the states into two groups
- accepting and non-accepting.
-
74State Reduction by Set Partitioning
- 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. - P1 P2
-
- p1 p1 p1 p2 p1 p1
- a? ? ? ? a? ? ? ?
- 3, 4, 5 0, 1, 2
- b? ? ? ? b? ? ? ?
- p2 p1 p1 p2 p1 p1
-
-
P1
P2
75Step 2 Partition the sets according to the
responses, and go to Step 1 until no partition
occurs.
P11 P12 P21 P22 p11 p11 p12
p12 a? ? ? a? ? ? 4, 5
3 1, 2 0
b? ? ? b? ? ? p11 p11
p11 p11
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
76Minimized DFA consists of four states of the
final partition, and the transitions are the one
corresponding to the starting DFA.
4, 5 3 1, 2 0
Minimized DFA
Starting DFA
77DFA Minimization Algorithm
Why does this work? Partition P ? 2Q (power
set) Start off with 2 subsets of Q F and
Q-F While loop takes Pi?Pi1 by splitting one
or more sets Pi1 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
P ? F, Q-F while ( P is still changing)
T ? for each set s ? P for each ?
? ? partition s by ?
into s1, s2, , sk T ? T ? s1, s2, ,
sk if T ? P then P ? T
This is a fixed-point algorithm!
78Minimization 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
79Two strings x,y are distinguished by a language
(DFA) if there is a distinguishing stringsuch
that only one of strings xz, yz ends in accepting
state (belongs to language).
80A set of strings is distinguished by language if
each pair of strings is distinguished.
81(No Transcript)
82(No Transcript)
83(No Transcript)
84(No Transcript)
85(No Transcript)
86Why ?
We can try shorter strings. Will do?
No, as both aba and aa get accept! Check!
87(No Transcript)
88(No Transcript)
89(No Transcript)
90(No Transcript)
91(No Transcript)
92(No Transcript)
93(No Transcript)
94(No Transcript)
95Set 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).
96We search for strings that are distinguished by
DFA!
Non-accepting
Accepting
Distinguishing string or
What happens when we read ?
From 1 and 6 by a we reach 3 which is accepting.
They form a special group.
From 5 we end in 6 by b, leaving its group.
97The minimal automaton has four states
98The Chomsky Hierarchy
99Non-regular languages
Context-Free Languages
Regular Languages
100Some Additional Examplesfor your excersize
101NFA N for (ab)abb
Example of subset construction for NFA ? DFA
102Translation table for DFA
Example of subset construction for NFA ? DFA
STATE INPUT SYMBOL INPUT SYMBOL
STATE a b
A B C D E B B B B B C D C E C
103Result of applying the subset construction of NFA
for (ab)abb.
Example of subset construction for NFA ? DFA
104- Another Example of State Elimination
105- Another Example of State Elimination
106Another Example of State Elimination
Resulting Regular Expression
107In General
108- Obtaining the final regular expression
109Example From an NFA to a DFA
b
states a b
A B C
B B D
C B C
D B E
E B C
C
b
b
a
a
b
b
A
B
D
E
a
a
a
110Example From an NFA to a DFA
states a b
A B A
B B D
D B E
E B A
b
b
b
a
b
A
B
D
E
a
a
a
111Example DFA Minimization
112Example DFA Minimization
- What about a ( b c ) ?
- First, the subset construction
?
b
q4
q5
?
?
?
a
?
?
q0
q1
q3
q2
q9
q8
c
?
?
q6
q7
?
113Example DFA Minimization
- Then, apply the minimization algorithm
- To produce the minimal DFA
final states
114Grammars
115Grammars
- Grammars express languages
- Example the English language
116 117- A derivation of the bird sings
118- A derivation of a dog barks
119- The language of the grammar
120Notation
Non-terminal (Variable)
Terminal
Production rule
121Example
Grammar
122 123 124- The language of the grammar
125Formal Definition
126Example
127- Sentential Form
- A sentence that contains
- variables and terminals
- Example
128 129(
)
130Example
Grammar
Derivations
131Example
Grammar
Derivations
132Another Grammar Example
Grammar
Derivations
133More Derivations
134The Language of a Grammar
- For a grammar with start variable
String of terminals
135Example
Since
136Notation
137Linear Grammars
138Linear Grammars
- Grammars with
- at most one variable (non-terminal)
- at the right side of a production
-
Examples
139A Non-Linear Grammar
Grammar
140Another Linear Grammar
141Right-Linear Grammars
- All productions have form
or
Example
142Left-Linear Grammars
- All productions have form
or
Example
143Regular Grammars
- A grammar is a regular grammar if and only if it
is right-linear or left-linear.
144Regular Grammars GenerateRegular Languages
145Theorem
146Theorem - Part 1
Any regular grammar generates a regular language
147Theorem - Part 2
Any regular language is generated by a regular
grammar
148Proof Part 1
149The case of Right-Linear Grammars
- Let be a right-linear grammar
- We will prove is regular
- Proof idea We will construct NFA
- with
150Example
151- Construct NFA such that every state
- is a grammar variable
152- Add edges for each production
153(No Transcript)
154 155 156 157 158NFA
Grammar
159In 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
Examplehttp//www.cs.duke.edu/csed/jflap/tutoria
l/grammar/toFA/index.htmlConvert Right-Linear
Grammar to FA by JFLAP
162Example
163The 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
In
166Construct right-linear grammar
In
In
167- It is easy to see that
- Since is right-linear, we have
-
-
Regular Language
Regular Language
Regular Language
168Proof - Part 2
Any regular language is generated by some
regular grammar
169Any regular language is generated by some
regular grammar
Proof idea
170Example
171Convert to a right-linear grammar
172In General
For any transition
Add production
variable
terminal
variable
173For any final state
Add production
174- Since is right-linear grammar
-
- is also a regular grammar with
-
175Regular Grammars
- A regular grammar is any
- right-linear or left-linear grammar
Examples
176Observation
- Regular grammars generate regular languages
Examples
177Chomskys Language Hierarchy
Non-regular languages
Regular Languages
178Application CompilerLexical Analysis(Lexikalisk
analys i Kompilatorteori)
179What is a compiler?
- A compiler is program that translates a source
language into an equivalent target language
180What is a compiler?
181What is a compiler?
182Phases of a compiler
Source Program
Scanner
Tokens
Parser
Parse Tree
Abstract Syntax Tree with attributes
183Compilation in a Nutshell
Source code (character stream)
if (b 0) a b
Lexical analysis
if
(
b
)
a
b
0
Token stream
Parsing
if
Abstract syntax tree (AST)
b
0
a
b
Semantic Analysis
if
boolean
int
Decorated AST
int b
int 0
int a lvalue
int b
184Stages of Analysis
- Lexical Analysis breaking the input up into
individual words/tokens - Syntax Analysis parsing the phrase structure
ofthe program - Semantic Analysis calculating the programs
meaning
185Lexical Analyzer
- Input is a stream of characters
- Produces a stream of names, keywords
punctuation marks - Discards white space and comments
186Lexical Analyzer
- 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-zA-Za-z0-9. - We can then construct an ? -NFA to recognize it
automatically.
187Lexical Analyzer
- By putting all these ?-NFAs 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.
188(No Transcript)