Lesson 13
Lesson 13
Lesson 13
Overview
of
Previous Lesson(s)
Over View
An NFA accepts a string if the symbols of the string specify a path
from the start to an accepting state.
In such a case the NFA does accept the string, one successful path is
enough.
3
Over View..
A deterministic finite automaton (DFA) is a special case of an NFA
where:
For each state S and input symbol a, there is exactly one edge out of s
labeled a.
4
Over View...
Algorithm for converting any RE to an NFA .
5
Over View...
Method:
The rules for constructing an NFA consist of basis rules for handling
subexpressions with no operators.
Inductive rules for constructing larger NFA's from the NFA's for the
immediate sub expressions of a given expression.
6
Over View...
Basis Step:
7
Over View...
Induction Step:
Suppose N(s) and N(t) are NFA's for regular expressions s and t,
respectively.
If r = s|t. Then N(r) , the NFA for r, should be constructed as
9
Over View...
Now Suppose r = s* , Then N(r) , the NFA for r, should be constructed as
N(r) accept all the strings in L(s)1 , L(s)2 , and so on , so the entire set of strings
accepted by N(r) is L(s*).
Finally suppose r = (s) , Then L(r) = L(s) and we can use the NFA N(s) as N(r).
10
TODAY’S LESSON
11
Contents
Design of a Lexical-Analyzer Generator
12
Lexical-Analyzer Design
Here we will see the designing technique in generating a lexical-
analyzer.
13
Structure of the Generated Analyzer
Its components are:
14
Structure of the Generated Analyzer
Architecture of a lexical analyzer generated by Lex.
15
Structure of the Generated Analyzer
To construct the automaton, we begin by taking each regular-
expression pattern in the Lex program and converting it to an NFA.
So we combine all the NFA's into one by introducing a new start state
with ɛ-transitions to each of the start states of the NFA's Ni for
pattern Pi
16
Structure of the Generated Analyzer
An NFA constructed from a Lex program
17
Pattern Matching Based on NFA 's
For pattern based matching the simulator starts reading characters
and calculates the set of states.
At some point the input character does not lead to any state or we
have reached the eof.
Since we wish to find the longest lexeme matching the pattern we
proceed backwards from the current point (where there was no state)
until we reach an accepting state (i.e., the set of NFA states, N-states,
contains an accepting N-state).
Each accepting N-state corresponds to a matched pattern.
The lex rule is that if a lexeme matches multiple patterns we choose
the pattern listed first in the lex-program.
18
Pattern Matching Based on NFA's..
Ex. Consider three patterns and their associated actions and
consider processing the input aaba.
a Action A1
abb Action A2
a*b+ Action A3
19
Pattern Matching Based on NFA's…
We begin by constructing the three NFAs.
20
Pattern Matching Based on NFA's…
We introduce a new start state and ε-transitions as discussed in
the previous section.
21
Pattern Matching Based on NFA's…
We start at the ε-closure of the start state, which is {0,1,3,7}.
22
Pattern Matching Based on NFA's…
We are back in {8} and ask if one of these N-states is an accepting
state.
23
DFA for Lexical Analyzer
In this section we see an architecture to convert the NFA for all the
patterns into an equivalent DFA, using the subset construction
mechanism of DFA from NFA.
Within each DFA state, if there are one or more accepting NFA states,
determine the first pattern whose accepting state is represented, and
make that pattern the output of the DFA state.
24
DFA for Lexical Analyzer..
A transition graph for the DFA handling the patterns a, abb and
a*b+ that is constructed by the subset construction from the NFA.
25
DFA for Lexical Analyzer…
The accepting states are labeled by the pattern that is matched by
that state.
For instance, the state {6, 8 } has two accepting states, corresponding
to patterns abb and a*b+.
Since the former is listed first, that is the pattern associated with state
{6,8}.
26
DFA for Lexical Analyzer…
In the diagram, when there is no NFA state possible, we do not
show the edge.
27
Optimization of DFA-based Pattern Matchers
Now we will talk about some algorithms that have been used to
implement and optimize pattern matchers constructed from
regular expressions.
28
Optimization of DFA-based Pattern Matchers..
The algorithm itself is quite efficient, running in time O(n log n),
where n is the number of states of the DFA.
29
Important States of an NFA
Prior to begin our discussion of how to go directly from a regular
expression to a DFA, we must first dissect the NFA construction
and consider the roles played by various states.
30
Important States of an NFA..
During the subset construction, two sets of NFA states can be
identified if they:
31
Important States of an NFA...
The constructed NFA has only one accepting state, but this state,
having no out-transitions, is not an important state.
32
Important States of an NFA...
It is useful to present the regular expression by its syntax tree,
where the leaves correspond to operands and the interior nodes
correspond to operators.
33
Important States of an NFA...
Ex. Syntax tree for (a|b)*abb#
34
Thank You