Lecture #7 Began Here

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

lecture #7 began here

Announcements

Nex thomewor kIpromi se:Iwi llas ktheTAt oruny ourprogram wi thanone xi


stentfi
l
ea sa
comma nd- l
ineargume nt!
Off
icehour s:M 1:30-2:30,T:4-5a ndbya ppointme nt
TAOf ficehours:F2:30-3:30a ndbya ppointme nt
Whi chofMi g
uelAr cinieg
a ,Mi c haelHa rr
is
,J ohnHe rrer
a,Pa dmaMa ll
apragada,Ruben
Paloma res, andMa rkWa tnea reher e?I fyoua r
ehe re,raiseyourha nda ndsayhello.
HW# 1ra wscores(outof1 00):
1009 89 5959 49 4919 190( approxima te"A"l i
st)
87848276 (a pproxima t
e" B"-" C"range)
30105 (pot enti
a l
lyfai
li
ng ;getmor ehelp)
Que sti
onsonHW#2, e.g.
I
nhome wor k2wea regivent wof i
leshw2. l andtok en.h.Intoken.ht herearefewt okenwhich
arenotpr esentinhw2. lf
ore xampl eE LS ESTOPATL ENCOMMAS E
MI COL ONORANDUNARY.
Tha nky ouf orre port
ingt hi
s,Iwi lla ddr egularexpr e
s s
ionsfort hes etoke
ns .I
ng ene r
alyou
shoulde i
the rask,orel see xpec ty ouwi llne edt of il
lina nymi s
s i
ngpiecesint hepa rt
ial
sol
ut i
onsI handouta sstarti
ngpoi ntsf ora ssi
gnme nts.

Flex ManpageExamplefest (contd)

• 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(^"*/")

lecture #7 began here


Finite Automata

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)
;
}
}

lecture #8 began here


Deterministic Finite Automata Examples

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:

Nondeterministic Finite Automata (NFA's)

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

ε-transitions make it simpler to merge automata:


multiple transitions on the same symbol handle common prefixes:

factoring may optimize the number of states. Is this picture OK/correct?

Regular expression examples

Can you draw an NFA corresponding to the following?

(
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.

1. For ε, draw two states with a single ε transition.

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.

NFA's can be converted automatically to DFA's

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.

Operations to keep track of sets of NFA states:

ε_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

You might also like