Semnti nlysis SI 16 Priniples of Progrmming Lnguges Fulty of omputer Siene Dlhousie University Winter 2012 Reding: hpter 4
Motivtion Soure progrm (hrter strem) Snner (lexil nlysis) Front end Prse tree Prser (syntti nlysis) Token strem k end Modified intermedite form Modified trget lnguge Semnti nlysis nd ode genertion Mhine-independent ode improvement Trget ode genertion Mhine-speifi ode improvement strt syntx tree or other intermedite form Trget lnguge (e.g., ssemly) Symol tle Semnti nlysis
Syntx nd Semntis Syntx Desries form of vlid progrm n e desried y ontext-free grmmr Semntis Desries mening of progrm nnot e e desried y ontext-free grmmr Some onstrints tht my pper syntti re enfored y semnti nlysis: E.g., use of identifier only fter its delrtion Semnti nlysis
Semnti nlysis Enfores semnti rules uilds intermedite representtion (e.g., strt syntx tree) Fills symol tle Psses results to intermedite ode genertor Two pprohes Interleved with syntti nlysis s seprte phse Forml mehnism: ttriutes grmmrs Semnti nlysis
Enforing Semnti Rules Stti semnti rules Enfored y ompiler t ompile time Exmple: Do not use undelred vrile Dynmi semnti rules ompiler genertes ode for enforement t run time Exmples: division y zero, rry index out of ounds Some ompilers llow these heks to e disled Semnti nlysis
ttriute Grmmrs n ttriute grmmr is n ugmented ontext-free grmmr: Eh symol in prodution hs numer of ttriutes. Eh prodution is ugmented with semnti rules tht opy ttriute vlues etween symols, Evlute ttriute vlues using semnti funtions, Enfore onstrints on ttriute vlues. Semnti nlysis
ttriute Grmmrs: Exmple E E + T E E - T E T T T * F T T / F T F F -F F ( E ) F onst Semnti nlysis
ttriute Grmmrs: Exmple E E + T E E - T E T T T * F T T / F T F F -F F ( E ) E 1 E 2 + T E 1 E 2 - T E T T 1 T 2 * F T 1 T 2 / F T F F 1 -F 2 F ( E ) F onst F onst E 1.vl := sum(e 2.vl, T.vl) E 1.vl := differene(e 2.vl, T.vl) E.vl := T.vl T 1.vl := produt(t 2.vl, F.vl) T 1.vl := quotient(t 2.vl, F.vl) T.vl := F.vl F 1.vl := negte(f 2.vl) F.vl := E.vl F.vl := onst.vl Semnti nlysis
Synthesized ttriutes (ottom-up) The lnguge { n n n n 1} = {,,,... } is not ontext-free ut n e reognized y n ttriute grmmr. Synthesized ttriutes: ttriutes of LHS re omputed from ttriutes of RHS. S ondition:.ount =.ount =.ount.ount := 1 1 2 1.ount := 2.ount + 1.ount := 1 1 2 1.ount := 2.ount + 1.ount := 1 1 2 1.ount := 2.ount + 1 Semnti nlysis
Synthesized ttriutes: Prse Tree Deortion (1) Input: Prse tree S Semnti nlysis
Synthesized ttriutes: Prse Tree Deortion (1) Input: Prse tree S = = 2 2 2 1 1 1 Semnti nlysis
Synthesized ttriutes: Prse Tree Deortion (2) Input: Prse tree S Semnti nlysis
Synthesized ttriutes: Prse Tree Deortion (2) Input: Prse tree S 2 2 2 1 2 1 1 Semnti nlysis
Inherited ttriutes (Top-Down) Inherited ttriutes: ttriutes flow from left to right (from LHS to RHS nd from symols of RHS to symols of RHS further to the right). S.inhount :=.ount;.inhount :=.ount.ount := 1 1 2 1.ount := 2.ount + 1 ondition:.inhount = 1 1 2 2.inhount := 1.inhount 1 ondition:.inhount := 1 1 2 2.inhount := 1.inhount 1 Semnti nlysis
Inherited ttriutes: Prse Tree Deortion (1) Input: Prse tree S Semnti nlysis
Inherited ttriutes: Prse Tree Deortion (1) Input: Prse tree S 2 2 2 1 1 = 1 1 = 1 Semnti nlysis
Inherited ttriutes: Prse Tree Deortion (2) Input: Prse tree S Semnti nlysis
Inherited ttriutes: Prse Tree Deortion (2) Input: Prse tree S 2 2 1 2 1 1 = 1 Semnti nlysis
ttriute Flow nnottion or deortion of prse tree Proess of evluting ttriutes Synthesized ttriutes ttriutes of LHS of prodution re omputed from ttriutes of the symols in its RHS. ttriutes flow ottom-up in the prse tree. Inherited ttriutes ttriutes for symols in RHS re omputed from ttriutes of LHS nd symols in RHS preeding them. ttriutes flow top-down in the prse tree. Semnti nlysis
S-ttriuted nd L-ttriuted Grmmrs S-ttriuted grmmr ll ttriutes re synthesized. ttriutes flow ottom-up. L-ttriuted grmmr For eh prodution X Y 1 Y 2... Y k, X.syn depends on X.inh nd Y 1.inh, Y 1.syn, Y 2.inh, Y 2.syn,..., Y k.inh, Y k.syn. For ll 1 i k, Y i.inh depends on X.inh nd Y 1.inh, Y 1.syn, Y 2.inh, Y 2.syn,..., Y i 1.inh, Y i 1.syn. S-ttriuted grmmrs re speil se of L-ttriuted grmmrs. Semnti nlysis
LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs simple grmmr for rithmeti expressions using only ddition nd sutrtion 10 + 5 = (10 ) + 5 = 12 10 + 5 10 ( + 5) = 2 Semnti nlysis
LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs simple grmmr for rithmeti expressions using only ddition nd sutrtion E T E ET 10 + 5 = (10 ) + 5 = 12 10 + 5 10 ( + 5) = 2 T n + - E E T E T + 5 T - 10 Semnti nlysis
LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs simple grmmr for rithmeti expressions using only ddition nd sutrtion E T E ET 10 + 5 = (10 ) + 5 = 12 10 + 5 10 ( + 5) = 2 T n + - E E T E T + 5 T - 10 This grmmr ptures left-ssoitivity. Semnti nlysis
LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs simple grmmr for rithmeti expressions using only ddition nd sutrtion E T E ET 10 + 5 = (10 ) + 5 = 12 10 + 5 10 ( + 5) = 2 T n + - E 7 E 12 dd T 5 E 10 su T + 5 5 T 10-10 10 This grmmr ptures left-ssoitivity. Semnti nlysis
LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs simple grmmr for rithmeti expressions using only ddition nd sutrtion E T E ET 10 + 5 = (10 ) + 5 = 12 10 + 5 10 ( + 5) = 2 T n + - E 7 E 12 dd T 5 Rule R PREDIT(R) E T 10 10 10 10 - su T + 5 5 E T E ET T n + - {n} {n} {n} {+} {-} This grmmr ptures left-ssoitivity. It is not LL(1). Semnti nlysis
LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs n LL(1) grmmr for the sme lnguge E T E $ E ɛ E 1 T E 2 T n + - Rule R E T E $ {n} E 2.op :=.fun(e E 1.op, T.vl); ɛ E 1.vl {$} := E 2.vl E T E {+,-} T n + - PREDIT(R) {n} {+} {-} Semnti nlysis
LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs n LL(1) grmmr for the sme lnguge E T E $ E ɛ E 1 T E 2 T n + - Rule R E T E $ {n} E 2.op :=.fun(e E 1.op, T.vl); ɛ E 1.vl {$} := E 2.vl E T E {+,-} T n + - PREDIT(R) {n} {+} {-} E T E $ 10 T E - T E + 5 ɛ Semnti nlysis
LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs n LL(1) grmmr for the sme lnguge E T E $ E ɛ E 1 T E 2 T n + - E.op := T.vl; E.vl := E.vl E.vl := E.op E 2.op :=.fun(e 1.op, T.vl); E 1.vl := E 2.vl T.vl := n.vl.fun := dd.fun := su E 12 10 10 12 T E $ 10 10 su T 7 E 12 - dd T 5 12 E 12 + 5 5 ɛ Semnti nlysis
tion Routines tion routines re instrutions for d-ho trnsltion interleved with prsing. Prser genertors llow progrmmers to speify tion routines in the grmmr. tion routines n pper nywhere in rule (s long s the grmmr is LL(1)). Exmple E 1 T { E 2.op =.fun(e 1.op, T.vl) } E 2 { E 1.vl = E 2.vl } tion routines re supported, for exmple, in y nd ison. Semnti nlysis