270 likes | 413 Views
This lecture delves into the transformative technique of Attribute Grammar (AG), exploring its fundamental concepts and practical applications. The presentation starts with an introduction to AG's approach and the associated tools, illustrated by examples that enhance understanding. It highlights the semantic functions processed through trees and showcases the creation of simplifiers, type checkers, and compilers. The session culminates in a conclusion that emphasizes the significance of recursive semantic functions in programming. Join us to unravel the complexities of AG and its utility in computation.
E N D
Part 1 : Transformation Technique Lecture 1: Attribute Grammar Dr. Ir. I.S.W.B. Prasetya wishnu@cs.uu.nl A. Azurat S.Kom. ade@cs.uu.nl
Plan • Introduction • AG Approach • AG Tool • Examples • Conclusion
Transformation transformation result sentence "abba" semantic function parser P parse tree A A P B P B a b b a
Example data Form = Form `AND` Form | Form `IMP` Form | Var String | Const Bool Example:(Var "a" `AND` Var "b") `IMP` Var "a"
Example simp (Var a) = Var a simp (Const c) = Const c For every semantic function, you have to code the recursion by hand. simp (p `AND` q) = case (simp p, simp q) of ... (Const True , q') -> q' (p' , Const True) -> p' otherwise -> Nothing
a computation on tree Composite Semantic Function • In composition: sequencing computations can be subtle • Upgrading a computation: may introduce various intermediate administrative data stucture whose relation can be quite subtle repmin t = replace (minT t) t
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 Attribute Grammar Approach It is a formalism to abstractly describe semantic functions
x inherited attribute And And Var y 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 synthesized attribute
Defining countVar in AG DATA Form | Const Bool | Var String | And p,q: Form
Defining countVar in AG ATTR Form [ x : String | | result : Int ] SEM Form | Const lhs .result = 0 | Var lhs .result = if @lhs.x == @string then 1 else 0
Defining countVar in AG SEM Form ... | And p .x = @lhs.x q .x = @lhs.x lhs .result = @p.result + @q.result
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 ...
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
cryptic notation ? currently ... each AG description only generate 1 sem-function per data structure. Hurdles SEM ... | Var lhs .result = if @lhs.x == @string then 1 else 0 ... sem_Form ((And (_p) (_q))) = ... ...
substitution list Example: Renaming rename [ x a, y b ] x /\ y /\ z = a /\ b /\ z
Writing Rename with AG imports { import List } { 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 ]
can be omitted copy rule Writing Rename with AG SEM Form | Const lhs .result = Const @bool | Var lhs .result = Var (subst @string @lhs.subs) | And lhs .result = And @p.result @q.result p .subs = @lhs.subs q .subs = @lhs.subs
Example: Normalizing Names normalizeNames x /\ c /\ z = a /\ b /\ c
PASS 1: compute subs-list first ... [ (x, a), (c, b), (z, c) ] PASS 2: rename Strategy normalizeNames x /\ c /\ z = a /\ b /\ c
for generating new (normalized) name 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 | ]
can be omitted copy rule cntVar, nsubs cntVar, nsubs Const b Pass 1 SEM Form | Const lhs .nsubs = @lhs.nsubs .cntVar = @lhs.cntVar
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'
Can be omitted copy 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
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 ]
no imperative sequencing of passes! copy rule! Combining Aspects SEM Root | Root f .cntVar = 0 .nsubs = [ ] .subs = @f.nsubs lhs .result = @f.result
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
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