Chapter 5 Syntax Directed Translation

Similar documents
[Syntax Directed Translation] Bikash Balami

Syntax-Directed Translation

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

Syntax Directed Translation

COP5621 Exam 3 - Spring 2005

Syntax-Directed Translation Part II

Abstract Syntax Tree

Syntax-Directed Translation Part I

Lecture 14 Sections Mon, Mar 2, 2009

Syntax Directed Translation

Semantic Analysis Attribute Grammars

Principles of Programming Languages

More On Syntax Directed Translation

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Syntax-Directed Translation

Semantic Analysis with Attribute Grammars Part 4

A Simple Syntax-Directed Translator

Semantic Analysis with Attribute Grammars Part 3

Syntax-Directed Translation

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

CSCI Compiler Design

CSCI Compiler Design

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

We now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a

Program Assignment 2 Due date: 10/20 12:30pm

CS 406: Syntax Directed Translation

Principles of Programming Languages

Chapter 4 - Semantic Analysis. June 2, 2015

5. Syntax-Directed Definitions & Type Analysis

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos

Evaluation of Semantic Actions in Predictive Non- Recursive Parsing

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

UNIT IV INTERMEDIATE CODE GENERATION

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

CSCI Compiler Design

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

Summary: Semantic Analysis

A simple syntax-directed

Abstract Syntax Trees Synthetic and Inherited Attributes

QUESTIONS RELATED TO UNIT I, II And III

More Assigned Reading and Exercises on Syntax (for Exam 2)

Syntax-Directed Translation. Introduction

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

CSCI Compiler Design

Compiler Construction

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

CPS 506 Comparative Programming Languages. Syntax Specification

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Semantic Analysis. Role of Semantic Analysis

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Compiler Construction

CSE302: Compiler Design

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Type Checking. Chapter 6, Section 6.3, 6.5

A programming language requires two major definitions A simple one pass compiler

Computer Programming: C++

LECTURE 3. Compiler Phases

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

Static and Dynamic Semantics

This book is licensed under a Creative Commons Attribution 3.0 License

Type Checking. Error Checking

Syntax-Directed Translation

Intermediate Code Generation Part II

4. Semantic Processing and Attributed Grammars

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Attribute Grammars. Attribute Grammars

As we have seen, token attribute values are supplied via yylval, as in. More on Yacc s value stack

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Abstract Syntax Trees & Top-Down Parsing

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

Programming Languages

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

Ambiguity and Errors Syntax-Directed Translation

1 Lexical Considerations

Question Bank. 10CS63:Compiler Design

Chapter 3. Describing Syntax and Semantics

COP4020 Spring 2011 Midterm Exam

Chapter 4 :: Semantic Analysis

Introduction to Parsing. Lecture 8

Compilers. Compiler Construction Tutorial The Front-end

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

2068 (I) Attempt all questions.

CS 4201 Compilers 2014/2015 Handout: Lab 1

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

Programming Lecture 3

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309

There is a level of correctness that is deeper than grammar. There is a level of correctness that is deeper than grammar

Partial Exam Compilation and Program Analysis (CAP) October, 24th, 2016 Duration: 2 Hours

Transcription:

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 5060: Compiler Design Discussion Chapter 5 Syntax Directed Translation Eng. Eman R. Habib May, 2014

2 Computer Architecture Discussion Exercise 1: Give an SDD to differentiate expressions such as x (3 x + x x) involving the operators + and, the variable x, and constants. Assume that no simplification occurs, so that, for example, 3 x will be translated into 3 1 + 0 x E E 1 + T E.t = E 1.t + T.t E.d = E 1.d + T.d E E 1 - T E.t = E 1.t - T.t E.d = E 1.d - T.d E T E.t = T 1.t E.d = T 1.d T T 1 F T.t = T 1.t F.t T.d = T 1.t F.d + F.t T 1.d T T 1 / F T.t = T 1.t / F.t T.d = ( F.t T 1.d - T 1.t F.d ) / F.t F.t T F T.t = F.t T.d = F.d F (E) F.t = ( E.t ) F.d = ( E.d ) F digit F.t = digit.value F.d = 0 F id F.t = id.lexem F.d = 1 Exercise 2: Below is a grammar for expressions involving operator + and integer or floating-point operands. Floating-point numbers are distinguished by having a decimal point. E E + T T T num.num num a) Give an SDD to determine the type of each T and expression E. b) Extend your SDD of (a) to translate expressions into postfix notation. Use the unary operator inttofloat to turn an integer into an equivalent float. a) E E 1 + T E T T num.num T num if (E 1.type = int & T.type = int) then E.type = int else E.type = float E.type = T.type T.type = float T.type = int

3 Computer Architecture Discussion b) E E 1 + T E T T num 1.num 2 T num if (E 1.type = int & T.type = int) then E.type = int E.t = E 1.t T.t + else if E 1.type = int then E.type = float E.t = inttofloat(e 1.t) T.t + else if T.type = int then E.type = float E.t = E 1.t inttofloat (T.t) + else E.type = float E.t = E 1.t T.t + E.type = T.type E.t = T.t T.type = float T.t = num 1.value. num 2.value T.type = int T.t = num.value Exercise 3: Rewrite the underlying grammar in the syntax-directed definition of Example 5.10 so that type information can be propagated using synthesized attributes alone. D int A D float B A A, id A id B B, id B id addtype(id.entry, int) addtype(id.entry, int) addtype(id.entry, float) addtype(id.entry, float)

