Midterm Solutions COMS W4115 Programming Languages and Translators Wednesday, March 25, :10-5:25pm, 309 Havemeyer

Similar documents
Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

A Simple Syntax-Directed Translator

CSE 401 Midterm Exam 11/5/10

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

Principles of Programming Languages COMP251: Syntax and Grammars

This book is licensed under a Creative Commons Attribution 3.0 License

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

2.2 Syntax Definition

Midterm I (Solutions) CS164, Spring 2002

MIDTERM EXAMINATION - CS130 - Spring 2003

CSCE 314 Programming Languages

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)

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

CSE 401 Midterm Exam Sample Solution 2/11/15

Question Marks 1 /11 2 /4 3 /13 4 /5 5 /11 6 /14 7 /32 8 /6 9 /11 10 /10 Total /117

Part 5 Program Analysis Principles and Techniques

CSE 401 Midterm Exam Sample Solution 11/4/11

Syntax Analysis Part I

HW 3: Bottom-Up Parsing Techniques

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

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

Optimizing Finite Automata

CMSC 330 Practice Problem 4 Solutions

CS606- compiler instruction Solved MCQS From Midterm Papers

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Syntax Analysis Check syntax and construct abstract syntax tree

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

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

CMSC 330, Fall 2009, Practice Problem 3 Solutions

Question Points Score

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

LECTURE 17. Expressions and Assignment

CSCE 531 Spring 2009 Final Exam

afewadminnotes CSC324 Formal Language Theory Dealing with Ambiguity: Precedence Example Office Hours: (in BA 4237) Monday 3 4pm Wednesdays 1 2pm

Compiler Construction

UNIT IV INTERMEDIATE CODE GENERATION

Introduction to Syntax Analysis. Compiler Design Syntax Analysis s.l. dr. ing. Ciprian-Bogdan Chirila

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

Question Bank. 10CS63:Compiler Design

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

Properties of Regular Expressions and Finite Automata

MIDTERM EXAM (Solutions)

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Compiler Design Aug 1996

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Compiler Construction

Translator Design CRN Test 3 Revision 1 CMSC 4173 Spring 2012

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

Lexical Considerations

1 Lexical Considerations

ECE251 Midterm practice questions, Fall 2010

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

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

MA513: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 18 Date: September 12, 2011

More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

A simple syntax-directed

Lecture 7: Deterministic Bottom-Up Parsing

CS 314 Principles of Programming Languages. Lecture 3

Introduction to Parsing. Lecture 5

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

CSE 5317 Midterm Examination 4 March Solutions

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Compiler Construction

COMPILER DESIGN LECTURE NOTES

Formal Languages and Compilers Lecture VI: Lexical Analysis

Syntax. 2.1 Terminology

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Lecture 8: Deterministic Bottom-Up Parsing

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]

LANGUAGE PROCESSORS. Presented By: Prof. S.J. Soni, SPCE Visnagar.

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

VALLIAMMAI ENGINEERING COLLEGE

Principles of Compiler Design

CSE 12 Abstract Syntax Trees

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

CS 314 Principles of Programming Languages

VIVA QUESTIONS WITH ANSWERS

Lexical Scanning COMP360

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

2068 (I) Attempt all questions.

CSCI312 Principles of Programming Languages!

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Formal Languages and Automata Theory, SS Project (due Week 14)

MIDTERM EXAMINATION. CSE 130: Principles of Programming Languages. Professor Goguen. February 16, points total

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Introduction to Compiler Construction

CS 415 Midterm Exam Spring SOLUTION

UVa ID: NAME (print): CS 4501 LDI Midterm 1

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00

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

Lexical Considerations

GUJARAT TECHNOLOGICAL UNIVERSITY

Problem Score Max Score 1 Syntax directed translation & type

Transcription:

Department of Computer cience Columbia University Midterm olutions COM W4115 Programming Languages and Translators Wednesday, March 25, 2009 4:10-5:25pm, 309 Havemeyer Closed book, no aids. ach question is worth 20 points. Question 5(c) is extra credit, 10 points. 1. Briefly explain the essential difference between a) call-by-value and call-by-reference. How are parameters passed in C and Java? In call-by-value, the actual parameter is evaluated or copied; its value is placed in the location of the corresponding formal parameter of the called procedure. In call-by-reference, the address of the actual parameter is passed to the callee as the value of the corresponding formal parameter. C and Java use call-by-value. b) static scope and dynamic scope. How is scoping done in C and Java? cope specifies the textual region of a program in which there is an active association (binding) between a name and the object it represents. tatic scoping associates the use of a name with the closest lexically enclosing declaration. Dynamic scoping chooses the most recent active declaration at runtime. C and Java use static scoping. 2. Java compilation. a) Draw a block diagram showing how programs are compiled and executed in Java. 1

