Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.

Slides:



Advertisements
Similar presentations
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Advertisements

Compiler Construction Sohail Aslam Lecture IR Taxonomy IRs fall into three organizational categories 1.Graphical IRs encode the compiler’s knowledge.
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.
Syntax Directed Translation
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Department.
Abstract Syntax Tree (AST)
Syntax Directed Translation
CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Syntax-Directed Translation
CH5.1 CSE 4100 Chapter 5: Syntax Directed Translation Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut.
1 Compiler Construction 강의 8. 2 Context-Free Languages The inclusion hierarchy for context-free languages Context-free languages Deterministic languages.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
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.
Chapter 8: Intermediate Code Generation
1 Semantic Analysis. 2 Semantic Analyzer Attribute Grammars Syntax Tree Construction Top-Down Translators Bottom-Up Translators Recursive Evaluators Type.
1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
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.
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.
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.
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.
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.
Semantics Analysis.
Chapter 5: Syntax Directed Translation
Syntax-Directed Translation
Compiler Construction
Chapter 5 Syntax Directed Translation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Syntax-Directed Translation Part I
Syntax-Directed Translation Part II
Chapter 5: Syntax Directed Translation
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
SYNTAX DIRECTED TRANSLATION
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
Syntax-Directed Translation Part II
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Syntax-Directed Translation Part II
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation Part II
Directed Acyclic Graphs (DAG)
Syntax-Directed Translation Part I
COP4020 Programming Languages
COP4020 Programming Languages
Chapter 5 Syntax Directed Translation
Presentation transcript:

Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called “annotating the parse tree.” Computing values to be stored at the annotated nodes is called “decorating the parse tree.”

Synthesized Attributes Attributes passed up the parse tree. For example, the following involves synthesizing attributes: expr: expr ‘+’ expr { $$ = $1; }

Synthesized Attribute $$ = $1; E ($$) / | \ E ($1) + E ($2)

Inherited Attributes Attributes passed downwards or sideways in the parse tree. The following involves passing attributes down the parse tree: expr: expr ‘+’ expr { $1 = $$; } The following involves passing sideways the parse tree: expr: expr ‘+’ expr { $1 = $2; }

Attribute Inherited Downwards $1 = $$; E ($$) / | \ E ($1) + E ($2)

Attribute Inherited Sideways $2 = $1; E ($$) / | \ E ($1) + E ($2)

Desk Calculator - Semantic Rules ProductionSemantic Rules L  E n E  E 1 + T E  T T  T 1 * F T  F F  ( E ) F  digit print(E.val) E.val = E 1.val + T.val E.val = T.val T.val = T 1.val * F.val T.val = F.val F.val = E.val F.val = digit.lexval

Parse Tree for 3 * n L | \ E.val = 19 n / | \ E.val = 15 + T.val = 4 | | T.val = 15 F.val = 4 / | \ | T.val = 3 * F.val = 5 digit.lexval = 4 | | F.val = 3 digit.lexval = 5 | digit.lexval = 3

Declarations – Semantic Rules productionSemantic Rules D  T L T  int T  float L  L 1, id L  id L.in = T.type T.type = integer T.type = float L 1.in = L.in addtype(id.entry,L.in)

Parse Tree for float id 1, id 2, id 3 D / \ T.type = float L.in = float | / | \ float L.in = float, id 3 / | \ L.in = float, id 2 | id 1

Abstract Syntax Tree (AST) (a  b) + ((a  b) + c) + / \  + / \ / \ a b  c / \ a b

Table Representation of AST (1)  ab (2)  ab (3)+(2)c (4)+(1)(3)

Directed Acyclic Graph (DAG) (a  b) + ((a  b) + c) + \ + / \  c / \ a b

Table Representation of DAG (1)  ab (2)+(1)c (3)+(1)(2)

Generate AST from Productions ProductionSemantic Rules E  E 1 + T E  E 1 – T E  T T  ( E ) T  id T  num E.nptr = mknode(‘+’, E 1.nptr,T.nptr) E.nptr = mknode(‘’, E 1.nptr,T.nptr) E.nptr = T.nptr T.nptr = E.nptr T.nptr = mkleaf(id, id.entry) T.nptr = mkleaf(num, num.val)

Prod to AST Example (a  b) + ((a  b) + c) p 1 mkleaf(id,a) p 2 mkleaf(id,b) p 3 mknode(‘-’, p 1, p 2 ) p 4 mkleaf(id,a) p 5 mkleaf(id,b) p 6 mknode(‘-’, p 4, p 5 ) p 7 mkleaf(id,c) p 8 mknode(‘+’, p 6, p 7 ) p 9 mknode(‘+’, p 3, p 8 )