COP5621 Exam 3 - Spring 2005

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

Lecture 14 Sections Mon, Mar 2, 2009

COP4020 Spring 2011 Midterm Exam

Semantic Analysis Attribute Grammars

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

Syntax-Directed Translation Part I

Syntax-Directed Translation Part II

Summary: Semantic Analysis

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Syntax-Directed Translation

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

COP5621 Exam 4 - Spring 2005

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Chapter 6 Intermediate Code Generation

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

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

Programming Languages

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

Using an LALR(1) Parser Generator

Syntax-Directed Translation. Introduction

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

More On Syntax Directed Translation

5. Syntax-Directed Definitions & Type Analysis

Syntax-Directed Translation

Static Checking and Type Systems

UNIT IV INTERMEDIATE CODE GENERATION

Principles of Programming Languages

[Syntax Directed Translation] Bikash Balami

Intermediate Representations

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

Compiler Construction Assignment 3 Spring 2018

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

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

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

Syntax Directed Translation

Compiler Lab. Introduction to tools Lex and Yacc

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

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

Syntax Directed Translation

Abstract Syntax Trees Synthetic and Inherited Attributes

Syntax-Directed Translation

Syntax-Directed Translation

Question Bank. 10CS63:Compiler Design

Chapter 4 :: Semantic Analysis

Time : 1 Hour Max Marks : 30

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

Semantic Analysis with Attribute Grammars Part 3

Semantic Analysis. Role of Semantic Analysis

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

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

QUESTIONS RELATED TO UNIT I, II And III

Question Points Score

Static and Dynamic Semantics

Principles of Programming Languages

A Simple Syntax-Directed Translator

CS2210: Compiler Construction. Code Generation

Module 26 Backpatching and Procedures

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

4. Semantic Processing and Attributed Grammars

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

COP4020 Programming Assignment 2 Spring 2011

Problem Score Max Score 1 Syntax directed translation & type

Syntax and Grammars 1 / 21

CSE302: Compiler Design

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Compiler Design Aug 1996

Final Exam Review Questions

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

CS 314 Principles of Programming Languages. Lecture 9

CSCE 531 Spring 2009 Final Exam

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

Semantic Analysis and Intermediate Code Generation

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Chapter 2: Syntax Directed Translation and YACC

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

LECTURE NOTES ON COMPILER DESIGN P a g e 2

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

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

LR Parsing LALR Parser Generators

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

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Evaluation of Semantic Actions in Predictive Non- Recursive Parsing

Class Information ANNOUCEMENTS

VALLIAMMAI ENGINEERING COLLEGE

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

2068 (I) Attempt all questions.

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

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Syntax-Directed Translation. Lecture 14

Compiler Theory. (Semantic Analysis and Run-Time Environments)

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

Chapter 4. Action Routines

MIDTERM EXAMINATION - CS130 - Spring 2003

Parsing Techniques. AST Review. AST Data Structures. Implicit AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

CPS 506 Comparative Programming Languages. Syntax Specification

COP4020 Programming Assignment 2 - Fall 2016

Transcription:

COP5621 Exam 3 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full cred it and helpful for partial credit). You can collect 100 points in total for this exam. A bonus question for an addit ional 15 points is included. If the total number of points for this exam exceed 100, the excess points are ca rried over to the next exams. 1. Match the terms below with the given sentences so as to best complete each sentence. Use no term more than once. Some terms will go unused. (10 points) (1) one (8) syntax-directed definitions (2) two (9) semantic actions (3) three (10) abstract syntax trees (4) four (11) translation schemes (5) attributes (12) parse trees (6) synthesized (13) inherited (7) S (14) L (a) There are two notations for associating semantic rules and actions with grammar productions: and. (b) In translation schemes, (c) Quadruples are an implementation of are inserted within the right-hand sides of productions. -address code statements. (d) The larger class of -attributed definitions enable translations to be performed without explicit construction of a parse tree. (e) The values of attributes are passed upwards in a parse tree. 2. Give one reason why compilers generate intermediate code (rather than generating machine code directly). (5 points) 1

3. Given the following grammar for an expression: represent the string E E + T E E - T E T T T * F T T / F T F F ( E ) F id a*(b-c)+(b-c) as a parse tree, an abstract syntax tree, and a DAG that is minimal. (15 points) 2

4. Below is a grammar expressed in Yacc defining the syntax of a decimal constant. Add semantic actions to calculate the value of the constant and print the value to standard output. You may assume that Yacc s stack type (YYSTYPE) is int.(15 points) decimal : digits digits digit : 0 1... 9 : digits digit digit 5. What are the three proposed evaluation methods for semantic rules to decorate parse trees? (5 points) 3

6. Consider the following syntax-directed definition: D T L L.type := T.type T int T.type := int T real T.type := real T ^T 1 T.type := pointer(t 1.type) L V V.type := L.type L L 1, V L 1.type := L.type V.type := L.type V id enter(id.entry, V.type) V id [ T ] enter(id.entry, array(v.type, T.type)) (a) Identify the synthesized and inherited attributes. (5 points) (b) Show the annotated parse tree for the sentence. (15 points) ^real id 1, id 2 [int] (c) Is the syntax-directed definition S-attributed? Is it L-attributed? (5 points) 4

7. Consider the following syntax-directed definition: E E 1 + T E T T num E.val := E 1.val + T.val E.val := T.val T.val := num.val (a) Eliminate left recursion and show the converted translation scheme. (10 points) (b) Write a recursive descent parser for the converted translation scheme. (5 points) 5

8. Suppose we build an interpreter in Yacc to evaluate integer and floating point expressions. The following fragment is part of this Yacc specification: %union { Symbol *sym struct { int type union { int i float f val rec %token <sym> ID %type <rec> expr %% expr : expr + expr {... ID { if ($1->type == Tint) { $$.type = Tint $$.val.i = $1->val.i else { $$.type = Tfloat $$.val.f = $1->val.f The expr nonterminal has a type and a val field containing the type and value, respectively. Give the Yacc semantic actions to evaluate the sum + on integers and floats.(10 points) 6

9. (bonus question). (15 points) The following syntax-directed definition uses the backpatching technique to generate short-circuit code for Boolean expressions. The right-hand side of an assignment can be a Boolean expression. A 1 is to be stored in the assigned variable when the Boolean expression evaluates to true and a 0 is to be stored when the expression is false. S if E then M S backpatch(e.t, M.q) backpatch(e.f, nextquad) S id := E backpatch(... ) emit(id.place := 0) emit(goto nextquad+1) backpatch(... ) emit(id.place := 1) E E 1 or M E 2... E E 1 and M E 2... E ( E 1 )... E not E 1... E id E.t := makelist(nextquad) emit(if id.place goto ) E.f := makelist(nextquad) emit(goto ) M ɛ M.q := nextquad (a) Complete the definition by completing the two backpatch operations in the assignment operation and the semantic rules for the not operator. (10 points) (b) Generate intermediate code for if not (a or b) then c := (a and b) The first three-address statement is numbered 0 (i.e. nextquad returns 0 the first time). You may assume that id.place represents the identifier s name. Use additional sheets when necessary. (5 points) 7