80 likes | 216 Views
LW - Grammatica CF. Prog ::= Decs main () Stats. Decs ::= Dec | Dec Decs. Dec ::= VDec | FDec. VDec ::= PDec | Type Id = Exp; PDec ::= Type Id; FDec ::= Type Id(PDecs) {[ with Decs do ] Stats result Exp} | void Id(PDecs) {[ with Decs do ] Stats} PDecs ::= PDec | PDec PDecs.
E N D
LW - Grammatica CF Prog ::= Decs main() Stats. Decs ::= Dec | Dec Decs. Dec ::= VDec | FDec. VDec ::= PDec | Type Id = Exp; PDec ::= Type Id; FDec ::= Type Id(PDecs) {[with Decs do] Stats result Exp} | void Id(PDecs) {[with Decs do] Stats} PDecs ::= PDec | PDec PDecs. Type ::= int | bool. Stats ::= Stat | Stat Stats. Stat ::= Exp; | while (Exp) {Stats} | if (Exp) {Stats} else {Stats}| ?Id | !Exp. Exp ::= Id | Id = Exp | Id(Exps) | Exp + Exp | … Exps ::= Exp | Exp Exps.
Funzioni di Semantica Statica [_]sProg : LProg(LW) P {*} [_]sDecs : LDecs(LW) Envs Envs PEnvs [_]sDec : LDec(LW) Envs Envs PEnvs [_]sDec(d,r,rl) = [_]sDecs(d,r,rl) [_]sVDec : LVDec(LW) Envs Envs PEnvs [_]sVDec(d,r,rl) = [_]sDecs(d,r,rl) [_]sPDecs : LPDecs(LW) Envs Envs PEnvs [_]sPDecs(d,r,rl) = [_]sDecs(d,r,rl) [_]sPDec : LPDec(LW) Envs Envs PEnvs [_]sPDec(d,r,rl) = [_]sDecs(d,r,rl) [_]sFDec : LFDec(LW) Envs Envs PEnvs [_]sFDec(d,r,rl) = [_]sDecs(d,r,rl) [_]sStats : LStats(LW) Envs P {*} [_]sStat : LStat(LW) Envs P {*} [_]sStat(st,r) = [_]sStats(st,r) [_]sExp : LExp(LW) Envs P Types [_]sExp(e,r) = [_]sExps(e,r) [_]sExps : LExps(LW) Envs P Types+ Envs = [LId(LW) PTypes]Fin, dove Types = BTypes (BTypes+ RTypes), dove BTypes = {int, bool} e RTypes = BTypes {void} Notazione per funzioni parziali a dominio finito f[X PA]Fin se f è [], Dom(f) = Ø se f è [a1/x1,…, an/xn] con tutte le xidistinte, Dom(f) = {x1,…,xn } e f(xi)=(ai ) se f è g[a/x], Dom(f) = Dom(g) {x} e f(x)= g(x) se x x, altrimenti f(x)=a se f è g[h], Dom(f) = Dom(g)Dom(h) e f(x)= h(x) se x Dom(h), altrimenti f(x)= g(x) se f è g\X, Dom(f) = Dom(g)\X e f(x)= g(x)
Regole della Sem. Statica per Decs [d]s(r, rl) = rloc [ds]s(r, rloc) = r0 _____________________________________________________ [d ds]s(r, rl) = r0 [e]s(r[rl])=t _________________________________ idDom(rl) [t id = e;]s(r,rl) = rl[t/id] ______________________________ idDom(rl) [t id;]s(r,rl) = rl[t/id] [pl](r[rl],[(lt,void)/f]) = rloc[sts](r[rl] [rloc]) = * lt=types(pl) __________________________________________________________________ fDom(rl) [void f(pl) {sts}](r,rl) = rl[(lt,void)/f] [pl](r[rl],[(lt,t)/f]) = rloc[sts](r[rl] [rloc]) = * [e](r[rl] [rloc]) = t lt=types(pl) _____________________________________________________________________________________ fDom(rl) [t f(pl) {sts result e}](r,rl) = rl[(lt,t)/f] t ≠ void [pl](r[rl],[(lt,void)/f]) = rloc[ds](r[rl], rloc) = r’loc[sts](r”) = * lt=types(pl) ________________________________________________________________________________ fDom(rl) [void f(pl) {with ds do sts}](r,rl) = rl[(lt,void)/f]r” = r[rl] [r’loc] [pl](r[rl],[(lt,t)/f]) = rloc[ds](r[rl], rloc) = r’loc[sts](r”) = * [e](r”) = t lt=types(pl) __________________________________________________________________________________________ fDom(rl) [t f(pl) {with ds do sts result e}](r,rl) = rl[(lt,t)/f]r” = r[rl] [r’loc] t ≠ void
Regole della Sem. Statica per Exps, Stats e Prog [e]s(r) = t[es]s(r) = lt ____________________________________ [e es]s(r) = t lt ________________________ id Dom(r) [id]s (r) = r(id) r(id) BTypes [e]s(r) = t __________________________ r(id) = t [id = e]s (r) = t [es](r) = lt _____________________ r(id) = (lt,t) [id(es)](r) = t [e1](r) = int [e2](r) = int ____________________________________ [e1+ e2](r) = int etc... [e]s(r) = t ________________ [e;]s (r) = * [e]s(r) = bool [sts]s (r) = * _________________________________________ [while (e) {sts}]s (r) = * [e]s(r) = t ________________ [!e]s (r) = * ____________________ r(id) BTypes [?id]s (r) = * [e]s(r) = bool [sts1]s (r) = * [sts2]s (r) = * ______________________________________________________________ [if (e) {sts1} else{sts2} ]s (r) = * [ds]([],[]) = r [sts]s(r) = * ________________________________________________ [ds main() sts]s (r) = *
Funzioni di Semantica Denotazionale [_]Prog : LProg(LW) InputP Output [_]Decs : LDecs(LW) Env States PEnv States [_]Dec : LDec(LW) Env States PEnv States [_]Dec(d,r,s) = [_]Decs(d,r,s) [_]VDec : LVDec(LW) Env States PEnv States [_]VDec(d,r,s) = [_]Decs(d,r,s) [_]PDecs : LPDecs(LW) Env States PEnv States [_]PDecs(d,r,s) = [_]Decs(d,r,s) [_]PDec : LPDec(LW) Env States PEnv States [_]PDec(d,r,s) = [_]Decs(d,r,s) [_]FDec : LFDec(LW) Env States PEnv States [_]FDec(d,r,s) = [_]Decs(d,r,s) [_]Type:LType(LW) Set [_]Stats : LStats(LW) Env States PStates [_]Stat : LStat(LW) Env States PStates [_]Stat(st,r,s) = [_]Stats(st,r,s) [_]Exp : LExp(LW) Env States P Value States [_]Exp(e,r,s) = [_]Exps(e,r,s) [_]Exps : LExps(LW) Env States P Value+ States Env = [LId(LW) p FunLoc]Fin, dove Loc = TBTypesLocT Fun = [Arg pRes], Arg = [Z| B]+ States e Res = [Z| B|{*}] States States = Input Output [Loc p Value]Fin, dove Value = TBTypes[T] e [int] = Z, [bool] = B, [void] = {*} Input = Output = Value* Nuova Loc Env States definito da Nuova(l,r,s) sse l (Im(r) Dom(s))
Regole della Sem. Denot. per Decs 1 F è definita induttivamente da tutte le regole della semantica più le seguenti 2 A [Aw se c’è il with] e B, per leggibilità riportate nel riquadro ______________________________________ [void f(T x) {[with ds do]sts}](r,s) = (r[F/f],s) [sts](r[/f,lx/x],sc[v/lx] ) = s’ A _____________________________________ Nuova(lx,r,sc) F(v,sc) = (*,s’\{lx}) [e](r0[/f],s0) = (u ,s’) F(u,s’) = (*,s”) B __________________________________________________ [f(e)](r0[/f],s0) = (*,s”) [ds](r[/f,lx/x],sc[v/lx] ) = r1,s1 [sts](r1,s1) = s’ Aw________________________________________________________ Nuova(lx,r,sc) F(v,sc) = (*,s’\{lx}) F è definita induttivamente da tutte le regole della semantica più le seguenti 2 A [Aw se c’è il with] e B, per leggibilità riportate nel riquadro ____________________________________________ [RT f(T x) {[with ds do]sts return e}](r,s) = (r[F/f],s) [sts](r[/f,lx/x],sc[v/lx] ) = s1 [e](r [/f,lx/x],s1) = (u ,s’) A ________________________________________________________ Nuova(lx,r,sc) F(v,sc) = (u,s’\{lx}) [ds](r[/f,lx/x],sc[v/lx] ) = r1,s1 [sts](r1,s1) = s2 [e](r1,s2) = (u ,s’) Aw_____________________________________________________________________________ Nuova(lx,r,sc) F(v,sc) = (u,s’\{lx}) [e](r0[/f],s0) = (v ,s’) F(v,s’) = (u,s”) B __________________________________________________ [f(e)](r0[/f],s0) = (u,s”)
Regole della Sem. Denot. per Decs ed Exps [d](r,s) = (r’,s’) [ds](r’,s’) = (r”,s”) ___________________________________________________ [d ds] (r,s) = (r”,s”) [e](r,s) = (v,s’) ______________________________________ Nuova(l,r,s’) [t x = e](r,s) = (r[l/x],s’[v/l]) lLocT ________________________________ Nuova(l,r,s) [t x](r,s) = (r[l/x],s[/l]) lLocT [e](r,s) = (v,s’) [es](r,s’) = (lv,s”) ___________________________________________________ [e es] (r,s) = (v lv,s”) [e](r,s) = (v,s’) ______________________________________ [x = e](r,s) = (v,s’[v/r(x)]) [es](r,s) = (lv ,s’) ______________________________ [f(es)](r,s) = r(f)(lv,s’) ___________________________r(x) Loc [x](r,s) = (s(r(x)),s) [e](r,s) = (v,s’) [e’](r,s’) = (v’,s”) __________________________________________________ v + v’ = a [e + e’] (r,s) = (a,s”) etc...
Regole della Sem. Denot. per Stats e Prog [st](r,s) = s’ [sts](r,s’) = s” __________________________________________ [st sts ] (r,s) = s” [e](r,s) = (v ,s’) _______________________ [e;](r,s) =s’ [e](r,s) = (tt ,s’) [sts](r,s’) = s” ____________________________________________ [if (e) {sts} else {sts’}](r,s) = s” [e](r,s) = (ff ,s’) [sts’](r,s’) = s” ____________________________________________ [if (e) {sts} else {sts’}](r,s) = s” [e](r,s) = (ff ,s’) ___________________________________ [while (e) {sts}](r,s) = s’ [e](r,s) = (tt ,s’) [sts](r,s’) = s” [while (e) {sts}](r,s”) = s0 ___________________________________________________________________________________ [while (e) {sts}](r,s) = s0 [ds]([],(i,l,[])) = (r ,s) [sts](r,s) = (i’,o,m) __________________________________________________________ [ds main() sts](i) = o