Presentation is loading. Please wait.

Presentation is loading. Please wait.

CH4.1 CSE244 L-Attributed Definitions Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.

Similar presentations


Presentation on theme: "CH4.1 CSE244 L-Attributed Definitions Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit."— Presentation transcript:

1 CH4.1 CSE244 L-Attributed Definitions Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT 06269 aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias

2 CH4.2 CSE244 Traversals of Parse-trees  Identify a natural traversal that will allow the 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.  Not consistent with all possible ways to define inherited attributes though.

3 CH4.3 CSE244 L-Attributed Definitions  A syntax-directed definition is called L-Attributed if  In the semantic rules of a production A  X 1 …X n the inherited attributes of X j depend only on  The attributes (synthesized or inherited) of the symbols X 1 …X j-1  The inherited attributes of A  All S-attributed Definitions are also L-attributed.

4 CH4.4 CSE244Example Example of a Syntax-Directed Definition that is not L-attributed. PRODUCTIONSEMANTIC RULE A  LML.i = f 1 (A.i) M.i = f 2 (L.s) A.s = f 3 (M.s) A  QRR.i = f 4 (A.i) Q.i = f 5 (R.s) A.s = f 6 (Q.s)

5 CH4.5 CSE244 Translation Schemes  A CFG with “semantic actions” embedded into its productions. Useful for binding the order of evaluation into the parse-tree.  As before semantic actions might refer to the attributes of the symbols. Example. expr  expr + term { print(“+”) } expr  expr - term { print(“-”) } expr  term term  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.

6 CH4.6 CSE244 Writing Translation Schemes  Start with a Syntax-Directed Definition.  In General: Make sure that we never refer to an attribute that has not been defined already.  For S-Attributed Definitions, we simply put all semantic rules into {…} at the rightmost of each production.  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…

7 CH4.7 CSE244Examples S  A 1 {S.s = A 1.s+A 2.s} A 2 A  a {A.s = 1} This will not work… On the other hand this is good: S  A 1 A 2 {S.s = A 1.s+A 2.s} A  a {A.s = 1}

8 CH4.8 CSE244 Examples II S  A 1 A 2 {A 1.in = 1; A 2.in = 2} A  a {print(A.in)} This will not work… On the other hand this is good: S  {A 1.in = 1; A 2.in = 2} A 1 A 2 A  a {print(A.in)} Or even: S  {A 1.in = 1} A 1 {A 2.in = 2} A 2 A  a {print(A.in)}

9 CH4.9 CSE244 A “Case Study” I  Math Formatting Language (EQN). S  B B  B B B  B sub B B  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

10 CH4.10 CSE244 Case Study II  How do we want to interpret strings such as  How do we want to interpret strings such as text sub text text?  Every is a “TextBox” or simply a “box”  Every text is a “TextBox” or simply a “box” S  B B  B B B  B sub B B  text  In the grammar stands for a box and for a statement.  In the grammar B stands for a box and S for a statement.  Intended translation is a typesetting of the given text, interpreting as  Intended translation is a typesetting of the given text, interpreting B sub B as B B

11 CH4.11 CSE244 Case Study III First we give a syntax-directed definition. We employ one attribute for called that will give the height of the statement (synthesized) We employ one attribute for S called ht that will give the height of the statement (synthesized) The height of text is specified by the lexical analyzer For we employ a attribute (synthesized) as well as an attribute that will denote a “multiplier.” For B we employ a ht attribute (synthesized) as well as an attribute ps (inherited) that will denote a “multiplier.” Also the functions shrink( ), max( ), disp( ) PRODUCTIONSEMANTIC RULE S  BB.ps = 10; S.ht = B.ht B  B 1 B 2 B 1.ps = B.ps; B 2.ps = B.ps; B.ht = max(B 1.ht, B 2.ht) B  B 1 sub B 2 B 1.ps = B.ps; B 2.ps = shrink(B.ps); B.ht = disp(B 1.ht, B 2.ht) B  textB 1.ht = text.h * B.ps Observe that the definition is L-attributed

12 CH4.12 CSE244 Case Study IV PRODUCTIONSEMANTIC RULE S  BB.ps = 10; S.ht = B.ht B  B 1 B 2 B 1.ps = B.ps; B 2.ps = B.ps; B.ht = max(B 1.ht, B 2.ht) B  B 1 sub B 2 B 1.ps = B.ps; B 2.ps = shrink(B.ps); B.ht = disp(B 1.ht, B 2.ht) B  textB 1.ht = text.h * B.ps TRANSLATION SCHEME S  {B.ps = 10} B { S.ht = B.ht } B  {B 1.ps = B.ps} B 1 {B 2.ps = B.ps } B 2 {B.ht = max(B 1.ht, B 2.ht) } B  {B 1.ps = B.ps} B 1 sub {B 2.ps = shrink(B.ps)} B 2 {B.ht = disp(B 1.ht, B 2.ht)} B  text {B 1.ht = text.h * B.ps }


Download ppt "CH4.1 CSE244 L-Attributed Definitions Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit."

Similar presentations


Ads by Google