190 likes | 310 Views
First-class Attribute Grammars. Oege de Moor joint work with Kevin Backhouse and Doaitse Swierstra. Structuring AGs. flow patterns chains. semantic aspects type checking. remote attribute access copying inherited attributes. and many more. our aims:
E N D
First-class Attribute Grammars Oege de Moor joint work with Kevin Backhouse and Doaitse Swierstra
Structuring AGs flow patterns chains semantic aspects type checking remote attribute access copying inherited attributes and many more... • our aims: • a compositional semantics for attribute grammars • expressed as executable functions in Haskell • to study both existing and novel combining forms
Attributions type Attrs = Name Value Value : disjoint sum of all attribute types joining attributions () :: Attrs Attrs Attrs a record type would be safer, but also more cumbersome
Attributes type At = ( Attrs, Attrs ) embed (e,p) = e project (e,p) = p project embed = id type safety for attribute values
Rules An attribute definition rule at production P maps the input attributes of P to some of P’s output attributes (inherited of parent, synthesised of children) (synthesised of parent, inherited of children) allow multiple output attributes so rules can be joined
Rules An attribute definition rule is a function (inherited of parent, synthesised of children) (synthesised of parent, inherited of children) what is the type of these things? type Fam = (Attrs, [Attrs]) type Rule = Fam Fam attribution of parent attribution of each child
Example rule tree0 ::= tree1 tree2 tree0.locmin = min tree1.locmin tree2.locmintree2.gmin = tree0.gmin synthesised inherited (tree0, [tree1 , tree2 ] ) (embed locmin (min (project locmin tree1) (project locmin tree1) ), [, embed gmin (project gmin tree0)])
Example rule chainRule :: At Rule chainRule (e,p) (inhp, syncs) = (last outs, init outs) where outs = map (e p) ins ins = inhp : syncs
Recap • Attributionsfinite map from names to values • Families(parent attribution, [child attribution]) • Rulesinput family (inherited, [synthesised]) to output family (synthesised, [inherited])
Semantic aspects type Aspect = ProdName Rule count number of occurrences of x to left of each leaf counts :: Int Aspect counts x = chain count [node] define at root[tree] : 0 leaf[tree] : if val.value == x then tree.count + 1 else tree.count root: start tree node: tree tree tree leaf: tree val details in paper
Aims semantics of building blocks: Attributions Families Rules Aspects How assemble them into semantics of translators?
Semantics of trees type SemTree = Attrs Attrs inherited synthesised
Semantics of productions type SemProd = [SemTree] SemTree semantics of children semantics of parent rules are similar, but we have notconnected defining occurrences to uses
From rules to productions inherited attributes of children synthesised attributes of parent knit :: Rule SemProd knit r fs inhp = synp where (synp, inhcs) = r (inhp,syncs) syncs = applyList fs inhcs rule inherited attributes of parent synthesised attributes of children semantics of children applyList [] xs = [] applyList (f:fs) (x:xs) = f x : applyList fs xs
Semantics of AGs type AG = ProdName SemProd knitAspect :: Aspect AG knitAspect as = knit as
Translation data Tree = Fork ProdName [Tree] | Val Attrs attribution from scanner/parser trans :: AG Tree SemTree trans ag (Fork l ts) inh = ag l (map (trans ag) ts) inh trans ag (Val a) inh = a
Summary Attrs = Name Value Fam = (Attrs,[Attrs]) Rule = Fam Fam Aspect = ProdName Rule SemTree = Attrs Attrs SemProd = [SemTree] SemTree AG = ProdName SemProd trans :: AG Tree SemTree
Discussion • Executable denotational semantics • Prototype lacks static checksabstract interpretation • Record calculus gives greater safetybut can be too restrictive • Further structuringfirst-class productions