part 1 : transformation technique
DESCRIPTION
Part 1 : Transformation Technique. Lecture 1: Attribute Grammar. Dr. Ir. I.S.W.B. [email protected] A. Azurat [email protected]. Plan. Introduction AG Approach AG Tool Examples Conclusion. Transformation. transformation. result. sentence. "abba". semantic function. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/1.jpg)
Workshop: Towards Highly Portable SoftwareJakarta, 21 – 23 January 2003
Diselenggarakan oleh Universitas Indonesia
Part 1 : Transformation Technique
Dr. Ir. I.S.W.B. Prasetya [email protected]. Azurat S.Kom. [email protected]
Lecture 1: Attribute Grammar
![Page 2: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/2.jpg)
2
Plan
• Introduction• AG Approach• AG Tool• Examples• Conclusion
![Page 3: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/3.jpg)
3
Transformation
sentencetransformation result
"abba"
P
A P A
PB B
a ab b
parse tree
parser semantic function
![Page 4: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/4.jpg)
4
Example
data Form = Form `AND` Form
| Form `IMP` Form
| Var String
| Const Bool
Example:
(Var "a" `AND` Var "b") `IMP` Var "a"
![Page 5: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/5.jpg)
5
Example
simp (Var a) = Var asimp (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.
![Page 6: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/6.jpg)
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
![Page 7: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/7.jpg)
7
Attribute Grammar Approach
It is a formalism to abstractly describe semantic functions
AG toolAG 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
![Page 8: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/8.jpg)
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 yAnd
xsynthesized attribute
inherited attribute
![Page 9: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/9.jpg)
9
Defining countVar in AG
DATA Form
| Const Bool
| Var String
| And p,q: Form
![Page 10: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/10.jpg)
10
Defining countVar in AG
ATTR Form [ x : String | | result : Int ]
SEM Form
| Const lhs .result = 0
| Varlhs .result = if @lhs.x == @string then 1 else 0
![Page 11: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/11.jpg)
11
Defining countVar in AG
SEM Form
...
| Andp
.x = @lhs.x
q .x = @lhs.x
lhs .result = @p.result + @q.result
![Page 12: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/12.jpg)
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 200 ...
![Page 13: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/13.jpg)
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
![Page 14: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/14.jpg)
14
Hurdles
SEM ... | Var
lhs.result = if @lhs.x == @string then 1 else 0
cryptic notation ?
...sem_Form ((And (_p) (_q))) = ......
currently ... each AG description only generate 1 sem-function per data structure.
![Page 15: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/15.jpg)
15
substitution list
Example: Renaming
rename
[ x a, y b ]
x /\ y /\ z
=
a /\ b /\ z
![Page 16: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/16.jpg)
16
Writing Rename with AG
{type SubsList = [ ( String, String ) ]
subst :: String -> SubsList -> Stringsubst x s = case find (\(y,z)-> y==x) s of Just (_,z) -> z otherwise -> x}
ATTR Form [ subs : SubsList | | result : Form ]
imports{import List}
![Page 17: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/17.jpg)
17
Writing Rename with AG
SEM Form
| Const lhs .result = Const @bool
| Andlhs .result = And @p.result @q.result
| Varlhs .result = Var (subst @string @lhs.subs)
p .subs = @lhs.subsq .subs = @lhs.subs
can be omitted copy rule
![Page 18: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/18.jpg)
18
Example: Normalizing Names
normalizeNames
x /\ c /\ z
=
a /\ b /\ c
![Page 19: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/19.jpg)
19
Strategy
normalizeNames
x /\ c /\ z
=
a /\ b /\ c
PASS 1: compute subs-list first ...
[ (x, a), (c, b), (z, c) ]
PASS 2 : rename
![Page 20: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/20.jpg)
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
![Page 21: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/21.jpg)
21
can be omitted copy rule
Pass 1
SEM Form
| Const lhs
.nsubs = @lhs.nsubs
.cntVar = @lhs.cntVar
Const b
cntVar, nsubs cntVar, nsubs
![Page 22: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/22.jpg)
22
Pass 1
SEM Form ...
| Var
loc .seen = @string `alreadyIn` @lhs.nsubs
.nsubs' = @lhs.nsubs ++ [(@string, [chr(ord 'a' + @lhs.cntVar)])]
.cntVar' = @lhs.cntVar + 1
lhs
.nsubs = if @seen then @lhs.nsubs else @nsubs'
.cntVar = if @seen then @lhs.cntVar else @cntVar'
![Page 23: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/23.jpg)
23
Can be omittedcopy rule !
Pass 1
SEM Form... | And
p .cntVar = @lhs.cntVar .nsubs = @lhs.nsubs
q .cntVar = @p.cntVar .nsubs = @p.nsubs
lhs .cntVar = @q.cntVar .nsubs = @q.nsubs
![Page 24: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/24.jpg)
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 ]
![Page 25: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/25.jpg)
25
no imperative sequencing of passes!
copy rule!
Combining Aspects
SEM Root
| Rootf .cntVar = 0
.nsubs = [ ]
.subs = @f.nsubs
lhs
.result = @f.result
![Page 26: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/26.jpg)
26
Resulting AG Code ...SEM Form | Const
lhs.result = Const @bool | Var
lhs.result = Var (subst @string @lhs.subs) | And
lhs.result = And @p.result @q.result
SEM Form | Var
loc .seen = @string `alreadyIn` @lhs.nsubs .nsubs' = @lhs.nsubs ++ [(@string, [chr(ord 'a' + @lhs.cntVar)])] .cntVar' = @lhs.cntVar + 1
lhs .nsubs = if @seen then @lhs.nsubs else @nsubs' .cntVar = if @seen then @lhs.cntVar else @cntVar'
SEM Root | Root
f .cntVar = 0 .nsubs = [] .subs = @f.nsubs
![Page 27: Part 1 : Transformation Technique](https://reader036.vdocuments.mx/reader036/viewer/2022062501/56815c34550346895dca1869/html5/thumbnails/27.jpg)
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