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 have little structure. ◮ During the process structure such as an Abstract Syntax Tree ( AST ) is created. AST input code output code
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.
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
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 )
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 )
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 )
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 )
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 )
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 : [ ])))))
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.
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
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
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
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
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
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
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 )
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.
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 .
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 ]
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 ]
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 ]
Synthesised attribute computation in UUAG ATTR Root Expr Decls Decl Var [ | | allvars : { [ String ] } USE {∪} { [ ] } ] SEM Var | Ident lhs . allvars = [ @ name ]
Synthesised attribute computation in UUAG ATTR Root Expr Decls Decl Var [ | | allvars : { [ String ] } USE {∪} { [ ] } ] SEM Var | Ident lhs . allvars = [ @ name ]
Synthesised attribute computation in UUAG ATTR ∗ [ | | allvars : { [ String ] } USE {∪} { [ ] } ] SEM Var | Ident lhs . allvars = [ @ name ]
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.
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.
Recommend
More recommend