Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

SYNTAX DIRECTED TRANSLATION 11CS Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
1 Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what.
Lecture # 17 Syntax Directed Definition. 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”)
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
Abstract Syntax Tree (AST)
Syntax Directed Translation
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Syntax-Directed Translation
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Semantic Analysis1 Checking what parsers cannot.
Topic: Syntax Directed Translations
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
1 Semantic Analysis. 2 Semantic Analyzer Attribute Grammars Syntax Tree Construction Top-Down Translators Bottom-Up Translators Recursive Evaluators Type.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
Overview of Previous Lesson(s) Over View  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values of.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 Syntax-Directed Translation We associate information with the programming language constructs by attaching attributes to grammar symbols. 2.Values.
國立台灣大學 資訊工程學系 薛智文 98 Spring Syntax-Directed Translation (textbook ch#5.1–5.6, 4.8, 4.9 )
Syntax Directed Definition and Syntax directed Translation
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Compiler Principle and Technology Prof. Dongming LU Apr. 15th, 2015.
Semantic Analysis Attribute Grammar. Semantic Analysis  Beyond context free grammar  Is x declared before it is used?  Is x declared but never used?
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
1 Syntax-Directed Translation Part II Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Lecture 9 Symbol Table and Attributed Grammars
Semantic analysis Jakub Yaghob
Semantics Analysis.
Syntax-Directed Translation
Context-Sensitive Analysis
Compiler Construction
Chapter 5 Syntax Directed Translation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
Syntax-Directed Translation Part I
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Definition
SYNTAX DIRECTED TRANSLATION
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation
Directed Acyclic Graphs (DAG)
Syntax-Directed Translation Part I
Chapter 5 Syntax Directed Translation
Presentation transcript:

Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer to the symbol-table entry for that id, the attribute of a num is the value of the num ; 2.thinking : In the syntax analysis step, should each grammar symbol has its attribute? tip1 : Complier not only gives an answer whether the string is a sentence, but also need to do a lot of work, such as the generation of object code, error management and so on. tip2 : Many compiler is based on syntax analysis stage as the core, around syntax analysis to complete semantic analysis, intermediate code generation, code optimization and so on 。 tip3 : The attribute of grammar symbol is very board, it can be generation of code, calculation of the value, information stored in the symbol table and so on.

4.1 Syntax-Directed Definitions e.g.Syntax-directed definition of a simple desk calculator PRODUCTION SEMANTIC RULES L  E n L  E n print (E.val) print (E.val) E  E 1 + T E  E 1 + T E.val := E 1.val + T.val E.val := E 1.val + T.val E  T E  T E.val := T.val E.val := T.val T  T 1 * F T  T 1 * F T.val := T 1.val * F.val T.val := T 1.val * F.val T  F T  F T.val := F.val T.val := F.val F  (E) F  (E) F.val := E.val F.val := E.val F  digit F  digit F.val := digit.lexval F.val := digit.lexval

4.1 Syntax-Directed Definitions the form of Syntax-Directed Definitions Basic grammar Attributes are associated with grammar symbol Each grammar production A   has a set of semantic rules like b := f(c 1, c 2, …, c k ), where f is the function, b and c 1, c 2, …, c k are the attribute of the grammar symbol. Synthesized Attributes : if b is the attribute of A , c 1, c 2, …, c k are the attribute of right side symbol in the grammar production or other attribute of A. Inherited Attributes : if b is the attribute of a right side sumbol of x. Attribute is defined at the children or it self Attribute is defined at its parent, it self and its siblings 15/36

4.1 Syntax-Directed Definitions Synthesized Attributes S-Attributed Definitions : every attribute is synthesized PRODUCTION SEMANTIC RULES L  E n L  E n print (E.val) print (E.val) E  E 1 + T E  E 1 + T E.val := E 1.val + T.val E.val := E 1.val + T.val E  T E  T E.val := T.val E.val := T.val T  T 1 * F T  T 1 * F T.val := T 1.val * F.val T.val := T 1.val * F.val T  F T  F T.val := F.val T.val := F.val F  (E) F  (E) F.val := E.val F.val := E.val F  digit F  digit F.val := digit.lexval F.val := digit.lexval

4.1 Syntax-Directed Definitions The annotated parse tree for 8+5*2 n digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 Each node’s attribute value is annotated According to the annotated parse tree, let’s study the calculaton order of S-attributed definition

4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n

4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n

4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n 20/36

4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n

4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n

4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n

4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n

4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n 25/36

4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n

4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n

4.1 Syntax-Directed Definitions The calculation of attribute value of nodes of the annotated tree can be done from bottom to up digit.lexval = 2 L E.val = 18 n T.val = 10 E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n

4.1 Syntax-Directed Definitions The annotated parse tree :Each node’s attribute value is annotated digit.lexval = 2 L E.val = 18 n T.val = 10E.val = 8 T.val = 8 F.val = 8 digit.lexval = 8 T.val = 5 + * F.val = 5 F.val = 2 digit.lexval = 5 8+5*2 n

4.1 Syntax-Directed Definitions Inherited Attributes int id, id, id PRODUCTION SEMANTIC RULES D  TL D  TL L.in := T.type L.in := T.type T  int T  int T. type := integer T. type := integer T  real T  real T. type := real T. type := real L  L 1, id L  L 1, id L 1.in := L.in; L 1.in := L.in; addtype (id.entry, L.in ) addtype (id.entry, L.in ) L  id L  id addtype (id.entry, L.in ) addtype (id.entry, L.in ) 30/36

4.1 Syntax-Directed Definitions Annotated parse tree for int id 1, id 2, id 3 Dint T.type = integer, id 3 L.in = integer id 2 id 1,

4.1 Syntax-Directed Definitions Dependency Graphs Dependency graph for the annotated parse tree of int id 1, id 2, id 3 D  TL L.in := T.type D  TL L.in := T.typeD int int T, id 3 L L L id 2 id 1, 1 entry 10 2 entry 3 entry in 9 8 in 7 6 in 5 4 type

4.1 Syntax-Directed Definitions Dependency Graphs Dependency graph for the annotated parse tree of int id 1, id 2, id 3 L  L 1, id L 1.in := L.in; addtype (id.entry, L.in ) addtype (id.entry, L.in )D int int T, id 3 L L L id 2 id 1, 1 entry 10 2 entry 3 entry in 9 8 in 7 6 in 5 4 type

4.1 Syntax-Directed Definitions Ordering the Evaluation of Attributes Topological sort : if there is an edge of the dependency graph from Ni to Nj,then i < j e.g. : 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 D int int T, id 3 L L L id 2 id 1, 1 entry 10 2 entry 3 entry in 9 8 in 7 6 in 5 4 type

4.1 Syntax-Directed Definitions Ordering the Evaluation of Attributes 1 、 Structure input parse tree , 2 、 Structure dependency graph , 3 、 sort nodes by topological sort, 4 、 calculate the attribute by the order of topological sort 。 35/36

Supplement The understanding of attribute nonterminal  analysis process ( function ) synthesized attributes  the return value of process inherited attributes  the parameters of process

e.g. int id, id, id PRODUCTION SEMANTIC RULES D  TL D  TL L.in := T.type L.in := T.type T  int T  int T. type := integer T. type := integer T  real T  real T. type := real T. type := real L  id, L 1 L  id, L 1 L 1.in := L.in; L 1.in := L.in; addtype (id.entry, L.in ) addtype (id.entry, L.in ) L  id L  id addtype (id.entry, L.in ) addtype (id.entry, L.in ) Review

Review e.g. void D() { T_temp = T(); L_in = T_temp; L(L_in); L(L_in); return ; } int T() { switch lookahead { case INT: return INTEGER; { case INT: return INTEGER; case REAL: return REAL; case REAL: return REAL; default: error; default: error;}} void L(int L_in) {match(id); match( ‘, ’ ); match( ‘, ’ ); addtype(id.entry, L_in); L(L_in); L(L_in);} Void L(int L_in) { match (id); addtype(id.entry, L.in); } PRODUCT ION SEMANTIC RULES D  TL D  TL L.in := T.type T  int T  int T. type := integer T  real T  real T. type := real L  id, L 1 L  id, L 1 L 1.in := L.in; addtype (id.entry, L.in ) L  id L  id addtype (id.entry, L.in )

Exercise 4.1 According to the syntax-directed definition of table 4.1 , please structure the annotated parse tree for expression5 * (4*3+2) 4.1 According to the syntax-directed definition of table 4.1 , please structure the annotated parse tree for expression5 * (4*3+2) 。