Compilers Principles, Techniques, & Tools Taught by Jing Zhang

Slides:



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

Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
SYNTAX DIRECTED TRANSLATION 11CS Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal.
Semantics Static semantics Dynamic semantics attribute grammars
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
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.
Syntax Directed Definitions Synthesized Attributes
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Syntax-Directed Translation
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 CMPS 450 Syntax-Directed Translations CMPS 450 J. Moloney.
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.
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.
LESSON 04.
Syntax Directed Definition and Syntax directed Translation
UNIT – 5 SYNTAX-DIRECTED TRANSLATION
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Course Revision.. Contents  Lexical Analysis  Our Compiler Structure  Tokens, Lexemes & Patterns  RE’s & Regular Definitions  Transition Diagrams.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
SDTs used to implement SDDs A non-cyclic SDD (having definitions of attributes) can always be implemented by a SDT (having actions that assign values to.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Overview of Previous Lesson(s) Over View  Structure of the LR Parsing Table  Consists of two parts: a parsing-action function ACTION and a goto function.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
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.
Lecture 9 Symbol Table and Attributed Grammars
Semantic analysis Jakub Yaghob
Syntax-Directed Translation
A Simple Syntax-Directed Translator
Constructing Precedence Table
Introduction to Parsing (adapted from CS 164 at Berkeley)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
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
CS 3304 Comparative Languages
Lexical and Syntax Analysis
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Definition
SYNTAX DIRECTED TRANSLATION
Chapter 2: A Simple One Pass Compiler
COMPILER DESIGN 11CS30013 & 11CS30014 Group October 2013
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Chapter 10: Compilers and Language Translation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
COP4020 Programming Languages
Lecture 16 Boolean Expressions and Control Flow
Chapter 5 Syntax Directed Translation
Faculty of Computer Science and Information System
Compiler design Review COMP 442/6421 – Compiler Design
Presentation transcript:

Compilers Principles, Techniques, & Tools Taught by Jing Zhang (jzhang@njust.edu.cn)

Syntax-Directed Translation

Outlines Introduction Syntax-Directed Definitions Evaluation Orders for SDD 's Applications of Syntax-Directed Translation Syntax-Directed Translation Schemes

6.1 Introduction We associate information with a language construct by attaching attributes to the grammar symbol(s) representing the construct. A syntax-directed definition specifies the values of attributes by associating semantic rules with the grammar productions. An infix-to-postfix translator subscript in E1 distinguishes the occurrence of E in the production body from the occurrence of E as the head

6.1 Introduction A syntax-directed translation scheme embeds program fragments called semantic actions within production bodies. The position of a semantic action in a production body determines the order in which the action is executed. In production (5. 2) , the action occurs at the end , after all the grammar symbols; in general, semantic actions may occur at any position in a production body. Between the two notations, syntax-directed definitions can be more readable, and hence more useful for specifications. However, translation schemes can be more efficient , and hence more useful for implementations.

6.2 Syntax-Directed Definitions A syntax-directed definition ( SDD) is a context-free grammar together with attributes and rules. Attributes are associated with grammar symbols and rules are associated with productions. If X is a symbol and a is one of its attributes, then we write X.a to denote the value of a at a particular parse-tree node labeled X. Attributes may be of any kind: numbers , types , table references , or strings, for instance. The strings may even be long sequences of code, say code in the intermediate language used by a compiler.

6.2 Syntax-Directed Definitions - Inherited and Synthesized Attributes A synthesized attribute for a nonterminal A at a parse-tree node N is defined by a semantic rule associated with the production at N. Note that the production must have A as its head. A synthesized attribute at node N is defined only in terms of attribute values at the children of N and at N itself. An inherited attribute for a nonterminal B at a parse-tree node N is defined by a semantic rule associated with the production at the parent of N. Note that the production must have B as a symbol in its body. An inherited attribute at node N is defined only in terms of attribute values at N's parent , N itself, and N's siblings.

6.2 Syntax-Directed Definitions - Inherited and Synthesized Attributes Terminals can have synthesized attributes, but not inherited attributes. Attributes for terminals have lexical values that are supplied by the lexical analyzer; there are no semantic rules in the SDD itself for computing the value of an attribute for a terminal. The rule for production 1 , sets L.val to E.val, which we shall see is the numerical value of the entire expression. Production 2 also has one rule, which computes the val attribute for the head E as the sum of the values at E1 and T. At any parse tree node N labeled E, the value of val for E is the sum of the values of val atthe children of node N labeled E and T.

6.2 Syntax-Directed Definitions - Inherited and Synthesized Attributes An SDD that involves only synthesized attributes is called S-attributed. In an S-attributed SDD, each rule computes an attribute for the nonterminal at the head of a production from attributes taken from the body of the production. An S-attributed SDD can be implemented naturally in conjunction with an LR parser. An SDD without side effects is sometimes called an attribute grammar. Side effects: printing the result or interacting with a symbol table

6.2 Syntax-Directed Definitions - Inherited and Synthesized Attributes Evaluating an SDD at the Nodes of a Parse Tree. To visualize the translation specified by an SDD, it helps to work with parse trees, even though a translator need not actually build a parse tree. A parse tree, showing the value(s) of its attribute(s) is called an annotated parse tree. For SDD's with both inherited and synthesized attributes, there is no guarantee that there is even one order in which to evaluate attributes at nodes.

6.2 Syntax-Directed Definitions - Inherited and Synthesized Attributes

6.2 Syntax-Directed Definitions - Inherited and Synthesized Attributes The selection of the attributes is determined by parsing method. Bottom-up

6.2 Syntax-Directed Definitions - Inherited and Synthesized Attributes The selection of the attributes is determined by parsing method. Top-down

6.3 Evaluation Orders for SDD 's - Dependency Graphs "Dependency graphs" are a useful tool for determining an evaluation order for the attribute instances in a given parse tree. While an annotated parse tree shows the values of attributes, a dependency graph helps us determine how those values can be computed. A dependency graph depicts the flow of information among the attribute instances in a particular parse tree; an edge from one attribute instance to another means that the value of the first is needed to compute the second. Edges express constraints implied by the semantic rules.

6.3 Evaluation Orders for SDD 's - Dependency Graphs

6.3 Evaluation Orders for SDD 's - Dependency Graphs

6.3 Evaluation Orders for SDD 's - S-Attributed Definitions An SDD is S-attributed if every attribute is synthesized When an .SDD is S-attributed, we can evaluate its attributes in any bottom up order of the nodes of the parse tree. It is often especially simple to evaluate the attributes by performing a postorder traversal of the parse tree and evaluating the attributes at a node N when the traversal leaves N for the last time.

6.3 Evaluation Orders for SDD 's - L-Attributed Definitions

6.3 Evaluation Orders for SDD 's - L-Attributed Definitions

6.3 Evaluation Orders for SDD 's - Semantic Rules with Controlled Side Effects We shall control side effects in SDD's in one of the following ways : Permit incidental side effects that do not constrain attribute evaluation. In other words, permit side effects when attribute evaluation based on any topological sort of the dependency graph produces a "correct" translation, where "correct" depends on the application . Constrain the allowable evaluation orders, so that the same translation is produced for any allowable order. The constraints can be thought of as implicit edges added to the dependency graph.

6.3 Evaluation Orders for SDD 's - Semantic Rules with Controlled Side Effects

6.3 Evaluation Orders for SDD 's - Semantic Rules with Controlled Side Effects

6.4 Applications of Syntax-Directed Translation Construction of Syntax Trees

6.4 Applications of Syntax-Directed Translation

6.4 Applications of Syntax-Directed Translation

6.4 Applications of Syntax-Directed Translation The Structure of a Type The nonterminals B and T have a synthesized attribute t representing a type. The nonterminal C has two attributes: an inherited attribute b and a synthesized attribute t. The inherited b attributes pass a basic type down the tree, and the synthesized t attributes accumulate the result.

6.4 Applications of Syntax-Directed Translation

6.5 Syntax-Directed Translation Schemes Syntax-directed translation schemes are a complementary notation to syntax directed definitions. All of the applications of syntax-directed definitions can be implemented using syntax- directed translation schemes. A syntax- directed translation scheme (SDT) is a context free grammar with program fragments embedded within production bodies . The program fragments are called semantic actions and can appear at any position within a production body.

6.5 Syntax-Directed Translation Schemes

6.5 Syntax-Directed Translation Schemes Postfix Translation Schemes The simplest SDD implementation occurs when we can parse the grammar bottom-up and the SDD is S-attributed. In that case, we can construct an SDT in which each action is placed at the end of the production and is executed along with the reduction of the body to the head of that production.

6.5 Syntax-Directed Translation Schemes Parser-Stack Implementation of Postfix SDT’s Postfix SDT's can be implemented during LR parsing by executing the actions when reductions occur. The attribute(s) of each grammar symbol can be put on the stack in a place where they can be found during the reduction. The best plan is to place the attributes along with the grammar symbols (or the LR states that represent these symbols) in records on the stack itself.

6.5 Syntax-Directed Translation Schemes

6.5 Syntax-Directed Translation Schemes SDT's With Actions Inside Productions

6.5 Syntax-Directed Translation Schemes

6.5 Syntax-Directed Translation Schemes

6.5 Syntax-Directed Translation Schemes-Left Recursion

6.5 Syntax-Directed Translation Schemes When the actions of an SDD compute attributes rather than merely printing output, we must be more careful about how we eliminate left recursion from a grammar. However , if the SDD is S-attributed, then we can always construct an SDT by placing attribute-computing actions at appropriate positions in the new productions.

6.5 Syntax-Directed Translation Schemes

6.5 Syntax-Directed Translation Schemes SDT's for L-Attributed Definitions The rules for turning an L-attributed SDD into an SDT are as follows: 1. Embed the action that computes the inherited attributes for a nonterminal A immediately before that occurrence of A in the body of the production. If several inherited attributes for A depend on one another in an acyclic fashion, order the evaluation of attributes so that those needed first are computed first. 2. Place the actions that compute a synthesized attribute for the head of a production at the end of the body of that production.

6.5 Syntax-Directed Translation Schemes

6.5 Syntax-Directed Translation Schemes

6.5 Syntax-Directed Translation Schemes

Homework 6 Consider the following grammar (a) write a syntax-directed definition that prints out the number of parenthesis pairs. (b) write a syntax-directed translation scheme that prints out the depth of each a. (c) Write a syntax-directed translation scheme that prints out the order number of each a.