4 Computer Architecture Discussion Exercise 4: Suppose declarations are generated by the following grammar. D id L L, id L :T T int float Construt a translation scheme to enter the type of each identifier into the symbol table. D id L L, id L 1 L :T T int T float addtype(id.entry, L.type) L.type = L 1.type addtype(id.entry, L 1.type) L.type = T.type T.type = int T.type = float Exercise 5: Turn the L-attributed SDD of the following figure into an SDT. S L {B.ps = L.s} B {S.ht = B.ht} L ϵ {L.s = 10} B {B 1.ps = B.ps} B 1 {M.i = B.ps} M {B 2.ps = M.s} B 2 {B.ht = max(b 1.ht, B 2.ht)} B { B 1.ps = B.ps} B1 sub {N.i = B.ps} N { B 2.ps = N.s} B 2 {B.ht = disp(b 1.ht, B 2.ht)} B text {B.ht = text.h X B.ps} M ϵ {M.s = M.i} N ϵ {N.s = shrink(n.i)}

5 Computer Architecture Discussion Exercise 6: In this problem you are asked to develop a syntax-directed translation scheme to evaluate binary strings. First you are asked to develop the CFG and then the corresponding attribute grammar illustrating its operation via an example. a) Develop a context-free grammar (CFG) that describes all the binary strings (over the alphabet {0,1}) whose decimal values are multiples of 4. Assume that there can be leading 0s digits but any input sequence that corresponds to the decimal value zero is not in the language, i.e. 0000 but 00100 is in the language. b) For the CFG developed above define the attributes for the non-terminal symbols and terminal symbols of the grammar and develop semantic rules that can be used to compute as the value of an attribute at the start symbol, the numeric decimal value of the input string. For instance for the string 00100 the decimal values of an attribute you need to defined and that is associated with the start symbol should be the value 4. c) Using the attribute grammar developed in b) show the annotated parse tree for the input 11100 illustrating the dependences between the evaluation of the attributes in this particular parse tree. S List 100 R List.pos = 3 S.value = (List.value + 4) * 2 R.Bits R R 1 0 R.Bits = R 1.Bits + 1 R Ԑ R.Bits = 0 List List 1 Bit List 1.pos = List.pos + 1 Bit.pos = List.pos List.value = List 1.value + Bit.value List Bit Bit.pos = List.pos List.value = Bit.value Bit 0 Bit.value = 0 Bit 1 Bit.value = 2 Bit.pos *2 0 =28 R Bits =0 Ԑ

6 Computer Architecture Discussion Exercise 7: Consider the following syntax-directed definition over the grammar defined by G = ({S, A, Sign}, S, {,, -, +, n }, P) with P the set of production and the corresponding semantic rules depicted below. There is a special terminal symbol n that is lexically matched by any string of one numeric digit and whose value is the numeric value of its decimal representation. For the non-terminal symbols in G we have defined two attributes, sign and value. The nonterminal A has these two attributes whereas S only has the value attribute and Sign only has the sign attribute. S A Sign S.val = A.val; A.sign = Sign.sign; print(a.val); Sign + Sign.sign = 1 Sign - Sign.sign = 0 A n A.val = value(n) A A1, n A1.sign = A.sign; if(a.sign = 1) then A.val = max (A1.val,value(n)); else A.val = min (A1.val,value(n)); For this Syntax-Directed Definition answer to the following questions: a) Explain the overall operation of this syntax-directed definition and indicate (with a brief explanation) which of the attributes are either synthesized or inherited. b) Give an attributed parse tree for the source string 5,2,3- and evaluate the attributes in the attributed parse tree depicting the order in which the at tributes need to be evaluated (if more than one order is possible indicate one.) c) Suggest a modified grammar and actions exclusively using synthesized attributes. Explain its basic operation. a) This syntax-directed definition computes the maximum or minimum of a sequence of integers depending on the suffix - or + respectively. As to the attributes, clearly the sign attribute for the Sign non-terminal is synthesized. The val attribute is also synthesized but the sign attribute for A is inherited as it flows from right-to-left (thus from top to bottom in the parse tree) for a production of A. b)

7 Computer Architecture Discussion c) The idea is to decouple at the top the two situations, computing a minimum or maximum based on the value of the Sign non-terminal. To accomplish this we have two very similar non-terminal symbols B and C as shown below, which replace the A terminal symbol. Note also the presence of two productions for S for the two computation functions, min and max and the absence of the non-terminal Sign. All attributes are now synthesized as shown in the figure below. S B + S.val = B.val; print(b.val) S C - S.val = C.val; print(c.val) B n B.val = value(n) B B 1, n B.val = max(b1.val,value(n)) C n C.val = value(n) C C 1, n C.val = min(c1.val,value(n))

8 Computer Architecture Discussion Exercise 8: Let synthesized attribute val give the value of the binary number generated by S in the following grammar. For example, on input 101.101, S.val = 5.625. S L.L L L LB B B 0 1 S L 1.L 2 S L L L 1 B L B B 0 B.val = 0 B 1 B.val = 1 S.val = L 1.val + (L 2.val/2^L 2.bit) S.val = L.val L.val = L 1.val 2 + B.val L.bit = L.bit+1 L.val = B.val L.bit = 1 باالعادة بعملها مناقشة واخر كومبايلر مناقشة اخر هي انو بما... فلسطين ترجعلنا و حقنا يرجعلنا هللا انو اختم فبدي 66 ال نكبتنا ذكرى أيار 15 بكرة انو وبما... أرضه على يرجع الجئ وكل... فلسطين كل موعدنا والعودة تجمعنا وفلسطين #انتماء #نكبة_فلسطين #nakbba_66 انو زكركو بدي اإلداري االعتقال إلسقاط اإلدرايين األسرى إلضراب 21 ال اليوم هو اليوم انو وبما... ندعيلهم انو نعملو بنقدر شي واقل مطالبهم ويحققو ينتصرو لحتى معهم نتضامن بيستنو األسرى #مي_وملح Best Wishes