Escolar Documentos
Profissional Documentos
Cultura Documentos
a(a|d)*
Winter 2007 SEG2101 Chapter 8 3
Rules
is a regular expression that denotes {}, the set
containing empty string.
If a is a symbol in , then a is a regular expression that
denotes {a}, the set containing the string a.
Suppose r and s are regular expressions denoting the
language L(r) and L(s), then
(r) |(s) is a regular expression denoting L(r)L(s).
(r)(s) is regular expression denoting L (r) L(s).
(r) * is a regular expression denoting (L (r) )*.
(r) is a regular expression denoting L (r).
= 3 return(relop, NE)
other
*
4 return(relop, LT)
> 5 return(relop, EQ)
=
6 7 return(relop, GE)
other *
8 return(relop, GT)
Recognition of Reserved Words
and Identifiers
Problem: keywords look like identifiers
Solution:
Install the reserved words in the symbol table
initially
Create separate transition diagrams for each
keyword
Examples for Identifiers and
Keywords
start *
letter other
9 10 11 return(getToken(),
installID())
letter or digit
*
Completion of the Running
Example – Unsigned Numbers
3.14E-5
3.14
314
Transition Diagram for
Whitespace
delim
*
start delim other
22 23 24
. deli
digi m
* start deli other *
14
digit t15 other
21 22 m
23 24
E
E digit
+ or - digit other *
16 17 18 19
digit
Transition Diagram
C code to find next start state
C Code for Lexical analyzers
Finite Automata
Finite automata are recognizers
They simply say “yes” or “no” about each input
string
Two kinds:
Nondeterministic finite automata (NFA)
No restrictions on the labels of the edges
Deterministic finite automata (DFA)
For
each state, and for each symbol, there’s exactly
one edge with that symbol leaving that state
Nondeterministic Finite Automata
NFA consists of
A finite set of states S
A set of input symbol , the input alphabet
A transition function that gives, for each state,
and for each symbol in ∪{} a set of states
A state s0 from S (the start state or initial state)
A set of states F, a subset of S (the accepting
states, or final states)
NFA can be represented by a transition
graph
There’s an edge labeled a from state s to
state t iff t is one of the next states for state s
and input a
It’s similar to a transition diagram except:
The same symbol can label edges from one state
to several different states
An edge may be labeled by , in addition to
symbols from the input alphabet
An Example NFA: (a|b)*abb
b
2 4
b
Deterministic Finite Automata
DFA is a special case of an NFA where:
There are no moves on input
For each state s and input symbol a, there’s
exactly one edge out of s labeled s
Every regular expression and every NFA
can be converted to a DFA accepting the
same language
Example DFA accepting (a|b)*abb
b
b
start a b b
0 1 2 3
a
a
a
Construction of an NFA from a
Regular Expression
(Thomson’s algorithm)
Basis:
For expression , construct the NFA
start
i f
start
i N(s) N(t) f
abb
start a b b
0 1 2 3
NFA for the union of two regular
expressions r=N(s)|N(t)
N(s)
start
i f
N(t)
a
a|b 1 2
start
0 5
b
3 4
NFA for the closure of a regular expression N(s)*
start
i N(s) f
(a|b)* a
2 3
start
0 1 6 7
b
4 5
NFA for (a|b)*abb#
a
2 3
start
a b b #
0 1 6 7 8 9 10 11
b
4 5