Anda di halaman 1dari 52

rogrammlng Language

LnvlronmenLs
rogramm|ng Language Syntax
lts olwoys eosy to lmplemeot wbeo
we koow oboot tbe Jeslqo
9/12/2011 2 All Saeed khan LC213 LL
SynLax
W CompuLer languages as compared wlLh
naLural languages needs Lo be a blL more
preclse
W Why?
9/12/2011 All Saeed khan LC213 LL 3
SynLax
W 8oLh
SynLax (form)
SemanLlcs (meanlng)
W musL noL have any amblgulLles so LhaL lL can
be lnLerpreLed easlly by boLh humans and
compuLers whaL Lhe program do
9/12/2011 All Saeed khan LC213 LL 4
SynLax
W 1o provlde Lhe needed degree of preclslon
language deslgners and lmplemenLers use
formal synLacLlc and semanLlc notat|on
W 1o undersLand Lhe language feaLures ln depLh
lLs lmporLanL Lo undersLand Lhese noLaLlons
flrsL
9/12/2011 All Saeed khan LC213 LL 3
Lxample
W ulglLs are synLacLlc bulldlng blocks for
numbers
W A number ls represenLed by an arblLrary
lengLh (nonempLy) sLrlng of dlglLs beglnnlng
wlLh a nonzero dlglL
9/12/2011 All Saeed khan LC213 LL 6
SynLax SemanLlcs 8elaLlon
W Cne symbol wlLh same synLax can have
mulLlple semanLlcs
W ln our example Lhe dlglLs can be glven any
meanlng le Lhey can represenL maLhs or
Lhey can represenL days of week or lndex
some llsL
W Why lL ls lmporLanL Lo dlsLlngulsh among
synLax and semanLlcs?
9/12/2011 All Saeed khan LC213 LL 7
SynLax SemanLlcs 8elaLlon
W 1he dlsLlncLlon ls crlLlcal because
ulfferenL L's ofLen provlde feaLures wlLh very
slmllar semanLlcs buL very dlfferenL synLax
Compllers and lnLerpreLers can efflclenLly dlscover
Lhe synLacLlc sLrucLure of a program Lhan lLs
semanLlcs
9/12/2011 All Saeed khan LC213 LL 8
SynLax
W SLudylng Lhe synLax usually depends on Lwo
Lasks
Pow we ec|fy the tructura| ru|e of a L
Pow Lhe compller |dent|f|e the tructure of an
lnpuL program
W lrsL Lask relles on requ/or expressions and
context free qrommors
W Second relles on scanners and parsers
9/12/2011 All Saeed khan LC213 LL 9
SynLax SpeclflcaLlon
W ormal speclflcaLlon of synLax requlres a seL of
rules
W 1hese rules are used Lo consLrucL program
from characLers
W 1here are Lhree baslc klnds of formal rules
ConcaLenaLlon
AlLernaLlon
kleene Closure
9/12/2011 All Saeed khan LC213 LL 10
SynLax SpeclflcaLlon
W 1hese rules speclfy Lhe baslc synLax for
repeLlLlve sLrucLurlng one addlLlonal rule ls
used called ecur|on
W Any seL of sLrlngs LhaL can be deflned ln Lerms
of flrsL Lhree rules ls called a equ/or 5et
W 8egular seLs are generaLed by regular
expresslons and recognlzed by scanners
9/12/2011 All Saeed khan LC213 LL 11
SynLax SpeclflcaLlon
W Any seL of sLrlngs LhaL can be deflned by
lncluslon of recurslon ls called %context
free /onquoqe)
W CL are generaLed %) contextfree
qrommors and recognlzed by parsers
9/12/2011 All Saeed khan LC213 LL 12
-oLlon of Languages
W 1he meanlng of word language varles greaLly
dependlng upon wheLher we are Lalklng abouL
formal languages regular or conLexLfree
programmlng languages
W A formal" language ls [usL a seL of sLrlngs
wlLh no accompanylng semanLlcs
9/12/2011 All Saeed khan LC213 LL 13
1okens
W 1he baslc bulldlng blocks of programs
W 1hey lnclude
keywords
ldenLlflers
-umbers
Symbols
9/12/2011 All Saeed khan LC213 LL 14
8egular Lxpresslons
W 1o speclfy Lokens we use Lhe noLaLlon of
regular expresslons
W A regular expresslons ls one of Lhe followlng
A characLer
An empLy sLrlng denoLed by c
ComblnaLlon of Lwo expresslons (concaLenaLlon)
1wo regular expresslons C8ed
Lxpresslons followed by kleene sLar
9/12/2011 All Saeed khan LC213 LL 13
8Ls ln AppllcaLlons
W wlldcard characLers for paLLern maLchlng
Lg &doc knowlng LhaL Lhe asLerlsk ls
lnLerpreLed as a wlldcard
W egu|ar exre|on are [usL an elaboraLe
exLenslon of Lhls capablllLy
9/12/2011 All Saeed khan LC213 LL 16
1ools for 8Ls
W LdlLro
hLLp//wwwregular
expresslonslnfo/edlLpadprohLml
W -L1 class used
SysLem1exL8egularLxpresslons namespace
9/12/2011 All Saeed khan LC213 LL 17
ConLexL ree Crammars
W 8L work well for deflnlng Lokens
W 8L are unable Lo speclfy oesteJ consLrucLs
W ln any programmlng language ablllLy Lo deflne
a consLrucL ln Lerms of lLself ls cruclal
9/12/2011 All Saeed khan LC213 LL 18
ConLexLree Crammars
W Lach rule ln CC ls known as a #9roduction
W Symbols on lefL hand slde of a producLlon are
known as #Ioriob/es or Non1ermino/s
W Symbols LhaL consLlLuLe Lhe derlved sLrlngs
from Lhe grammar are known as #1ermino/s
W 1he nonLermlnal ln Lhe flrsL producLlon of a
grammar ls called a #5tort 5ymbo/
9/12/2011 All Saeed khan LC213 LL 19
uerlvaLlons and arse 1rees
W A CC shows us how Lo generaLe a
synLacLlcally valld sLrlng of Lermlnals
W Choose a producLlon wlLh Lhe sLarL symbol on
Lhe LPS
W 8eplace Lhe nonLermlnals on Lhe 8PS of Lhe
producLlon
9/12/2011 All Saeed khan LC213 LL 20
uerlvaLlons and arse 1rees
W A serles of replacemenL operaLlons LhaL shows
how Lo derlve a sLrlng of Lermlnals from Lhe
sLarL symbol ls called a #erivotion
W Lach sLrlng of symbols along Lhe way ls called
a #5ententio/ orm
W 1he flnal senLenLlal form conslsLlng of only
Lermlnals ls called Lhe yie/d of Lhe derlvaLlon
9/12/2011 All Saeed khan LC213 LL 21
uerlvaLlons and arse 1rees
W We can represenL a derlvaLlon graphlcally as a
'parse Lree'
W 8ooL of Lhls Lree ls Lhe sLarL symbol of Lhe
grammar
W 1he leaves of Lhe Lree are lLs yleld
W Lach lnLernal node LogeLher wlLh lLs chlldren
represenLs Lhe use of a producLlon
9/12/2011 All Saeed khan LC213 LL 22
AmblgulLy
W ln our example Lhe Lree ls noL unlque
W 1he facL LhaL some sLrlngs are Lhe yleld of
more Lhan one parse Lree Lells us LhaL our
grammar ls 'mbiquous
W arsers use dlsamblguaLlng rules Lo avold or
choose Lhe mosL sulLable language
9/12/2011 All Saeed khan LC213 LL 23
AmblgulLy
W ln Ls Lhe mosL crlLlcal grammars are Lhe one
whlch consLlLuLes 'arlLhmeLlc expresslons'
W We have Lo be careful ln deslgnlng Lhese
grammars Lhe producLlons have Lo capLure
Lhe 'assoclaLlvlLy' and 'precedence' of
operaLors
9/12/2011 All Saeed khan LC213 LL 24
lnlLe AuLomaLon (Scannlng)
W lnlLe auLomaLa are compuLlng devlces LhaL
accepL/recognlze regular languages
(represenLed by regular expresslons) and are
used Lo model operaLlons of many sysLems we
flnd ln pracLlce
W lnlLe AuLomaLon ls a sLrucLured approach
usually used Lo bulld scanners
9/12/2011 All Saeed khan LC213 LL 23
CeneraLlng a A
W 1o generaLe a A from a seL of 8es
auLomaLlcally a 'Scanner CeneraLor' Lool ls
used
9/12/2011 All Saeed khan LC213 LL 26
CeneraLlng a A
W ln effecL 8Ls consLlLuLe a declaraLlve L for a
llmlLed problem domaln
W 1he As are broadly classlfled as
ueLermlnlsLlc
-on ueLermlnlsLlc
W 1hls classlflcaLlon ls done on Lhe basls of
naLure of LranslLlons
9/12/2011 All Saeed khan LC213 LL 27
CeneraLlng a A
W Converslon from a 8L Lo a uA ls a Lhree sLep
process
ConverL 8L lnLo a -A
ConverL -A Lo a uA
CpLlmlzaLlon of Lhe uA
9/12/2011 All Saeed khan LC213 LL 28
-onueLermlnlsLlc lnlLe AuLomaLon
W -A ls llke a uA excepL LhaL
1here may be more Lhan one LranslLlon ouL of a
glven sLaLe labeled by a glven lnpuL characLer
1here may be so called 'epsllon LranslLlons' Lhese
are LranslLlons label by empLy sLrlng symbol
9/12/2011 All Saeed khan LC213 LL 29
;ulz 1 ;uesLlon 1
W lnd Lhe shorLesL sLrlng LhaL ls noL ln Lhe
language represenLed by Lhe 8L a*(ab)*b*
2
W lnd a 8L correspondlng Lo Lhe language of all
sLrlngs over Lhe alphabeL ab LhaL conLaln
exacLly Lwo a's
2
9/12/2011 All Saeed khan LC213 LL 30
;ulz 1 ;uesLlon 1
W uescrlbe as slmply as posslble ln Lngllsh Lhe
language correspondlng Lo Lhe 8L 2
a*b(a*ba*b)*a*
W WrlLe Lhe 8L 'a+' ln Lerms of kleene Closure?
1
9/12/2011 All Saeed khan LC213 LL 31
;ulz 1 ;uesLlon 2
W uraw Lhe lnlLe SLaLe Machlne for Lhe
followlng 8L 3
a(a|b)
+
b?a*
9/12/2011 All Saeed khan LC213 LL 32
ConverLlng 8L Lo an -A
W Lach sLep preserves Lhree lnvarlanLs
1here are no LranslLlons lnLo Lhe lnlLlal sLaLe
1here ls a slngle flnal sLaLe
1here are no LranslLlons ouL of Lhe flnal sLaLe
W 1hese lnvarlanLs allow smaller machlnes Lo be
[olned lnLo larger machlnes wlLhouL any
amblgulLy
9/12/2011 All Saeed khan LC213 LL 33
Why noL an -A
W Why Lo we need uA Lo lmplemenL a 8egular
Lxpresslon (8L)?
W WlLh no way Lo #quess" Lhe rlghL LranslLlon Lo
Lake from any glven sLaLe any pracLlcal
lmplemenLaLlon of an -A would need Lo
explore all posslble LranslLlons concurrenLly
or vla backLracklng
W 1o avold Lhese complexlLles we use a uA
9/12/2011 All Saeed khan LC213 LL 34
-A Lo uA
W #set of subsets" consLrucLlon
W 1he key ldea ls for Lhe sLaLe of Lhe uA afLer
readlng a glven lnpuL Lo represenL Lhe seL of
sLaLes LhaL Lhe -A mlghL have reached on Lhe
same lnpuL
W Lxample
9/12/2011 All Saeed khan LC213 LL 33
Lexlcal Lrrors
W An error LhaL occurs when Lhere ls an
unexpecLed characLer ln Lhe lnpuL
W ulfferenL compllers deals wlLh Lhese errors
dlfferenLly
9/12/2011 All Saeed khan LC213 LL 36
ragmas
W 1hese are speclal consLrucLs LhaL provlde
dlrecLlves or hlnLs Lo Lhe compller
W 1hey are also referred Lo as 'slgnlflcanL
commenLs' because ln mosL cases Lhey do
noL effecL Lhe meanlng of Lhe program only
Lhe compllaLlon process
W 1hey are besL handled by Lhe 'parsers'
9/12/2011 All Saeed khan LC213 LL 37
arser
W A porser ls a plece of program LhaL Lakes a
physlcal represenLaLlon of some daLa and
converLs lL lnLo an lnmemory form for Lhe
program as a whole Lo use
9/12/2011 All Saeed khan LC213 LL 38
arslng
W PearL of Lhe compller
W ln charge of Lhe compllaLlon process
W lnpuL SLream of Lokens
W CuLpuL arse 1ree
9/12/2011 All Saeed khan LC213 LL 39
arslng
W CC generaLes a ConLexLree Language
W 1he 'arser' recogn|ze Lhls language
W We can generaLe a parser for any grammar
LhaL runs ln C(n
3
) where n ls Lhe lengLh of Lhe
lnpuL program
W Cublcal Llme ls noL accepLable ln mosL cases
We need llnear Llme algorlLhms
W Lxample Lo round up parslng and compllaLlon
9/12/2011 All Saeed khan LC213 LL 40
AsslgnmenL # 1
W WrlLe analysls reporL on Larley's AlgorlLhm
and Cocke?oungerkasaml (C?k) AlgorlLhm?
ueadllne 14
Lh
CcLober 2011
9/12/2011 All Saeed khan LC213 LL 41
ar|ng
'usablllLy ulscusslon'
Lxample Web8rowser
9/12/2011 All Saeed khan LC213 LL 42
racLlcallLy of arslng
W arslng ls used ln every sofLware LhaL we use
W 1he mosL wldely and commonly used sofLware
ls a webbrowser"
W LeLs see how lL works and whaL parL parslng
has Lo play ln lL?
9/12/2011 All Saeed khan LC213 LL 43
uncLlonallLy
W 1he browser maln funcLlonallLy ls Lo presenL
Lhe web resource you choose by requesLlng lL
from Lhe server and dlsplaylng lL on Lhe
browser wlndow
W 1he resource formaL ls usually P1ML buL also
u lmage and more
9/12/2011 All Saeed khan LC213 LL 44
Plgh Level SLrucLure
9/12/2011 All Saeed khan LC213 LL 43
8enderlng Lnglne
W ulsplaylng Lhe requesLed conLenLs on browser
screen
W 8y defaulL Lhe renderlng englne can dlsplay
P1ML and xML documenLs and lmages
W lL can dlsplay oLher Lypes Lhrough a |ug|n (a
browser exLenslon)
An example ls dlsplaylng u uslng a u vlewer
plugln
9/12/2011 All Saeed khan LC213 LL 46
8enderlng Lnglnes
W lrefox Cecko
W Safarl WebklL (Cpen Source)
W Chrome WebklL
9/12/2011 All Saeed khan LC213 LL 47
Cecko low
9/12/2011 All Saeed khan LC213 LL 48
arslng
W arslng can be separaLed lnLo Lwo sub
processes
lexlcal analysls and
synLax analysls
9/12/2011 All Saeed khan LC213 LL 49
Lexlcal Analysls
W Lexlcal analysls ls Lhe process of breaklng Lhe
lnpuL lnLo Lokens
W 1okens are Lhe language vocabulary Lhe
collecLlon of valld bulldlng blocks
W ln human language lL wlll conslsL of all Lhe
words LhaL appear ln Lhe dlcLlonary for LhaL
language
9/12/2011 All Saeed khan LC213 LL 30
SynLax Analysls
W SynLax analysls ls Lhe appllcaLlon of Lhe
language synLax rules
W ComponenLs of a parser
Lexer (1okenlzer)
arser (1ree CeneraLor)
9/12/2011 All Saeed khan LC213 LL 31
1hank you
W or more deLalls on parslng
W hLLp//Lallgarslelcom/ro[ecLs/howbrowsersw
ork1hLm#lnLroducLlon
9/12/2011 All Saeed khan LC213 LL 32

Anda mungkin juga menyukai