Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……

Slides:



Advertisements
Similar presentations
SYNTAX DIRECTED TRANSLATION 11CS Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal.
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
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.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Attribute Grammars Recall the yacc program Parse a given expression
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
Compiler Construction Sohail Aslam Lecture Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not.
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
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Semantic Analysis1 Checking what parsers cannot.
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.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
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.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Introduction to Compiling
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
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.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Compiler Principle and Technology Prof. Dongming LU Apr. 15th, 2015.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Review 1.Structure of the course Lexical Analysis Syntax Analysis Grammar & Language RG & DFA Top-down LL(1) Parsing Bottom-Up LR Layered Automation Semantic.
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.
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
Parsing & Context-Free Grammars
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 5 Syntax Directed Translation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Compiler Lecture 1 CS510.
CS416 Compiler Design lec00-outline September 19, 2018
Introduction CI612 Compiler Design CI612 Compiler Design.
Chapter 2: A Simple One Pass Compiler
Compilers B V Sai Aravind (11CS10008).
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CS416 Compiler Design lec00-outline February 23, 2019
SYNTAX DIRECTED DEFINITION
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Chapter 5 Syntax Directed Translation
Faculty of Computer Science and Information System
Presentation transcript:

Syntax Directed Translation

Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……

Syntax Directed Translation The parsing process and parse trees are used to direct semantic analysis and the translation of the source program. This can be a separate phase of a compiler or we can augment our conventional grammar with information to control the semantic analysis and translation. Such grammars are called attribute grammars (P & Attribute Equation)

The syntax analyzer directs the whole process 1.Calls the lexical analyzer whenever syntax analyzer wants another token. 2.Perform the actions of semantic analyzer. 3.Perform the actions of the intermediate code generator.

Syntax-Directed Definitions Generalization of a CFG in which each grammar symbol has an associated set of attributes, partitioned into two subsets called the synthesized and inherited attributes of that grammar symbol.

Form of a Syntax Directed Definition For each grammar production A  α has associated with it a set of semantic rules of the form b:= f(c 1, c 2,……..,c k ), where f is a function, and either b is a synthesized attribute of A and c 1, c 2,……..,c k are attributes belonging to the grammar symbols of the production, or, b is an inherited attribute of one of the grammar symbols on the right side of the production, and c 1, c 2,……..,c k are attributes belonging to the grammar symbols of the production.

Synthesized & Inherited Attributes An attribute can represent anything we choose: a string, a number, a type, a memory location, or whatever. The value of a synthesized attribute at a node is computed from the values of attributes at the children of that node in the parse tree. The value of an inherited attribute is computed from the values of attributes at the siblings and parent of that node.

Synthesized & Inherited Attributes

Types of inherited dependencies 1. Inheritance from parent to siblings 2. Inheritance from sibling to sibling (b) 3. Sibling inheritance via sibling pointers (c)

Cont… There are two notations for associating semantic rules with productions 1.Syntax directed definitions - CFG in which each grammar symbol has an associated set of attributes, (synthesized inherited attributes) 2. Translation schemes. The scheme associates to each production rule of the source context free language a production rule of the target context free language A → α,β where α is called the source element of the rule. β is called the translation element of the rule

Conceptual views of syntax directed definitions I/P P.Tree E.Order for Semantic rules D.Graph

Dependency Graph It is a Directed graph representing dependencies of several objects towards each other. It is possible to derive an evaluation order or the absence of an evaluation order that respects the given dependencies from the dependency graph.

Definition of a dependency graph Given a set of objects S and a Transitive relation R= SxS with modeling a dependency “a needs b evaluated first”, the dependency graph is a graph G=(S,T).

Example A = B+C; B = 5+D; C=4; D=2; then S = A,B,C,D and R = (A,B),(A,C),(B,D). You can derive this relation directly: A depends on B and C, because you can add two variables if and only if you know the values of both variables.

Syntax Directed Semantics (semantics directed syntax) Attributes are associated directly with the grammar symbols of the language.(T & N) If X is a G & a is an attribute associated to X, then we can write X.a Given collection of attributes a 1,a 2,..a k, the principle of syntax directed semantics implies that for each grammar rule X 0  X 1 …X n (X 0 is a Non terminal & X i are arbitrary symbols). Each relationship is specified by an attribute equation or semantic rule of the form: X i.A j =f ij (X 0.a 1,...X 0.a k,X 1.a 1,…..X 1.a k,….X n.a k,…..X n.a k )

Cont… An attribute grammar for the attributes a 1,….a k is the collection of all such equations, for all the grammar rules of the language. Attribute grammars are written in tabular form in which each grammar rule listed with the set of attribute equations or semantic rules associated to that rule. Grammar RuleSemantic rules Rule 1Associated attribute equations,,,, Rule nAssociated attribute equations

Example Number  number digit|digit Digit  0|1|2|3|4|5|6|7|8|9 G (ex) digit  0 implies that digit has a value 0, this can be expressed by an attribute equation digit.val=0 and we associate this equation with the rule digit  0. (ex) number  digit (A.EQN) number.val = digit.val (for a single digit) number  number digit number1  number2 digit (distinguishing the numbers) Consider the number 34, LMD of this number is: Number  number digit  digit digit  3 digit  34 We can also multiply the value of number2 by 10 and add the value of the digit (34=3*10+4) Then the attribute equation will be :Number1.val=number2.val *number3.val +number4.val

cont… GrammarAttribute Equations Number1  number2 digit (34) Number1.val=number2.val *number3.val +number4.val Number  digit Number.val=digit.val Digit  0 Digit.val=0 Digit  1 Digit.val=1 Digit  2 Digit.val=2 Digit  3 Digit.val=3 Digit  4 Digit.val=4 Digit  5 digit.val=5 Digit  6 Digit.val=6 Digit  7 Digit.val=7 Digit  8 Digit.val=8 Digit  9Digit.val=9

Synthesized Attributes A syntax directed definition that uses synthesized attributes exclusively is said to be an S-attributed definition. A parse tree for an S-attributed definition can always be annotated by evaluating the semantic rules for the attributes at each node bottom up, from the leaves to the root.

L-Attribute

Annotate Parse Tree