Download presentation
Presentation is loading. Please wait.
Published byAnita Berglund Modified over 6 years ago
1
COMPILER DESIGN 11CS30013 & 11CS30014 Group 33 23 October 2013
2
Topics Covered Evaluation of a parse tree containing both Synthesized and Inherited attributes L – attributed grammars 23 October 2013 Compiler Design – CS31003
3
Evaluation of parse tree with Inherited attributes
Dependency Graph : A graph with the attributes of the symbols in the parse tree as nodes and directed edges indicating the flow of attributes. Evaluation of a parse tree of an S-attributed SDD is simple. It needs just a Post Order Traversal of the dependency graph. However, doing the same with a grammar having inherited attributes as well is incorrect. 23 October 2013 Compiler Design – CS31003
4
Evaluation of parse tree with Inherited attributes
7 /14 A F X 10 /11 Consider the dependency graph on the right. Objective : To find a linear order of traversal using Topological sort on the DAG implies that FinishTime(u) > FinishTime(v) and that u must come before v in the linear order. Implying that the linear order must be descending w.r.t. FinishTime Y Z B E 8 /13 9 /12 u v Q P C D 1 /6 4 /5 R G 2 /3 Black lines denote the productions. Red arrows are the actual edges of the dependency graph – denoting the movement of attributes Therefore, the linear order is : A B E F D C G Attributes should be calculated in this order so that the parse tree can be calculated with success 29 December 2018 Compiler Design – CS31003
5
L – attributed SDD An L – attributed SDD can contain both Synthesized and Inherited attributes but with certain constraints. The constraints are : Synthesized attribute of a symbol can be calculated using the inherited attribute of the parent only and not the synthesized attribute. Consider a production of the form : A -> X1 X2 … Xi … Xn Then, for the calculation of the inherited attribute of Xi, the synthesized and inherited attributes of only X1, X2, … , Xi-1 are allowed to be used There should be no cycle in the dependency graph denoting the calculation of attributes Set of grammars supported by S-attributed SDD is a subset of the set of grammars supported by L-attributed SDD Both LR and LL parsers are able to parse languages having L-attributed grammars 29 December 2018 Compiler Design – CS31003
6
L – attributed SDD A X Y An example of Invalid L-attributed SDD
Consider the production with the semantic rules A X Y A.i = f(X.j, Y.k) X.i = f(A.i, Y.k) The grammar is Invalid because for the calculation of the attribute i of the symbol X, we need an attribute of Y – which comes after X in the production 29 December 2018 Compiler Design – CS31003
7
Applications of L – attributed SDD
Facilitates translation of source code to target code Parse Tree Intermediate Code Evaluation of the parse tree. While evaluation of the parse tree, we can : Print Update data structures (particularly, Symbol Table) Construct Data structures (particularly, Syntax Tree) Execute internal codes Semantic Analysis Side Effect : Functions/procedures clubbed with semantic rules. For example, insertion of datatype into the symbol table entry of a variable when the declaration of that variable is encountered. 23 October 2013 Compiler Design – CS31003
8
Evaluation of L-attributed SDD (example)
Consider the following L-attributed grammar : Terminals : int, float, id Non Terminals : D(start symbol), T, L Attributes : L.inh(INHERITED), T.type Productions with semantic rules : D T L L.inh = T.type T int T.type = integer T float T.type = float L L1 , id L1.inh = L.inh addTable(L.inh, id.lexVal) L id addTable(L.inh, id.lexVal) addTable() function adds the datatype to the identifier 23 October 2013 SIT-IIT KGP
9
Evaluation of L-attributed SDD (example)
Input string : int x , y D T L inh = “integer” type = “integer” inh = “integer” L , id lexVal = “y” int addTable(integer, x) id lexVal = “x” 23 October 2013 Compiler Design – CS31003
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.