Professional Documents
Culture Documents
Lecture #7 Began Here
Lecture #7 Began Here
Lecture #7 Began Here
Announcements
• L
ineCounter
/WordCounte
r
• i
ntnum_l
ines=0,
num_ cha
rs=0;
•
• %%
• \n +
+ num_
li
nes;+
+num_
cha
rs;
• . ++num_c
hars
;
•
• %%
• mai
n()
• {
• yyl
ex(
);
• pri
ntf
("#ofli
nes=%d, #ofchars=%d\
n",
• num_li
nes,num_chars);
• }
• Toycompil
erexample
• /*s
cannerforatoyPa
s c
al-l
i
kel
angua
ge*
/
•
• %{
• /*ne
edthi
sfort
heca
llt
oat
of(
)bel
ow*/
• #i
ncl
ude<math.
h>
• %}
•
• DI
GIT [
0-9]
• I
D [ a-
z][a
-z0-
9]*
•
• %%
•
• {
DIGI
T}+{
• pr
intf
("Anint
eger
:%s(
%d)
\n"
,yy
tex
t,
• at
oi(yyt
ext))
;
• }
•
• {
DIGI
T}+".
"{
DIGI
T}* {
• pr
intf
("Afl
oat:%s(%g
)\n"
,yy
tex
t,
• at
of(yyt
ext));
• }
•
• i
f|t
hen|be
gin|end|pr
ocedur
e|f
unct
ion {
• pr
int
f("
Ak eyword:
%s \
n",yy
tex
t);
• }
•
• {
ID} pr
int
f("
Ani
dent
if
ier
:%s
\n"
,yy
tex
t);
•
• "
+"|
"-"
|"*
"|"
/" pr
int
f("
Anope
rat
or:
%s\
n",
yyt
ext)
;
•
• "
{"[
^}\
n]*
"}" /
*ea
tupone
-li
nec
omme
nts*/
•
• [\
t\n]
+ /
*ea
tupwhi
tes
pac
e*/
•
• . pr
int
f("
Unr
ecog
niz
edc
har
act
er:
%s\
n",
yyt
ext)
;
•
• %%
•
• ma i
n(ar
gc,argv)
• i
ntar
gc;
• char**ar
gv;
• {
• ++ar
gv,-
-arg
c ;/*ski
pov
erpr
ogr
amna
me*
/
• i
f(argc
>0)
• y
yin=fopen(ar
gv[0]
,"r");
• el
se
• y
yin=stdi
n;
•
• y
yle
x()
;
• }
On the use of character sets (square brackets) in lex and similar tools
As t
udentrecentl
ysentmea nexampler e
g ul
arexpres
sionf orcomme ntst
hatr
ead:
COMMENT[ /*][[
^*/]*[*]
*]]*[
*/]
Onepr obl
em he r
eist hatsquarebracketsarenotpa r
e ntheses,theydonotnest,t
heydonot
supportconcate
na t
ionorot herregula
re xpres
si
onoper ator
s.T heymeanexact
ly:"mat
chany
oneoft he
secha r
acters"orfor^:"
ma tcha nyonecharacterthatisnotoneoft
hesechara
cte
rs".
Notealsothatyoucan't use^a sa" not"operatoroutsideofs quarebra
cket
s:youcan'
twrit
e
theexpress
ionfor"stufft
hatisn'
t*/"bys ayi
ng(^"*/")
Af
init
eautomaton(F
A)isanabs
tra
ct,
ma thema
tic
alma
chi
ne,
als
oknowna
saf
ini
tes
tat
e
ma
c hi
ne,wit
hthefol
lowi
ngcomponents
:
1.As
etofstate
sS
2.As
etofinputsymbol
sE(
theal
phabet
)
3.At
ransi
ti
onfuncti
onmov
e(st
ate
,symbol
):ne
wst
ate
(s)
4.As
tarts
tateS0
5.As
etoffinal
sta
tesF
Thewordfinite r
eferstothesetofstat
es:ther
eisaf i
xedsizetothismachi
ne.No" s
tac
ks",
no
"vi
rt
ualmemor y",justak nown numberofs t
ate
s .The wor d automaton ref
erstothe
exec
uti
onmode :thereisnoinst
ruct
ionset,the
reisnos equenceofinstr
uct
ions
,therei
sjus
ta
har
dwire
ds hortloopt hatex
ecutest
hes ameinst
ruct
ionov erandov er
:
whi
l
e((
c=g
etc
har
()
)!=E
OF)S:
=mov
e(S
,c)
;
DF
As
T
hetypeoffi
ni
teautomatat
hati
seasi
estt
ounders
tandandsi
mpl es
ttoimpl
eme nt(say,e ven
i
nha r
dware
)iscal
l
edadet er
mini
sti
cfi
nit
eautomaton(DFA)
.Thewor ddeterministic here
r
efe
rstotheret
urnv a
lueoff
unct
ionmove(s
tat
e,symbol)
,whic
hg oestoatmos tones ta
t e
.
E
xample:
S={s
0,s1,
s2}
E={a,
b,c}
move={(s
0,a
):s
1;(
s1,
b):
s2;
(s2,
c)
:s
2}
S0=s0
F={s
2}
Fi
niteautomat
ac or
respondina1: 1re
lat
ionshi
pt otra
ns i
ti
ondi
agr
ams;f
rom anytr
ansi
ti
on
dia
gram onecanwritedownt heformala
ut omatonintermsofi
tems#1-
#5above,andvi
ce
vers
a.Todrawthetr
a nsi
ti
ondi
a g
ramforafini
teautomaton:
• dra
wac i
rcleforeachstat
esinS; putal abeli
nsidetheci
rc
lest
oide
ntif
yeachs
tat
eby
numberorname
• dra
wana rrowbe tweenSiandS,
jla beledwithxwhe nev
erthetr
ans
it
ionsa
ysto
move(
Si,
x): S
j
• dra
wa" we dgi
e"intothest
a r
tst
at eS 0toidenti
fyit
• dra
was econdc i
rcl
einsi
deeachoft hefi
nalsta
te si
nF
T
heAut
oma
tonGa
me
I
fIgi
veyouatr
ansi
ti
ondi
agra
mofaf i
ni
tea
utoma
ton,
youc
anha
nd-
si
mul
atet
heope
rat
ionof
t
hataut
omatononanyi
nputIgi
vey
ou.
DFA Implementation
Thenicepar
taboutDFA'si
sthattheyar
ee f
fi
ci
ent
lyimplementedoncompute
rs.Wha tDF
A
doesthef
oll
owi
ngcodecorr
espondto?Whati
sthecorres
pondingre
gul
arex
pres
sion?Youcan
speedthi
scodefrag
me ntupev enfur
therifyou a
rewi l
li
ngt ousegot
o'sorwr i
teitin
ass
embler
.
s
tat
e: =S0
f
or(
;;
)
s
wit
ch( st
ate){
c
ase0:
s
wit
ch( i
nput){
'a':
stat
e=1; input=getchar(
);break;
'b':i
nput=ge t
char(
);break;
defaul
t:pri
ntf
("df
aer r
or\n")
; e
xit
(1)
;
}
c
ase1:
s
wit
ch( i
nput){
EOF :pr
intf
("acce
pt\n")
;exit
(0);
defaul
t:pri
ntf
("df
aer r
or\n")
; e
xit
(1)
;
}
}
Al
exi
cal
ana
lyz
ermi
ghta
ssoc
iat
edi
ff
ere
ntf
ina
lst
ate
swi
thdi
ff
ere
ntt
oke
nca
teg
ori
es:
C Comments:
Notat
ionalc
onve
nienc
emot ivat
esmor efl
exi
bl
ema chi
nesinwhi
chfunc
ti
onmove(
)cangoto
morethanonestat
eonag i
veninputsymbol
,andsomes t
ate
scanmovetoot
herst
atese
ven
wit
houtconsumi
nganinputsymbol(ε-
tra
nsi
ti
ons)
.
For
tuna t
ely
,onecanpr ovethatf
ora nyNF A,therei
sa nequiv
alentDFA.Theyarejus
ta
nota
tionalconv
eni
enc
e .So,f
ini
tea
utoma t
ahe lpusgetf
rom asetofregul
are
x pr
ess
ionst
oa
computerprogr
amthatrecog
niz
esthe
me ffi
ci
e ntl
y.
NFA Examples
(
a|c
)*b(
a|c
)*
(
a|c
)*|
(a|
c)*
b(a
|c)
*
(
a|c
)*(
b|ε
)(a
|c)
*
Regular expressions can be converted automatically to NFA's
Ea
chrul
einthedefi
nit
ionofreg
ula
re x
press
ionshasac or
respondi
ngNFA;
NF A'
sar
ecomposed
us
ingεtr
ans
iti
ons.Thi
sisci
tedinthetex
tas" Thompson'
scons t
ruc
ti
on"(
Loudenpg.64)
.We
wi
llwor
kexa
mpl e
ss uc
ha s(
a|b)
*abbincl
assandduringlab.
2. For any letter in the alphabet, draw two states with a single transition labeled with
that letter.
3. For regular expressions r and s, draw r | s by adding a new start state with ε
transitions to the start states of r and s, and a new final state with ε transitions from
each final state in r and s.
4. For regular expressions r and s, draw rs by adding ε transitions from the final states of
r to the start state of s.
5. For regular expression r, draw r* by adding new start and final states, and ε transitions
o f romt hestartstat et othef i
na ls
tate,
o f romt hefi
na lstat eba c
kt ot hestarts t
ate,
o f romt henews ta rttotheol ds ta
rta ndfromt heol df i
na lstatest othenewf i
na l
state.
6. For parenthesized regular expression (r) you can use the NFA for r.
In: NFA N
Out: DFA D
Method: Construct transition table Dtran (a.k.a. the "move function"). Each DFA state is a set
of NFA states. Dtran simulates in parallel all possible moves N can make on a given string.
ε_closure(s)
set of states reachable from state s via ε
ε_closure(T)
set of states reachable from any state in set T via ε
move(T,a)
set of states to which there is an NFA transition from states in T on symbol a