source program Translator Intermediate program input Java Virtual Machine output Java Compiler b) What is a Java just-in-time compiler? The intermediate program from the Java translator is a sequence of architecturally neutral bytecodes that are interpreted by the Java virtual machine. A Java just-in-time compiler translates the bytecodes into an equivalent sequence of native code for the target machine in order to achieve faster run-time performance. 3. Let L be the set of strings of the form abxba where x is a string of a s, b s, and c s that does not contain ba as a substring. a) Write a regular expression for L. Let R = ab(a b*c)*b+a. b) how how your regular expression generates the string ababcba. The prefix ab of R generates the prefix ab of the string. Then (a b*c)* generates abc. Finally b+ generates b and the final a of R generates the final a of the string. 2

c) Construct a deterministic finite automaton for L. a,c b a b b a 0 1 2 3 4 c All unspecified transitions are to a dead state. d) how how your automaton processes the input ababcba. a b a b c b a 0 1 2 2 3 2 3 4 4. Consider the context-free grammar G: + * a. a) how that G is ambiguous by constructing all parse trees for a + a * a. + * a * + a a a a a 3

b) Construct an unambiguous grammar for L(G) in which + is left associative, * is nonassociative and of higher precedence than +. (1) + T (2) T (3) T a * a (4) T a Draw the parse tree in your grammar for the input string a + a * a. + T T a * a a c) Construct an LR(1) parsing table for your grammar. The sets of LR(0) items for the augmented grammar are: I 0 : + T T T a * a T a I 1 : + T I 2 : T 4

I 3 : T a * a T a I 4 : + T T a * a T a I 5 : T a * a I 6 : + T I 7 : T a *a The parsing table is constructed from these sets of items. ach set of items corresponds to a state. The FOLLOW sets for the nonterminals are NONTRMINAL FOLLOW + $ T + $ The action and goto tables are shown below. Blank entries are errors. Note all entries are uniquely defined so the grammar is LR(1), and hence unambiguous. tate Action Goto a + * $ T 0 s3 1 2 1 s4 acc 2 r2 r2 3 r4 s5 r4 4 s3 6 5 s7 6 r1 r1 7 r3 r3 5. yntax-directed translation. a) Construct an DT that maps postfix expressions containing the digits 0, 1,, 9 and the binary operators and % into equivalent infix expressions. 5

Here is a DT using a synthesized attribute.v of type string for the nonterminal. In the semantic rules, we have used juxtaposition as the concatenation operator. 1 2 - {.v = "(" 1.v " - " 2.v ")"; } 1 2 % {.v = "(" 1.v " % " 2.v ")"; } 0 {.v = "0"; } 1 {.v = "1"; } 2 {.v = "2"; } 3 {.v = "3"; } 4 {.v = "4"; } 5 {.v = "5"; } 6 {.v = "6"; } 7 {.v = "7"; } 8 {.v = "8"; } 9 {.v = "9"; } b) how how your DT translates the expression 123 %. Here is an annotated parse tree for 123 % with the value of.v shown at each node. The output is the infix expression (1%(2-3)), the value of.v at the root of the tree...v=(1%(2-3)) %.v = 1.v = (2-3) 1.v = 2.v = 3-2 3 6

c) Modify your DT so that it uses the fewest possible number of parentheses in the output. Here is a modified DT that uses two synthesized attributes,.v and.p, for the nonterminal in the productions..v is the infix string associated with the nonterminal and.p is an integer giving the precedence level of the operator associated with. We assume the precedence level of % is 2, and is 1. For convenience, we set the precedence level of a digit to 3. To determine whether we need to put parentheses around a subexpression operand, we use the following rule. uppose we have the parse tree node: 1 2 op Then we put parentheses around 1.v if 1.p is less than the precedence level of op; we put parentheses around 2.v if 2.p is less than or equal to the precedence level of op. Otherwise, we do not add parentheses. The parentheses are there to make sure we evaluate the infix expression in the same order as the postfix expression. Here is the modified DT: 1 2 - { if ( 2.p == 1) 2.v = "(" 2.v ")";.v = 1.v "-" 2.v;.p = 1; } 1 2 % { if ( 1.p == 1) 1.v = "(" 1.v ")"; if ( 2.p 2) 2.v = "(" 2.v ")";.v = 1.v "%" 2.v;.p = 2; } 0 {.v = "0";.p = 3; } 1 {.v = "1";.p = 3; } 2 {.v = "2";.p = 3; } 3 {.v = "3";.p = 3; } 4 {.v = "4";.p = 3; } 5 {.v = "5";.p = 3; } 6 {.v = "6";.p = 3; } 7 {.v = "7";.p = 3; } 8 {.v = "8";.p = 3; } 9 {.v = "9";.p = 3; } 7

Here is the annotated parse tree for the input 123-% using the modified DT:.v = 1%(2-3).p = 2.v = 1.v = 2-3.p = 3.p = 1 % 1.v = 2.v = 3.p = 3.p = 3-2 3 8