SEMNTI NLYSIS PRINIPLES OF PROGRMMING LNGUGES Norbert Zeh Winter 2018 Dlhousie University 1/28
PROGRM TRNSLTION FLOW HRT Soure progrm (hrter strem) Snner (lexil nlysis) Front end Prse tree Prser (syntti nlysis) Token strem Bk end Modified intermedite form Modified trget lnguge Semnti nlysis nd ode genertion Mhine-independent ode improvement Trget ode genertion Mhine-speifi ode improvement bstrt syntx tree or other intermedite form Trget lnguge (e.g., ssembly) Symbol tble 2/28
ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 3/28
ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 3/28
SYNTX ND SEMNTIS Syntx Desribes form of vlid progrm n be desribed by ontext-free grmmr 4/28
SYNTX ND SEMNTIS Syntx Desribes form of vlid progrm n be desribed by ontext-free grmmr Semntis Desribes mening of progrm nnot be desribed by ontext-free grmmr 4/28
SYNTX ND SEMNTIS Syntx Desribes form of vlid progrm n be desribed by ontext-free grmmr Semntis Desribes mening of progrm nnot be desribed by ontext-free grmmr Some onstrints tht my pper syntti re enfored by semnti nlysis! Exmple: Use of identifier only fter its delrtion 4/28
SEMNTI NLYSIS Semnti nlysis Enfores semnti rules Builds intermedite representtion (e.g., bstrt syntx tree) Fills symbol tble Psses results to intermedite ode genertor 5/28
SEMNTI NLYSIS Semnti nlysis Enfores semnti rules Builds intermedite representtion (e.g., bstrt syntx tree) Fills symbol tble Psses results to intermedite ode genertor Two pprohes Interleved with syntti nlysis s seprte phse 5/28
SEMNTI NLYSIS Semnti nlysis Enfores semnti rules Builds intermedite representtion (e.g., bstrt syntx tree) Fills symbol tble Psses results to intermedite ode genertor Two pprohes Interleved with syntti nlysis s seprte phse Forml mehnism ttribute grmmrs 5/28
ENFORING SEMNTI RULES Stti semnti rules Enfored by ompiler t ompile time Exmple: Do not use undelred vrible. 6/28
ENFORING SEMNTI RULES Stti semnti rules Enfored by ompiler t ompile time Exmple: Do not use undelred vrible. Dynmi semnti rules ompiler genertes ode for enforement t runtime. Exmples: Division by zero, rry index out of bounds Some ompilers llow these heks to be disbled. 6/28
ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 7/28
ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 7/28
TTRIBUTE GRMMRS ttribute grmmr n ugmented ontext-free grmmr: Eh symbol in prodution hs number of ttributes. Eh prodution is ugmented with semnti rules tht opy ttribute vlues between symbols, Evlute ttribute vlues using semnti funtions, Enfore onstrints on ttribute vlues. 8/28
TTRIBUTE GRMMR: EXMPLE E E + T E E T E T T T F T T / F T F F F F ( E ) F onst 9/28
TTRIBUTE GRMMR: EXMPLE E E + T E E T E T T T F T T / F T F F F F ( E ) F onst E 1 E 2 + T { E 1.vl = dd(e 2.vl, T.vl) } E 1 E 2 T { E 1.vl = sub(e 2.vl, T.vl) } E T { E.vl = T.vl } T 1 T 2 F { T 1.vl = mul(t 2.vl, F.vl) } T 1 T 2 / F { T 1.vl = div(t 2.vl, F.vl) } T F { T.vl = F.vl } F 1 F 2 { F 1.vl = neg(f 2.vl) } F ( E ) { F.vl = E.vl } F onst { F.vl = onst.vl } 9/28
SYNTHESIZED ND INHERITED TTRIBUTES Synthesized ttributes ttributes of LHS of prodution re omputed from ttributes of RHS of prodution. 10/28
SYNTHESIZED ND INHERITED TTRIBUTES Synthesized ttributes ttributes of LHS of prodution re omputed from ttributes of RHS of prodution. Inherited ttributes ttributes flow from left to right: From LHS to RHS, From symbols on RHS to symbols lter on the RHS. 10/28
SYNTHESIZED TTRIBUTES: EXMPLE The lnguge L = { n b n n n > 0} = {b, bb, bbb,...} is not ontext-free but n be defined using n ttribute grmmr: 11/28
SYNTHESIZED TTRIBUTES: EXMPLE The lnguge L = { n b n n n > 0} = {b, bb, bbb,...} is not ontext-free but n be defined using n ttribute grmmr: S B {ondition:.ount = B.ount =.ount} {.ount = 1} 1 2 { 1.ount = 2.ount + 1} B b {B.ount = 1} B 1 B 2 b {B 1.ount = B 2.ount + 1} {.ount = 1} 1 2 { 1.ount = 2.ount + 1} 11/28
SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb 12/28
SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S B B b B b b 12/28
SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S 3 3 B 3 2 2 B b 2 1 1 B b 1 b 12/28
SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: 3 = 3 = 3 S 3 3 B 3 2 2 B b 2 1 1 B b 1 b 12/28
SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb 13/28
SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S B B b b 13/28
SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S 3 2 B 3 2 1 B b 2 1 b 1 13/28
SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: 3 2 3 S 3 2 B 3 2 1 B b 2 1 b 1 13/28
INHERITED TTRIBUTES: EXMPLE gin, we onsider the lnguge L = { n b n n n > 0} = {b, bb, bbb,...}. 14/28
INHERITED TTRIBUTES: EXMPLE gin, we onsider the lnguge L = { n b n n n > 0} = {b, bb, bbb,...}. S B {B.inhount =.ount;.inhount =.ount} {.ount = 1} 1 2 { 1.ount = 2.ount + 1} B b {ondition : B.inhount = 1} B 1 B 2 b {B 2.inhount = B 1.inhount 1} {ondition :.inhount = 1} 1 2 { 2.inhount = 1.inhount 1} 14/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb 15/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S B B b B b b 15/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S 3 B 2 B b 1 B b b 15/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S 3 3 B 3 2 B b 1 B b b 15/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S 3 3 B 3 2 2 B b 2 1 1 B b 1 b 15/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S 3 3 B 3 2 2 B b 2 1 1 = 1 B b 1 = 1 b 15/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb 16/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S B B b b 16/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S 3 B 2 B b 1 b 16/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S 3 3 B 3 2 B b 1 b 16/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S 3 3 B 3 2 2 B b 2 1 b 1 16/28
INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S 3 3 B 3 2 2 1 B b 2 1 b 1 = 1 16/28
TTRIBUTE FLOW nnottion or deortion of the prse tree: Proess of evluting ttributes 17/28
TTRIBUTE FLOW nnottion or deortion of the prse tree: Proess of evluting ttributes Synthesized ttributes: ttributes of LHS of eh prodution re omputed from ttributes of symbols on the RHS of the prodution. ttributes flow bottom-up in the prse tree. 17/28
TTRIBUTE FLOW nnottion or deortion of the prse tree: Proess of evluting ttributes Synthesized ttributes: ttributes of LHS of eh prodution re omputed from ttributes of symbols on the RHS of the prodution. ttributes flow bottom-up in the prse tree. Inherited ttributes: ttributes for symbols in the RHS of eh prodution re omputed from ttributes of symbols to their left in the prodution. ttributes flow top-down in the prse tree. 17/28
S-TTRIBUTED ND L-TTRIBUTED GRMMRS S-ttributed grmmr ll ttributes re synthesized. ttributes flow bottom-up. 18/28
S-TTRIBUTED ND L-TTRIBUTED GRMMRS S-ttributed grmmr ll ttributes re synthesized. ttributes flow bottom-up. L-ttributed grmmr For eh prodution X Y 1 Y 2... Y k, X.syn depends on X.inh nd ll ttributes of Y 1, Y 2,..., Y k. For ll 1 i k, Y i.inh depends on X.inh nd ll ttributes of Y 1, Y 2,..., Y i 1. 18/28
S-TTRIBUTED ND L-TTRIBUTED GRMMRS S-ttributed grmmr ll ttributes re synthesized. ttributes flow bottom-up. L-ttributed grmmr For eh prodution X Y 1 Y 2... Y k, X.syn depends on X.inh nd ll ttributes of Y 1, Y 2,..., Y k. For ll 1 i k, Y i.inh depends on X.inh nd ll ttributes of Y 1, Y 2,..., Y i 1. S-ttributed grmmrs re speil se of L-ttributed grmmrs. 18/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 19/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 10 3 + 5 E T E E T T + 5 3 10 19/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 10 3 + 5 = (10 3) + 5 = 12 10 3 + 5 10 (3 + 5) = 2 E E T E T T + 5 3 10 19/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 10 3 + 5 = (10 3) + 5 = 12 10 3 + 5 10 (3 + 5) = 2 E E T E T T + 5 3 10 This grmmr ptures left-ssoitivity. 19/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T 10 3 + 5 = (10 3) + 5 = 12 E ET 10 3 + 5 10 (3 + 5) = 2 T n + 12 E 7 E dd T 5 10 E sub T dd + 5 5 3 10 T sub 3 3 10 10 This grmmr ptures left-ssoitivity. 19/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 10 10 10 E T 10 7 sub sub 3 3 10 3 + 5 = (10 3) + 5 = 12 10 3 + 5 10 (3 + 5) = 2 12 E dd T 5 T dd + 5 5 3 E Rule R PREDIT(R) E T {n} E ET {n} T n {n} + {+} { } This grmmr ptures left-ssoitivity. 19/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 10 10 10 E T 10 7 sub sub 3 3 10 3 + 5 = (10 3) + 5 = 12 10 3 + 5 10 (3 + 5) = 2 12 E dd T 5 T dd + 5 5 3 E Rule R PREDIT(R) E T {n} E ET {n} T n {n} + {+} { } This grmmr ptures left-ssoitivity. It is not LL(1)! 19/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } 20/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } T E $ 10 T 3 E + E T E 5 ϵ 20/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T 3 + E T E 5 ϵ 20/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T sub 3 sub 3 3 T E + 5 ϵ E 20/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T 7 sub 3 sub 3 3 T E + 5 ϵ E 20/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T 7 sub 3 sub 3 3 dd T 5 E + dd 5 5 ϵ E 20/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T 7 sub 3 sub 3 3 dd T 5 12 E + dd 5 5 ϵ E 20/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T 7 sub 3 sub 3 3 dd T 5 12 E + dd 5 5 ϵ E 12 20/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 10 E $ 10 10 12 T 7 E sub 3 sub 3 3 dd T 5 12 E + dd 5 5 ϵ 12 20/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E 12 10 $ 10 10 12 T 7 E sub 3 sub 3 3 dd T 5 12 E + dd 5 5 ϵ 12 20/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: 12 PREDIT E E TE $ {n} T 10 E E 12 $ ϵ {$} 10 E TE {+, } 12 10 10 T 7 E T n {n} sub 3 + {+} sub 3 3 T E dd { } 5 12 + dd 5 5 ϵ 12 20/28
LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: E TE $ E ϵ E 1 TE 2 T n + {E.in = T.vl; E.vl = E.vl} {E.vl = E.in} {E 2.in =.fun(e 1.in, T.vl); E 1.vl = E 2.vl} {T.vl = n.vl} {T.fun = dd} {T.fun = sub} 20/28
ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 21/28
ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 21/28
TION ROUTINES tion routines re instrutions for d-ho trnsltion interleved with prsing. Prser genertors (e.g., bison or y) llow progrmmers to speify tion routines in the grmmr. tion routines n pper nywhere in rule (s long s the grmmr is LL(1)). 22/28
TION ROUTINES: EXMPLE Exmple: E T {$3.in = $1.fun($0.in, $2.vl)} E {$0.vl = $3.vl} 23/28
TION ROUTINES: EXMPLE Exmple: E T {$3.in = $1.fun($0.in, $2.vl)} E {$0.vl = $3.vl} orresponding prse funtion in reursive desent prser: def prseee(node0): node1 = PrseTreeNode() node2 = PrseTreeNode() node3 = PrseTreeNode() prse(node1) prset(node2) node3.op = node1.fun(node0.in, node2.vl) prseee(node3) node0.vl = node3.vl 23/28
ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 24/28
ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 24/28
BSTRT SYNTX TREES Problem with prse trees: They represent the full derivtion of the progrm using grmmr rules. Some grmmr vribles re there only to id in prsing (e.g., to eliminte left-reursion or ommon prefixes). ode genertor is esier to implement if the output of the prser is s ompt s possible. bstrt syntx tree (ST) ompressed prse tree tht represents the progrm struture rther thn the prsing proess. 25/28
BSTRT SYNTX TREE: EXMPLE (1) Fun fun id Stmts. Stmts ϵ Stmts Stmt Stmts Stmt... 26/28
BSTRT SYNTX TREE: EXMPLE (1) Fun fun id Stmts. Stmts ϵ Stmts Stmt Stmts Stmt... fun foo swp drop +. 26/28
BSTRT SYNTX TREE: EXMPLE (1) Fun fun id Stmts. Stmts ϵ Stmts Stmt Stmts Stmt... fun foo swp drop +. Fun fun id Stmts. Stmt (swp) Stmts Stmt (drop) Stmts Stmt (+) Stmts ϵ 26/28
BSTRT SYNTX TREE: EXMPLE (1) Fun fun id Stmts. Stmts ϵ Stmts Stmt Stmts Stmt... fun foo swp drop +. Fun fun id Stmts. ST: Stmt (swp) Stmts Fun Stmt (drop) Stmts id Stmts Stmt (+) Stmts Stmt (swp) Stmt (drop) Stmt (+) ϵ 26/28
BSTRT SYNTX TREE: EXMPLE (2) def prsefun(node0): node1 = PrseTreeNode() node2 = PrseTreeNode() mthfunkw() prseid(node1) prsesttements(node2) mthendkw() def prsesttements(node0): if next token is.: node0.sttements = [] else: node1 = PrseTreeNode() node2 = PrseTreeNode() prsesttement(node1) prsesttement(node2) node0.sttements = \ [node1.sttement] + \ node2.sttements Fun fun id Stmts. id Stmt (swp) Stmt (drop) Fun Stmts Stmt (+) Stmts Stmts Stmts Stmt (swp) Stmt (drop) Stmt (+) ϵ 27/28
SUMMRY Semnti nlysis ugments the prsing proess to represent the mening of the progrm. The output is often n nnotted bstrt syntx tree (ST). ttribute grmmrs nd tion routines re used to onstrut the ST. 28/28