1 440 Midterm 2012 fall. Given the following two classes: public class A { void doSomething () { System.out.println("A"); } public class B extends A{

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
1 Compiler Construction Intermediate Code Generation.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Environments and Evaluation
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
1 Genericity Parameterizing by Type. 2 Generic Class One that is parameterized by type  Works when feature semantics is common to a set of types On object.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
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.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Topic 3 The Stack ADT.
COP4020 Programming Languages
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
CSC 338: Compiler design and implementation
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
Types in programming languages1 What are types, and why do we need them?
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Languages and Compilers
Introduction CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
CMSC 330: Organization of Programming Languages Operational Semantics.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
BY: JAKE TENBERG & CHELSEA SHIPP PROJECT REVIEW: JGIBBERISH.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Click to edit Master text styles Stacks Data Structure.
System Software Theory (5KS03).
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
CS 536 / Fall 2017 Introduction to programming languages and compilers
CPSC 388 – Compiler Design and Construction
Review: Compiler Phases:
CSE401 Introduction to Compiler Construction
Compilers B V Sai Aravind (11CS10008).
CMPE 152: Compiler Design August 21/23 Lab
A type is a collection of values
Presentation transcript:

1 440 Midterm 2012 fall

Given the following two classes: public class A { void doSomething () { System.out.println("A"); } public class B extends A{ void doSomething () { System.out.println("B”); } The method doSomething() is 1.Overloaded; 2.Overridden. Answer: overidden

3 Answer: d

4 Answer: none of the above Which of the following is not part of a compiler? – Scanner; – Parser; – Code generator; – Optimizer; – None of the above.

5 Compilation and execution Output DataInput Data Target Program Abstract Program (Optimized) Parse Tree Symbol Table Source program Code Optimization Semantic Analysis Loader / Linker Code Generation Computer Lexical Analysis (scanning) Syntactic Analysis (parsing) compiler Token Sequence Abstract Program (Intermediate code) Object Program (Native Code)

Answer: None of the above 6

Answer: d Syntax for lambda expression – Variable – Application – Abstraction 7

8. What is the result of running (+ (* 1 2) (* 3 4)) in Scheme language? (a)14 (b)10 (c)11 (d)24 (e)none of the above 8

Answer: c Notice that eta conversion is not applicable here It is λx.fx  f 9

Answer: f 10

(2 points) Write the result of running the following program: (reduce concat '( ) '((1 2 3) (4 5) (6 7 8))) 11

Answer: d 12

Answer: a 13

Answer: c 14

Answer: a 15

Answer: b 16

Answer:b 17

Answer: b 18

19 Answer: d

20 Answer: b

21 True or false There is more than one way to reduce a lambda expression. One programming language can support more than one programming paradigm. One programming paradigm can be supported by more than one language. Abstract Data Type defines a data type using a set of values of the type.

22 Data Type and Abstract Data Type Data type – Data values – Operations on the data Abstract – Focus on some details while ignore others. –Simplified description of objects –Emphasis significant information only –Suppress irrelevant information Abstract Data Type – Focus on operations, ignore the concrete data representation. ADT

23 ADT Example Stack(); Stack push(Object item); Stack pop(); Object peek(); boolean empty(); for all [s: Stack; i: Object] Stack().empty() = true s.push(i).empty()=false Stack().pop()=error s.push(i).pop()=s; Stack().peek()=error s.push(i).peek()=i; (Note that this is not the Stack in Java) ADT defines a data type in terms of operations instead of the data The implementation of Stack data type can be an Array, or a Vector, or other data structure This is the idea of information hiding – Implementation is not relevant to the user of the data type – Implementation can be changed without affecting other parts of the system. The meaning of ADT can be specified in terms of the relationships between the operators, independent of the data representation. ADT

24 – Dynamic binding is the binding of the method name to the method definition dynamically at compilation time. answer: false. Dynamic binding happens at run time Recall the example class Cat extends Animal { void talk() { System.out.println("Meow."); } } class Interrogator { static void makeItTalk(Animal subject) { subject.talk(); } }

25 Given the following code fragment: Vector v = new Vector(); v.add("test"); String a = (String) v.get(0); Integer b = (Integer) v.get(0); – Whether the code will pass the compilation correctly? Answer: it will compile correctly. But it will have runtime error Type cast error Violate the strong typing rule

(1 point) Rewrite the λ expression λx.xz λy.xy by adding parentheses around sub-expressions so that there is no ambiguity. For instance, λx.xy should be rewritten as λx.(xy) – λx.((xz) (λy.(xy))) (1 point) Find and circle all free variables in the following λ expression (λx. x z) λy. w λw. w y z x (λx. x z) (λy. (w (λw. (((w y) z) x)))) 26

(4 points) Prove not (not true) = true using λ calculus, supposing the encoding of not, true, and false are defined using λ expressions as below: – not = λx.((x false) true) – true = λx.λy.x – false = λx.λy.y N=λx.xFT T=λxy.x F=λxy.y N(NT)  N(TFT)  N(F)  FFT  T  (TFT)FT  FFT  T N(NT)  (NT)FT  (TFT)FT  FFT  T 27 not (not true) = λx.((x false) true) (not true) = ((not true) false) true = ((λx.((x false) true) true) false) true = (((true false) true) false) true = ((((λx.λy.x) false) true) false) true = (((λy.false) true) false) true = ((false) false) true = ((λx.λy.y) false) true = (λy.y) true

(2 points) Prove if false then x else y = y using λ calculus, supposing if a then b else c = a b c, true and false are defined the same as in the previous question. F=λxy.y Fxy  y (2 points) Write the result of running the following program: (reduce concat '( ) '((1 2 3) (4 5) (6 7 8))) 28

29 a(X,X):-b(X,X). a(X,Z):-b(X,Y), a(Y,Z). b(a, b). b(b, c). b(b, d). b(a, d) | ?-a(a, X). Answer: no.

30 a(X, [ ], [X]). a(X, [Y|L], [X, Y|L]) :- X =< Y. a(X, [Y|L], [Y|IL]) :- X > Y, a(X, L, IL). | ?-a(2, [2, 3, 4], X) Answer: [2,2,3,4] It is an insertion.

31 static max(S x, T y){ return (x.compareTo(y) < 0 )? y:x; } Answer: static > T max(T x, T y){ return (x.compareTo(y) < 0 )?y:x; }

32 e1 Joe Smith Andrew Wang e2 Steve Miller

33 first name is e1 Joe Smith Andrew Wang first name is Steve

34 HI e1 Steve HI

35 Hello, Found you! e1 Hello, Found you!