L-Attributed Definitions
description
Transcript of L-Attributed Definitions
CH4.1
CSE244
L-Attributed DefinitionsL-Attributed Definitions
Aggelos KiayiasComputer Science & Engineering Department
The University of Connecticut371 Fairfield Road, Unit 1155
Storrs, CT [email protected]
http://www.cse.uconn.edu/~akiayias
CH4.2
CSE244
Traversals of Parse-treesTraversals of Parse-trees
Identify a natural traversal that will allow the Identify a natural traversal that will allow the systematic evaluation of attributes.systematic evaluation of attributes.
procedure dfsvisit(n:node){ for each child m of n from left to right do
{ evaluate inherited attributes of m;dfsvisit(m)
}evaluate synthesized attributes of n
} DFS consistent with S-Attributed Definitions.DFS consistent with S-Attributed Definitions. Not consistent with all possible ways to define Not consistent with all possible ways to define
inherited attributes though.inherited attributes though.
CH4.3
CSE244
L-Attributed DefinitionsL-Attributed Definitions
A syntax-directed definition is calledA syntax-directed definition is calledL-AttributedL-Attributed
ifif In the semantic rules of a production
A X1…Xn
the inherited attributes of Xj depend only on The attributes (synthesized or inherited) of the
symbols X1…Xj-1
The inherited attributes of A All S-attributed Definitions are also L-attributed.All S-attributed Definitions are also L-attributed.
CH4.4
CSE244
ExampleExample
Example of a Syntax-Directed Definition that is not L-attributed.
PRODUCTION SEMANTIC RULEA LM L.i = f1(A.i)
M.i = f2(L.s)A.s = f3(M.s)
A QR R.i = f4(A.i)Q.i = f5(R.s)A.s = f6(Q.s)
CH4.5
CSE244
Translation SchemesTranslation Schemes
A CFG with “semantic actions” A CFG with “semantic actions” embeddedembedded into its into its productions. Useful for binding the order of evaluation into productions. Useful for binding the order of evaluation into the parse-tree.the parse-tree.
As before semantic actions might refer to the attributes of As before semantic actions might refer to the attributes of the symbols.the symbols.
Example.Example.expr expr + term { print(“+”) }expr expr - term { print(“-”) }expr termterm 0 { print(“0”) }…term 9 { print(“9”) }
=> Traversing a parse tree for a Translation-Scheme produces the translation. (we will employ only DFS)
=> Translation schemes also help in materializing L-attributed Definitions.
CH4.6
CSE244
Writing Translation SchemesWriting Translation Schemes
Start with a Syntax-Directed Definition.Start with a Syntax-Directed Definition. In GeneralIn General: Make sure that we never refer to an attribute : Make sure that we never refer to an attribute
that has not been defined already.that has not been defined already. For S-Attributed Definitions, we simply put all semantic For S-Attributed Definitions, we simply put all semantic
rules into {…} at the rightmost of each production.rules into {…} at the rightmost of each production. If both inherited and synthesized attributes are involved:If both inherited and synthesized attributes are involved:
An inherited attribute for a symbol on the RHS of a production must be computed in an action before that symbol.
An action must not refer to a synthesized attribute of a symbol that is to the right.
A synthesized attribute for the NT on the LHS can only be computed after all attributes it references are already computed. (the action for such attributes is typically placed in the rightmost end of the production).
L-attributed definitions are suited for the above…L-attributed definitions are suited for the above…
CH4.7
CSE244
ExamplesExamples
S A1 {S.s = A1.s+A2.s} A2
A a {A.s = 1}
This will not work…This will not work…
On the other hand this is good:On the other hand this is good:S A1 A2 {S.s = A1.s+A2.s}
A a {A.s = 1}
CH4.8
CSE244
Examples IIExamples II
S A1 A2 {A1.in = 1; A2.in = 2}A a {print(A.in)}
This will not work…This will not work…
On the other hand this is good:On the other hand this is good:S {A1.in = 1; A2.in = 2} A1 A2
A a {print(A.in)}
Or even:Or even:S {A1.in = 1} A1 {A2.in = 2} A2
A a {print(A.in)}
CH4.9
CSE244
A “Case Study” IA “Case Study” I
Math Formatting Language (EQN).Math Formatting Language (EQN).S BB B BB B sub BB text
“sub” is a reserved word. “text” is interpreted by the lex.an. as any string
without spaces.
e.g. “E sub 1 .val” is read by the Lexical Analyzer as text sub text text
CH4.10
CSE244
Case Study IICase Study II
How do we want to interpret strings such asHow do we want to interpret strings such as text sub text text
?? Every Every text is a “TextBox” or simply a “box”is a “TextBox” or simply a “box”
S BB B BB B sub BB text
In the grammar In the grammar B stands for a box and stands for a box and S for a for a statement.statement.
Intended translation is a typesetting of the given Intended translation is a typesetting of the given text, interpreting text, interpreting B sub B as as BB
CH4.11
CSE244
Case Study IIICase Study III
First we give a syntax-directed definition.First we give a syntax-directed definition.We employ one attribute for We employ one attribute for S called called ht that will give the height of that will give the height of
the statement (synthesized)the statement (synthesized)The height of text is specified by the lexical analyzerThe height of text is specified by the lexical analyzerFor For B we employ a we employ a ht attribute (synthesized) as well as an attribute (synthesized) as well as an
attribute attribute ps (inherited) that will denote a “multiplier.”that will denote a “multiplier.”Also the functions Also the functions shrink( ), max( ), disp( )shrink( ), max( ), disp( )
PRODUCTION SEMANTIC RULES B B.ps = 10; S.ht = B.htB B1 B2 B1.ps = B.ps; B2.ps = B.ps;
B.ht = max(B1.ht, B2.ht)B B1 sub B2 B1.ps = B.ps; B2.ps = shrink(B.ps);
B.ht = disp(B1.ht, B2.ht)B text B1.ht = text.h * B.ps
Observe that the definition is L-attributed
CH4.12
CSE244
Case Study IVCase Study IV
PRODUCTION SEMANTIC RULES B B.ps = 10; S.ht = B.htB B1 B2 B1.ps = B.ps; B2.ps = B.ps;
B.ht = max(B1.ht, B2.ht)B B1 sub B2 B1.ps = B.ps; B2.ps = shrink(B.ps);
B.ht = disp(B1.ht, B2.ht)B text B1.ht = text.h * B.ps
TRANSLATION SCHEMES {B.ps = 10} B { S.ht = B.ht }B {B1.ps = B.ps} B1 {B2.ps = B.ps } B2 {B.ht = max(B1.ht,
B2.ht) }B {B1.ps = B.ps} B1 sub {B2.ps = shrink(B.ps)} B2 {B.ht =
disp(B1.ht, B2.ht)}B text {B1.ht = text.h * B.ps }