COMPILER DESIGN 11 CS 30013 11 CS 30014

  • Slides: 9
Download presentation
COMPILER DESIGN 11 CS 30013 & 11 CS 30014 Group 33 23 October 2013

COMPILER DESIGN 11 CS 30013 & 11 CS 30014 Group 33 23 October 2013 Compiler Design – CS 31003 1

Topics Covered • Evaluation of a parse tree containing both Synthesized and Inherited attributes

Topics Covered • Evaluation of a parse tree containing both Synthesized and Inherited attributes • L – attributed grammars 23 October 2013 Compiler Design – CS 31003 2

Evaluation of parse tree with Inherited attributes • Dependency Graph : A graph with

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 – CS 31003 3

Evaluation of parse tree with Inherited attributes 7 /14 A F Consider the dependency

Evaluation of parse tree with Inherited attributes 7 /14 A F Consider the dependency graph on the right. Objective : To find a linear order of traversal Y using Topological sort on the DAG /13 B 8 u X 10 /11 E v implies that Finish. Time(u) > Finish. Time(v) and that u must come before v in the linear order. P C D 4 /5 Implying that the linear order must be descending w. r. t. Finish. Time Therefore, the linear order is : G Z 9 /12 Q 1 /6 R 2 /3 • Black lines denote the productions. • Red arrows are the actual edges of the dependency graph – denoting the movement of attributes ABEFDCG Attributes should be calculated in this order so that the parse tree can be calculated with success 23 November 2020 Compiler Design – CS 31003 4

L – attributed SDD • An L – attributed SDD can contain both Synthesized

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 -> X 1 X 2 … Xi … Xn Then, for the calculation of the inherited attribute of Xi, the synthesized and inherited attributes of only X 1, X 2, … , 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 23 November 2020 Compiler Design – CS 31003 5

L – attributed SDD • An example of Invalid L-attributed SDD Consider the production

L – attributed SDD • An example of Invalid L-attributed SDD Consider the production with the semantic rules A XY 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 23 November 2020 Compiler Design – CS 31003 6

Applications of L – attributed SDD • Facilitates translation of source code to target

Applications of L – attributed SDD • Facilitates translation of source code to target code Parse Tree • Semantic Analysis 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 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 – CS 31003 7

Evaluation of L-attributed SDD (example) Consider the following L-attributed grammar : Terminals : int,

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 TL L. inh = T. type T int T. type = integer T float T. type = float L L 1 , id L 1. inh = L. inh L id add. Table(L. inh, id. lex. Val) add. Table() function adds the datatype to the identifier 23 October 2013 SIT-IIT KGP 8

Evaluation of L-attributed SDD (example) D Input string : int x , y te

Evaluation of L-attributed SDD (example) D Input string : int x , y te = h in int 23 October 2013 L L , id inh = “integer” ge r” T “in type = “integer” lex. Val = “y” add. Table(integer, x) id lex. Val = “x” Compiler Design – CS 31003 9