AUBER F4 - PowerPoint PPT Presentation

1 / 188
About This Presentation
Title:

AUBER F4

Description:

... Phases of a compiler Lexical Analyzer Scanner Parser Semantic Analyzer Source Program Syntax Analyzer Tokens Parse Tree Abstract ... verb noun the verb noun ... – PowerPoint PPT presentation

Number of Views:140
Avg rating:3.0/5.0
Slides: 189
Provided by: Gordan85
Category:
Tags: auber | abstract | noun | phases

less

Transcript and Presenter's Notes

Title: AUBER F4


1
CD5560 FABER Formal Languages, Automata and
Models of Computation Lecture 4 Mälardalen
University 2010
2
Regular 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
3
Two Basic Theorems
4
1 KleeneTheoremNFA?DFA Let M be NFA accepting
L. Then there exists DFA M that accepts L as
well.
5
II Finite Language TheoremAny finite language
is FSA-acceptable.
Example L abba, abb, abab
FSA Finite State Automaton DFA/NFA
6
Regular ExpressionsandRegular Languages
7
Theorem - Part 1
Languages Generated by Regular Expressions
Regular Languages
8
Theorem - Part 2
Languages Generated by Regular Expressions
Regular Languages
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

(where )
Regular Languages
11
Inductive Hypothesis
that and are regular languages
12
Inductive Step
  • We will prove

regular languages
13
  • By definition of regular expressions

14
It is known, and we will prove in this lecture
Regular languages are closed under
union
concatenation
star
15
  • Therefore

regular languages
is a regular language
And trivially
16
Proof Part 2
For any regular language there is
a regular expression with
Proof by construction of regular expression
17
Since 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
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
26
Some Properties of Regular Languages, Summary
27
Properties
For regular languages and we will
prove that
28
Regular languages are closed under
29
(No Transcript)
30
Example
31
Union (Thompsons construction)
  • NFA for

32
Example
33
Concatenation (Thompsons construction)
  • NFA for

34
Example
  • NFA for

35
Star Operation (Thompsons construction)
  • NFA for

36
Example
  • NFA for

37
Summary 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

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 (ab)abb step by step construction
(ab)
a
?
3
2
?
1
?
?
4
5
b
42
Example From a Regular Expression to an NFA
Example (ab)abb
(ab)
?
a
2
3
?
?
?
?
0
1
6
?
?
4
5
b
?
43
Example 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
44
Converting FA to a Regular ExpressionState
Eliminationhttp//www.math.uu.se/salling/Movies/
FA_to_RegExpression.mov
45
Example
  • 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
47
When 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
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.
,
2
1
3
f
51
,
2
1
3
f
52
Union again..
?
(
)
,
2
1
3
?
f
53
Without state 1...
?
(
)
s
2
3
?
f
54
Now we repeat the same procedure for the state
2...
?
(
)
2
?
(
?
3
?
f
55
Following the path s-2-3 we concatenate all
strings on our waydont forget a in 2!
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!
57
This is how the FA looks like without state 2
3
f
58
Finally we remove state 3
3
f
59
...so we concatenate strings s-3, loop in 3 and
3-f
3
f
60
Now 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
61
So union the arrows...
...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
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
67
Example
3
4
1
68
  • N.B. common mistake!

means
i.e.
NOT
See example on s 46 and 47 in Sallings book
69
Minimizing DFAby Set Partitioninghttp//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
Starting DFA
Reduced DFA
73
State Reduction by Set Partitioning
  • Step 0 Partition the states into two groups
  • accepting and non-accepting.

74
State 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
75
Step 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
76
Minimized 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
77
DFA 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
  • The algorithm

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!
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
79





Two 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).
80





A 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)
86





Why ?
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)
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
We 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.
97
The minimal automaton has four states
98
The Chomsky Hierarchy
99
Non-regular languages
Context-Free Languages
Regular Languages
100
Some Additional Examplesfor your excersize
101
NFA N for (ab)abb
Example of subset construction for NFA ? DFA
102
Translation 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
103
Result 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

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
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
110
Example 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
111
Example DFA Minimization
112
Example DFA Minimization
  • What about a ( b c ) ?
  • First, the subset construction

?
b
q4
q5
?
?
?
a
?
?
q0
q1
q3
q2
q9
q8
c
?
?
q6
q7
?
113
Example DFA Minimization
  • Then, apply the minimization algorithm
  • To produce the minimal DFA

final states
114
Grammars

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

120
Notation

Non-terminal (Variable)
Terminal
Production rule
121
Example
Grammar
  • Derivation of sentence

122
  • Derivation of sentence

123
  • Other derivations

124
  • The language of the grammar

125
Formal Definition
  • Grammar

126
Example
  • Grammar

127
  • Sentential Form
  • A sentence that contains
  • variables and terminals
  • Example

128
  • We write
  • Instead of

129
  • By default

(
)
130
Example

Grammar
Derivations
131
Example
Grammar
Derivations
132
Another Grammar Example
Grammar
Derivations
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 GenerateRegular Languages
145
Theorem
146
Theorem - Part 1
Any regular grammar generates a regular language
147
Theorem - Part 2
Any regular language is generated by a regular
grammar
148
Proof Part 1
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

152
  • Add edges for each production

153
(No Transcript)
154

155

156

157

158
NFA
Grammar

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


Examplehttp//www.cs.duke.edu/csed/jflap/tutoria
l/grammar/toFA/index.htmlConvert 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
In
166
Construct right-linear grammar

In
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
169
Any regular language is generated by some
regular grammar
Proof idea
170
Example
171
Convert to a right-linear grammar

172
In General
For any transition
Add production
variable
terminal
variable
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
Chomskys Language Hierarchy
Non-regular languages
Regular Languages
178
Application CompilerLexical Analysis(Lexikalisk
analys i Kompilatorteori)
179
What is a compiler?
  • A compiler is program that translates a source
    language into an equivalent target language

180
What is a compiler?
181
What is a compiler?
182
Phases of a compiler
Source Program
Scanner
Tokens
Parser
Parse Tree
Abstract Syntax Tree with attributes
183
Compilation 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
184
Stages 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

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

186
Lexical 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.

187
Lexical 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)
Write a Comment
User Comments (0)
About PowerShow.com