Workshop: Towards Highly Portable Software Jakarta, 21 – 23 January 2003 Diselenggarakan oleh Universitas IndonesiaUniversitas Indonesia Part 1 : Transformation Technique Dr. Ir. I.S.W.B. A. Azurat Lecture 1: Attribute Grammar
2 Plan Introduction AG Approach AG Tool Examples Conclusion
3 Transformation sentence transformation result "abba" P A P A P B B a a b b parse tree parser semantic function
4 Example data Form = Form `AND` Form | Form `IMP` Form | Var String | Const Bool Example: (Var "a" `AND` Var "b") `IMP` Var "a"
5 Example simp (Var a)= Var a simp (Const c)= Const c simp (p `AND` q)= case (simp p, simp q) of... (Const True, q') -> q' (p', Const True)-> p' otherwise -> Nothing For every semantic function, you have to code the recursion by hand.
6 Composite Semantic Function repmin t = replace (minT t) t In composition: sequencing computations can be subtle Upgrading a computation: may introduce various intermediate administrative data stucture whose relation can be quite subtle a computation on tree
7 Attribute Grammar Approach It is a formalism to abstractly describe semantic functions AG tool AG description sem. function repmin simplifier type checker compiler test generator... in the same class as compiler compiler (YACC) compiler generator example AG tool: LRC, AG
8 Attributes countVar (Const _) x= 0 countVar (Var y) x= if x==y then 1 else 0 countVar (And p q) x= countVar p x + countVar q x And Var y And x synthesized attribute inherited attribute
9 Defining countVar in AG DATA Form | Const Bool | Var String | And p,q: Form
10 Defining countVar in AG ATTR Form [ x : String | | result : Int ] SEM Form | Const lhs.result= 0 | Var lhs.result = then 1 else 0
11 Defining countVar in AG SEM Form... | And lhs.result
12 Generating the Sem Function Write AG source in file-name.ag –file-name must begin with CAPITAL letter c:/app/hugs/runhugs -98 –mdcfs file-name –Will generate file-name.hs Set the buffered number of lines in your DOS-window to
13 Result data Form = And (Form) (Form) | Const (Bool) | Var (String) sem_Form ((And (_p) (_q))) = sem_Form_Const (_bool) (_lhs_x) = 0 sem_Form (And (Var "x") (Var "y") ) "x" = 1
14 Hurdles SEM... | Var lhs.result = then 1 else 0 cryptic notation ?... sem_Form ((And (_p) (_q))) = currently... each AG description only generate 1 sem-function per data structure.
15 substitution list Example: Renaming rename [ x a, y b ] x /\ y /\ z = a /\ b /\ z
16 Writing Rename with AG { type SubsList = [ ( String, String ) ] subst :: String -> SubsList -> String subst x s = case find (\(y,z)-> y==x) s of Just (_,z) ->z otherwise -> x } ATTR Form [ subs : SubsList | | result : Form ] imports { import List }
17 Writing Rename with AG SEM Form | Const lhs.result= | And | Var lhs.result = can be omitted copy rule
18 Example: Normalizing Names normalizeNames x /\ c /\ z = a /\ b /\ c
19 Strategy normalizeNames x /\ c /\ z = a /\ b /\ c PASS 1: compute subs-list first... [ (x, a), (c, b), (z, c) ] PASS 2 : rename
20 Writing Pass 1 with AG { alreadyIn :: String -> SubsList -> Bool x `alreadyIn` s = find (\(y,k)-> y==x) s /= Nothing } ATTR Form [ | cntVar : Int nsubs : SubsList | ] for generating new (normalized) name
21 can be omitted copy rule Pass 1 SEM Form | Const lhs.nsubs= Const b cntVar, nsubs
22 Pass 1 SEM Form... | Var loc.seen ++ [chr(ord 'a' + 1 lhs.nsubs
23 Can be omitted copy rule ! Pass 1 SEM Form... |
24 Pass or Aspect ? Rather than sequencing passes, we will combine aspects ! Each SEM computation may consist of several aspects, rather than passes DATA Root | Root f : Form ATTR Root [ | | result : Form ]
25 no imperative sequencing of passes! copy rule! Combining Aspects SEM Root | Root f.cntVar=0.nsubs=[
26 Resulting AG Code... SEM Form | Const lhs.result= | Var lhs.result = | And SEM Form | Var loc.seen ++ [chr(ord 'a' + 1 @seen SEM Root | Root
27 Conclusion Abstract –recursion is generated –copying and chaining information is generated –sequencing passes is automatic Highly compositional –computation can be split in aspects Highly extensible –extending a computation is just adding a new aspect