Lecture #5 Began Here: Avoid These Top 10 Homework #1 Bugs in Your Homework #2
Lecture #5 Began Here: Avoid These Top 10 Homework #1 Bugs in Your Homework #2
We are doing (a large subset of) TRS-80 Color Computer Extended BASIC. Compared with last
semester, you will get more help from me on getting started with each assignment, and be
asked to do a little more.
Thema inl a ng uager e
fe re
nc emanualsareonourc l
as swe bpage
http:
// www. c s .
nms u.
edu/~je f
fery/
cours
es/370/
Ia mwr itingmyownc omme ntar
yoni t
http:
// www. c s .
nms u.
edu/~je f
fery/
cours
es/370/basi
c.
html wheremos tofy ourformal
speci
ficationswi llappe ar.
Iha vema deas t
artatsome
pseudoc ode ht tp: //www. cs.nms u.
edu/~j
effer
y/cours
es/37 0/basi
c.
icnforthe" inter
pret
e rloop"
thatwi l
lwr apa roundy ourc ompile
r.
I
napr evi
ouslecturewes awreg
ularex
press
ions,t
hepref
err
ednotati
onforspe
c i
fyi
ngpat
terns
ofc
ha ra
ctersthatdef
inetokencat
egori
es.Thebestwa
yt ogetafe
elforreg
ularexpre
ssi
onsis
toseee xampl
e s
.Notet hatr
egula
re x
press
ionsformthebasi
sforpatt
ernma tc
hinginmany
UNI
Xt oolssuc
ha sgrep,
a wk,
perl
,etc
.
Wea r
edoingasubse
tofBASICinthiscour
se,s
owhati
sthereg
ula
rexpre
ssi
onforeachoft
he
di
ff
erentle
xic
ali
temsthatappearinBAS I
Cprogr
ams
?T hi
sist
hefir
stofmanything
swhere
BASI
Cissomewhateas
iert
ode a
lwiththanC.
lexical
BASIC notes, and comparison with C
category
Foroperat
orstha tareregularexpressi
onoper at
or swenee d
ma r
kthemwi thdoubl equot esorbacks l
ashest
oi ndica
te
t
hecha
ract
ers youme anthecha racter,
nott heregularexpres
sion
ope
rat
ors
t
hemse
lves operat
or.Notes everaloperatorshaveac ommonpr efi
x.The
l
exical
analyze
rne e
dst olooka headtot el
lwhethe ran=is
anassi
gnme nt,orisfoll
owe dbya nother=f orexample.
t
heconcat
enat
ionof
r
eserv
ed Reser
vedwordsar
eal
somat
chedbyther
egula
rexpr
ess
ion
c
harac
ters
;ca
se
words fori
dent
if
ier
s,s
oadis
ambi
guat
ingr
ulei
sneeded.
i
nse
nsit
iv
e
no_ ;
$a tendsof
some;2s i
gni
fic
ant
i
dent
if
ier
s [
a-z
A-Z
_][
a-z
A-Z
0-9]
*
l
e t
ter
s!?;cas
e
i
nsensit
ive
i
ntsandreal
s,
numbe
rs 0x
[0-
9a-
fA-
F]+e
tc.
s
tart
ingwit
h[0-9]
+
c
omme
nts RE
M.* C'
scomme
ntsa
ret
ri
ckyr
egex
p's
al
most".
*";
no
s
tri
ngs es
cape
dquot
es
es
capes
wha
tel
se?
F
ILE*yyi
n; /
*setthi
svari
ablepri
ortoc al
l
ingyy
lex
()*/
i
ntyy
lex
(); /
*cal
lthisf
uncti
ononc eforeacht
oken*/
c
haryyt
ext[
]; /
*yyl
ex (
)wri
testhetoken'sl
exemetoana rr
ay*/
/
*note:wit
hflex
, I
be l
i
evee xt
erndecl
ara
tionsmustr
ead
e
xter
ncha
r*y
ytex
t;*/
T
he.lf
il
eformatc
onsi
st
sofamixt
ureofl
exsy
nta
xa ndCcodefr
agment
s.T
hepe r
cents
ign(
%)
i
sus
edtos i
gni
fyl
exe
lement
s.Thewhol
efi
lei
sdi
videdi
ntothr
eesec
ti
onssepa
rat
edby%%:
hea
der
%%
body
%%
hel
perf
unc
ti
ons
T
hehea de
rconsi
st
sofCcodefra
gmentsencl
osedin%{and%}a swella
sma c
rode f
ini
ti
ons
c
ons
ist
ingofanameandaregul
arexpre
ssi
ondenotedbythatname.l
exmacr
osa r
einvoked
e
xpl
i
cit
lybyencl
osi
ngt
hemacronameincur
lybrac
es.F
oll
owingar
esomeexampl
elexma c
ros.
l
ett
er [
a -
zA-
Z]
di
gi
t [
0-9]
i
dent {
lett
er}
({
let
ter
}|{
dig
it}
)*
Thebodyc onsist
sofofas e
que nceofr egularexpr
essi
onsf ordiffer
enttokenc at
egor
iesa nd
otherlexi
calenti
ties
.Eachreg ul
arexpr e
s s
ionca nhaveaCc odefragmentenc l
osedinc url
y
bracesthate xecut
eswhe nt ha tregulare xpr
essi
on i
sma t
ched.F ormos toft heregular
expres
s i
onsthi
sc odefrag
me nt(als
oc all
edasemantic action consi
stsofret
urninganinteger
thati
dentif
iesthetokencategorytother estofthecompil
e r,parti
cul
arl
yforusebyt hepa rs
er
tochecksyntax
. S
omet ypi
calregula
rex pres
s i
onsandsemant i
ca cti
onsmighti
nclude:
"" {/*no-op,disc
ardwhi
tespa
ce*/}
{i
dent} {ret
urnIDENT I
FIE
R;}
"*" {ret
urnAST ERI
SK;}
".
" {ret
urnPERIOD; }
Youa l
soneedr
egul
arexpres
sionsforl
exi
caler
ror
ssuc
hasunt
ermi
nat
edc
har
act
erc
ons
tant
s,
oril
l
egalcha
rac
ter
s.
Thehel
perf
uncti
onsi
nalexfi
let
ypic
all
ycomput
ele
xic
ala
ttr
ibut
es,s
ucha
sthea
ctua
lint
ege
r
orst
ri
ngval
uesdenot
edbyl
it
eral
s.
L
exfur
therext
endsther
egul
are
xpr
ess
ionswi
ths
eve
ralhel
pfulope
rat
ors
.Le
x'sr
egul
ar
e
xpr
ess
ionsi
ncl
ude:
c
nor
mal
cha
rac
ter
sme
ant
hems
elv
es
\
c
back
sla
shesc
apesr
emovethemeani
ngfrommostoper
atorc
har
act
ers
.Ins
idec
har
act
er
set
sandquotes
,ba
cks
las
hperf
ormsC-s
tylee
sca
pes.
"
s"
Doubl
equot
esme a
ntomat
chtheCs
tri
nggi
vena
sit
sel
f.T
hisi
spar
ti
cul
arl
yusef
ulf
or
mult
i-
byt
eoper
atorsa
ndmaybemor
ereada
blet
hanus
ingbac
ksl
ashmult
ipl
eti
mes.
[
s]
T
hisc
har
act
ers
etope
rat
orma
tche
sanyonec
har
act
era
mongt
hos
eins
.
[
^s]
Aneg
ate
d-s
etma
tche
sanyonec
har
act
ernota
mongt
hos
eins
.
.
T
hedotope
rat
orma
tche
sanyonec
har
act
ere
xce
ptne
wli
ne:
[^\
n]
r
*
ma
tchr0ormor
eti
mes
.
r
+
ma
tchr1ormor
eti
mes
.
r
?
ma t
chr0or1t ime.
r
{m,n}
ma t
chrbe twe e
nma ndnt ime s.
r
1r2
concatenat
ion.ma t
c hr
1followedbyr 2
r|r
1 2
al
ternati
on.ma tchr1orr2
(
r)
parenthese
ss peci
fyprecede ncebutdonotma tc
ha ny
t hi
ng
r/r
1 2
l
ook ahead.ma t
chr1wh enr 2fol
lows,wit
houtconsumingr2
^
r
ma t
chronl ywhe nitocc
ur sa tt
hebeg i
nni
ngofaline
r
$
ma t
chronl ywhe nitocc
ur sa tt
heendofal i
ne
Flex ManpageExamplefest
ToreadaUNI X"manpage",ormanualpa
ge ,
y outype"mancommand"whe r
ecomma ndisthe
UNIXprogr
am orli
brar
yfuncti
onyouneedinformati
onon. Readthema npageformantolearn
moreadvanceduses(
"manma n")
.
I
tturnsoutthefl
exma npageisi
ntendedtobepr ettycomplet
e,e noughsothatwecandra w
ourexamplesfr
om it
.Perhapswhatyoushoul df
igureoutfrom theseexamplesi
sthatfl
exis
act
uall
y.
..f
lexi
bl
e.Thefi
rsts
e v
era
lexamplesus ef
lexasafi
lte
rfrom standa
rdinputt
os t
andard
out
put.
• s
neak
ys t
ri
ngremovaltool:
• %%
• "z
apme "
• e
xce
sswhites
pacetr
immer
• %%
• [\t
]+ put char('');
• [\t
]+$ / *ignorethi
stoke
n* /
• s
neak
ys t
ri
ngsubst
it
utiontool:
• %%
• user
name printf("
%s "
,get
log
in(
));