attribute grammars in haskell with uuag
play

Attribute Grammars in Haskell with UUAG Andres L oh joint work - PowerPoint PPT Presentation

Attribute Grammars in Haskell with UUAG Andres L oh joint work with S. Doaitse Swierstra and Arthur Baars andres@cs.uu.nl 10 February 2005 A simplified view on compilers Input is transformed into output. Input and output language


  1. Attribute Grammars in Haskell with UUAG Andres L¨ oh joint work with S. Doaitse Swierstra and Arthur Baars andres@cs.uu.nl 10 February 2005

  2. A simplified view on compilers ◮ Input is transformed into output. ◮ Input and output language have little structure. ◮ During the process structure such as an Abstract Syntax Tree ( AST ) is created. AST input code output code

  3. Abstract syntax and grammars ◮ The structure in an AST is best described by a (context-free) grammar. ◮ A concrete value (program) is a word of the language defined by that grammar. Expr → Var -- variable | Expr Expr -- application | Var Expr -- lambda abstraction ◮ The rules in a grammar are called productions . The right hand side of a rule is derivable from the left hand side. ◮ The symbols on the left hand side are called nonterminals . ◮ A word is in the language defined by the grammar if it is derivable from the root nonterminal in a finite number of steps.

  4. Example grammar In the following, we will use the following example grammar for a very simple language: Root → Expr Expr → Var -- variable | Expr Expr -- application | Var Expr -- λ | Decls Expr -- let Decls → Decl Decls | ε Decl → Var Expr → String Var -- name

  5. Haskell: Algebraic datatypes ◮ In Haskell, you can define your own datatypes. ◮ Choice is encoded using multiple constructors . ◮ Constructors may contain fields . ◮ Types can be parametrized . ◮ Types can be recursive . = Zero | One data Bit data Complex = Complex Real Real data Maybe a = Just a | Nothing data List a = Nil | Cons a ( List a )

  6. Haskell: Algebraic datatypes ◮ In Haskell, you can define your own datatypes. ◮ Choice is encoded using multiple constructors . ◮ Constructors may contain fields . ◮ Types can be parametrized . ◮ Types can be recursive . = Zero | One data Bit data Complex = Complex Real Real data Maybe a = Just a | Nothing data List a = Nil | Cons a ( List a )

  7. Haskell: Algebraic datatypes ◮ In Haskell, you can define your own datatypes. ◮ Choice is encoded using multiple constructors . ◮ Constructors may contain fields . ◮ Types can be parametrized . ◮ Types can be recursive . = Zero | One data Bit data Complex = Complex Real Real data Maybe a = Just a | Nothing data List a = Nil | Cons a ( List a )

  8. Haskell: Algebraic datatypes ◮ In Haskell, you can define your own datatypes. ◮ Choice is encoded using multiple constructors . ◮ Constructors may contain fields . ◮ Types can be parametrized . ◮ Types can be recursive . = Zero | One data Bit data Complex = Complex Real Real data Maybe a = Just a | Nothing data List a = Nil | Cons a ( List a )

  9. Haskell: Algebraic datatypes ◮ In Haskell, you can define your own datatypes. ◮ Choice is encoded using multiple constructors . ◮ Constructors may contain fields . ◮ Types can be parametrized . ◮ Types can be recursive . = Zero | One data Bit data Complex = Complex Real Real data Maybe a = Just a | Nothing data List a = Nil | Cons a ( List a )

  10. Haskell: Algebraic datatypes (contd.) ◮ There is a builtin list type with special syntax. data [ a ] = [ ] | a : [ a ] [ 1, 2, 3, 4, 5 ] == ( 1 : ( 2 : ( 3 : ( 4 : ( 5 : [ ])))))

  11. Grammars correspond to datatypes ◮ Given this power, each nonterminal can be seen as a data type. ◮ Productions correspond to definitions of constructors. ◮ For each constructor, we need a name. ◮ Type abstraction is not needed, but recursion is.

  12. The example grammar translated Root → Expr Root = Root Expr data Expr → Var data Expr = Var Var | Expr Expr | App Expr Expr | Var Expr | Lam Var Expr | Decls Expr | Let Decls Expr Decls → Decl Decls data Decls = Cons Decls Decls | ε | Nil { - ε - } Decl → Var Expr data Decl = Decl Var Expr → String = Ident String Var data Var

  13. The example grammar translated Root → Expr DATA Root | Root Expr Expr → Var DATA Expr | Var Var | Expr Expr | App fun : Expr arg : Expr | Var Expr | Lam Var Expr | Decls Expr | Let Decls Expr Decls → Decl Decls DATA Decls | Cons hd : Decls tl : Decls | ε | Nil { - ε - } Decl → Var Expr DATA Decl | Decl Var Expr → String | Ident name : String Var DATA Var

  14. The example grammar translated Root → Expr DATA Root | Root Expr Expr → Var DATA Expr | Var Var | Expr Expr | App fun : Expr arg : Expr | Var Expr | Lam Var Expr | Decls Expr | Let Decls Expr Decls → Decl Decls TYPE Decls = [ Decl ] | ε Decl → Var Expr DATA Decl | Decl Var Expr → String | Ident name : String Var DATA Var

  15. UUAG datatypes ◮ Datatypes in UUAG are much like in Haskell. ◮ Constructors of different datatypes may have the same name. ◮ Some minor syntactical differences. ◮ Each field has a name. The type name is the default. DATA Expr | Var Var | App fun : Expr arg : Expr | Lam Var Expr | Let Decls Expr is an abbreviation of DATA Expr | Var var : Var | App fun : Expr arg : Expr | Lam var : Var expr : Expr | Let decls : Decls expr : Expr

  16. UUAG datatypes ◮ Datatypes in UUAG are much like in Haskell. ◮ Constructors of different datatypes may have the same name. ◮ Some minor syntactical differences. ◮ Each field has a name. The type name is the default. DATA Expr | Var Var | App fun : Expr arg : Expr | Lam Var Expr | Let Decls Expr is an abbreviation of DATA Expr | Var var : Var | App fun : Expr arg : Expr | Lam var : Var expr : Expr | Let decls : Decls expr : Expr

  17. An example value Root ( Let ( Cons ( Decl ( Ident "k" ) ( Var ( Ident "const" ))) ( Cons ( Decl ( Ident "i" ) ( Lam ( Ident "x" ) ( Var ( Ident "x" )))) Nil )) ( App ( Var ( Ident "k" )) ( Var ( Ident "i" )))) Haskell-like syntax: let k = const i = λ x → x in k i

  18. AST Root ( Root ) Let ( Expr ) App ( Expr ) Cons ( Decls ) Decl ( Decl ) Cons ( Decls ) Var ( Expr ) Var ( Expr ) Ident ( Var ) Var ( Expr ) Decl ( Decl ) Nil ( Decls ) Ident ( Var ) Ident ( Var ) Ident ( Var ) Ident ( Var ) Lam ( Expr ) Ident ( Var ) Var ( Expr ) Ident ( Var )

  19. Computation follows structure ◮ Many computations can be expressed in a common way. ◮ Information is passed upwards. ◮ Constructors are replaced by operations. ◮ In the leaves, results are created. ◮ In the nodes, results are combined.

  20. Synthesised attributes ◮ In UUAG (and in attribute grammars), computations are modelled by attributes . ◮ Each of the examples defines an attribute. ◮ Attributes that are computed bottom-up are called synthesised attributes .

  21. Synthesised attribute computation in UUAG ATTR Root Expr Decls Decl Var [ | | allvars : { [ String ] } ] SEM Root | Root lhs . allvars = @ expr . allvars SEM Expr | Var lhs . allvars = @ var . allvars | App lhs . allvars = @ fun . allvars ∪ @ arg . allvars | Lam lhs . allvars = @ var . allvars ∪ @ expr . allvars | Let lhs . allvars = @ decls . allvars ∪ @ expr . allvars SEM Decls | Cons lhs . allvars = @ hd . allvars ∪ @ tail . allvars | Nil lhs . allvars = [ ] SEM Decl | Decl lhs . allvars = @ var . allvars ∪ @ expr . allvars SEM Var | Ident lhs . allvars = [ @ name ]

  22. Synthesised attribute computation in UUAG ATTR Root Expr Decls Decl Var [ | | allvars : { [ String ] } ] SEM Root | Root lhs . allvars = @ expr . allvars SEM Expr | Var lhs . allvars = @ var . allvars | App lhs . allvars = @ fun . allvars ∪ @ arg . allvars | Lam lhs . allvars = @ var . allvars ∪ @ expr . allvars | Let lhs . allvars = @ decls . allvars ∪ @ expr . allvars SEM Decls | Cons lhs . allvars = @ hd . allvars ∪ @ tail . allvars | Nil lhs . allvars = [ ] SEM Decl | Decl lhs . allvars = @ var . allvars ∪ @ expr . allvars SEM Var | Ident lhs . allvars = [ @ name ]

  23. Synthesised attribute computation in UUAG ATTR Root Expr Decls Decl Var [ | | allvars : { [ String ] } ] SEM Expr | App lhs . allvars = @ fun . allvars ∪ @ arg . allvars | Lam lhs . allvars = @ var . allvars ∪ @ expr . allvars | Let lhs . allvars = @ decls . allvars ∪ @ expr . allvars SEM Decls | Cons lhs . allvars = @ hd . allvars ∪ @ tail . allvars | Nil lhs . allvars = [ ] SEM Decl | Decl lhs . allvars = @ var . allvars ∪ @ expr . allvars SEM Var | Ident lhs . allvars = [ @ name ]

  24. Synthesised attribute computation in UUAG ATTR Root Expr Decls Decl Var [ | | allvars : { [ String ] } USE {∪} { [ ] } ] SEM Var | Ident lhs . allvars = [ @ name ]

  25. Synthesised attribute computation in UUAG ATTR Root Expr Decls Decl Var [ | | allvars : { [ String ] } USE {∪} { [ ] } ] SEM Var | Ident lhs . allvars = [ @ name ]

  26. Synthesised attribute computation in UUAG ATTR ∗ [ | | allvars : { [ String ] } USE {∪} { [ ] } ] SEM Var | Ident lhs . allvars = [ @ name ]

  27. Abbreviations ◮ UUAG allows the programmer to omit straight-forward propagation. ◮ For synthesised attributes, a synthesised attribute is by default propagated from the leftmost child that provides an attribute of the same name. ◮ If instead the results should be combined in a uniform way, a USE construct can be employed. This takes a constant which becomes the default for a leaf, and a binary operator which becomes the default combination operator.

  28. Abbreviations ◮ UUAG allows the programmer to omit straight-forward propagation. ◮ For synthesised attributes, a synthesised attribute is by default propagated from the leftmost child that provides an attribute of the same name. ◮ If instead the results should be combined in a uniform way, a USE construct can be employed. This takes a constant which becomes the default for a leaf, and a binary operator which becomes the default combination operator.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend