CS 152: Programming Language Paradigms April 28 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

Slides:



Advertisements
Similar presentations
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Advertisements

Programming Languages and Paradigms
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
What is Concurrent Programming? Maram Bani Younes.
Programming Languages and Paradigms Object-Oriented Programming.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Object-Oriented Implementation of Reconciliations M.Sc. Seminar Talk by Costa Shapiro under supervision of Prof. Shmuel Katz Computer Science – Technion.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 149: Operating Systems March 3 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 10 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Object-Oriented Programming Chapter Chapter
CS 153: Concepts of Compiler Design November 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CS 146: Data Structures and Algorithms June 11 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 153: Concepts of Compiler Design September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design November 19 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
ISBN Chapter 12 Support for Object-Oriented Programming.
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
1 CS162: Introduction to Computer Science II Abstract Data Types.
CS 152: Programming Language Paradigms April 23 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Data Abstraction: The Walls
CS 153: Concepts of Compiler Design October 17 Class Meeting
CS 153: Concepts of Compiler Design October 5 Class Meeting
Chapter 3: Using Methods, Classes, and Objects
CS 153: Concepts of Compiler Design November 30 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
What is Concurrent Programming?
Multithreaded Programming
CS 144 Advanced C++ Programming May 7 Class Meeting
CS 151: Object-Oriented Design October 8 Class Meeting
Presentation transcript:

CS 152: Programming Language Paradigms April 28 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 2 Pass by Name  Originally implemented by Algol 60 as a way to do inline procedures. Replace the name of a formal parameter used within a procedure by the text of the actual parameter.  A lazy evaluation mechanism. The actual parameter is not evaluated until the name of the corresponding formal parameter is encountered at run time. _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 3 Pass by Name, cont’d  Suppose C used pass by name. What happens to array a ? int i; int a[2]; void inc(int x) { i++; x++; } int main() { i = 0; a[0] = 0; a[1] = 1; inc(a[i]); return 0; } int i; int a[2]; void inc(int x) { i++; a[i]++; } int main() { i = 0; a[0] = 0; a[1] = 1; inc(a[i]); return 0; } a[0] = 0 a[1] = 2 Pass-by-name parameters are evaluated as small functions called “thunks”. A thunk executes in the context of the caller.

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 4 Pass by Name, cont’d  Pass by name: What value will be printed? int i; int p(int y) { int j = y; i++; return j + y; } void q() { int j = 2; i = 0; printf("%d\n", p(i + j)); } main() { q(); } int i; int p(int y) { int j = i + j; i++; return j + i + j; } void q() { int j = 2; i = 0; printf("%d\n", p(i + j)); } main() { q(); }

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 5 Pass by Name, cont’d  Pass by name: Does this swap procedure work? void swap(int x, int y) { int temp = x; x = y; y = temp; } int i; int j; swap(i, j); int i; int a[10]; swap(i, a[i]); int temp = i; i = j; j = temp; int temp = i; i = a[i]; a[i] = temp; Yes! Oops!

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 6 Data Types  Recall that a data type specifies: The collection of values that belong to the type. The operations that can be performed on those values. _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 7 Data Types, cont’d  In a strongly typed language, variables have types.  Types are checked for assignment and comparison compatibility at compile time by the parser.  Most type errors are caught at compile time, but some aren’t caught until run time.  Strongly typed languages lead to “safer” programs but often place greater burdens on the programmers.

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 8 Logical and Physical Forms  Data types have a logical and a physical form.  The logical form embodies the concept of the data type.  Example: The concept of an array. A data structure consisting of a collection of components all of the same type, where each component is uniquely accessed via an integral index value. _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 9 Logical and Physical Forms, cont’d  The physical form is the implementation of the data type within a computer program.  Example: Implement an array either as: A contiguous block of memory where the index value is an offset from the starting address of the block to the desired component. A sparse matrix where an index value accesses a pointer to the desired component in memory. _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 10 Abstract Data Types  An abstract data type (ADT) is the realization of a data type in software.  The interface of the ADT is defined in terms of a collection of values of the type and the set of operations.  The implementation details are hidden from the users of the ADT through a concept known as encapsulation. _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 11 Encapsulation  Encapsulation allows the design and implementation details of an ADT to change without affecting its users, as long as the interface does not change.   Programmers who use an ADT should only think in terms of the operations on the data type without knowledge of the implementation. _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 12 Language Support for ADTs  Modern languages support programmers to create ADTs.  Allow programmers to define data type and its operations, with encapsulation and information hiding mechanisms to insulate users from implementation details. Build ADTs from previously-defined types.  Provide language constructs that enable all the implementation details of an ADT to be defined in one place and which expose only the interfaces to users.

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 13 Key Design Goals of an ADT  Modifiability  Reusability  Security _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 14 Example ADT: Logical Specification of a List  Maintain an ordered collection of elements. Primitive or a reference type elements. All elements will have the same type.  Access the elements of a list either Sequentially, one after another starting from a given element and ending with another given element. Randomly, via each element’s index.  Each list has a size, which is the number of elements that it contains. In particular, the empty list has size 0.

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 15 List Operations  Return the size of the list.  Add an element to the end of the list.  Clear the list (remove all of its elements).  At a given position of the list: Insert an element. Retrieve the element. Modify the element. Remove (and return) the element. _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 16 List Implementation  A list can be implemented as an array or as a linked list.  Encapsulate and hide the implementation from list users (other programmers). _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 17 Sample List Implementation in Java  Implementation in package ListImplementation.  Interface List provides the interface for list users.  Implement as an array. _ package ListImplementation; public interface List { int size(); void add(AnyType element); void insert(int index, AnyType element); AnyType get(int index); void set(int index, AnyType element); AnyType remove(int index); void clear(); }

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 18 Implement as an Array package ListImplementation; public class ListAsArray implements List { private static final int INITIAL_ALLOCATION = 10; private int size; private AnyType data[] = (AnyType[]) new Object[INITIAL_ALLOCATION]; private int currentAllocation() { return data.length; } private void doubleAllocation() { AnyType newData[] = (AnyType[]) new Object[2*data.length]; System.arraycopy(data, 0, newData, 0, data.length); data = newData; }... }

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 19 Implement as an Array, cont’d package ListImplementation; public class ListAsArray implements List public int size() { return size; public void add(AnyType newElmt) { if (size >= currentAllocation()) doubleAllocation(); data[size++] = newElmt; }... }

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 20 Implement as an Array, cont’d package ListImplementation; public class ListAsArray implements List public void insert(int index, AnyType newElmt) { if ((index = size)) { throw new IndexOutOfBoundsException(); } if (size >= currentAllocation()) doubleAllocation(); for (int i = size-1; i >= index; i--) { data[i+1] = data[i]; } data[index] = newElmt; size++; }... }

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 21 Implement as an Array, cont’d package ListImplementation; public class ListAsArray implements List public AnyType get(int index) { if ((index = size)) { throw new IndexOutOfBoundsException(); } return data[index]; public void set(int index, AnyType newElmt) { if ((index = size)) { throw new IndexOutOfBoundsException(); } data[index] = newElmt; }... }

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 22 Implement as an Array, cont’d package ListImplementation; public class ListAsArray implements List public AnyType remove(int index) { if ((index = size)) { throw new IndexOutOfBoundsException(); } AnyType removedElmt = data[index]; for (int i = index; i < size-1; i++) { data[i] = data[i+1]; } size--; return removedElmt; }... }

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 23 Implement as an Array, cont’d package ListImplementation; public class ListAsArray implements List public void clear() { size = 0; } }

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 24 Assignment #5: A Solution

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 25 Building a Parse Tree Recursively private Node parseList() { Node root = new Node(); Node currentNode = null; Token token = nextToken(); TokenType tokenType = token.getType(); while (tokenType != TokenType.SS_RPAREN) { if (currentNode == null) { currentNode = root; } else { Node newNode = new Node(); currentNode.setCdr(newNode); currentNode = newNode; } if (tokenType == TokenType.SS_LPAREN) { currentNode.setCar(parseList()); } else { currentNode.setToken(token); } token = nextToken(); tokenType = token.getType(); } return root; // of the parse tree }  Call parseList() to parse a list. The ( token has already been seen. Build a binary tree representing the list and return the root of the tree. Call parseList() recursively to parse a sublist whenever the token is (.

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 26 Building a Parse Tree Recursively private Node parseList() { Node root = new Node(); Node currentNode = null; Token token = nextToken(); TokenType tokenType = token.getType(); while (tokenType != TokenType.SS_RPAREN) { if (currentNode == null) { currentNode = root; } else { Node newNode = new Node(); currentNode.setCdr(newNode); currentNode = newNode; } if (tokenType == TokenType.SS_LPAREN) { currentNode.setCar(parseList()); } else { currentNode.setToken(token); } token = nextToken(); tokenType = token.getType(); } return root; // of the parse tree } Create the root of the parse tree. currentNode will be the node being worked on. Get the next token after the ( The first time through the loop, currentNode is the root node. Subsequently, each iteration moves currentNode down the cdr links of the parse tree. If the token is a (, recursively parse the sublist and return the subtree root node. Then currentNode adopts that root node as its car. Otherwise, if it’s just an element, currentNode sets the element in its data field. Get the next token and loop again.

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 27 Building a Parse Tree Recursively private Node parseList() { Node root = new Node(); Node currentNode = null; Token token = nextToken(); TokenType tokenType = token.getType(); while (tokenType != TokenType.SS_RPAREN) { if (currentNode == null) { currentNode = root; } else { Node newNode = new Node(); currentNode.setCdr(newNode); currentNode = newNode; } if (tokenType == TokenType.SS_LPAREN) { currentNode.setCar(parseList()); } else { currentNode.setToken(token); } token = nextToken(); tokenType = token.getType(); } return root; // of the parse tree } (1 2 3) root currentNode (a (1 2 3) b) root currentNode a b 1 2 3

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 28 Multithreaded Programming in Java  Multithreaded programming involves having multiple threads of execution happening concurrently within a single Java program.  A thread is a program unit that executes independently of other parts of the program. _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 29 Thread vs. Process  A process is an executing program managed by the operating system. A process has its own resources, such as its separate runtime stack and its separate share of main memory. Switching between processes is relatively slow.  A thread runs within a single process. Process resources such as memory are shared. Thread switching is very fast.

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 30 Advantages of Multithreaded Programming  Better resource utilization. Example: When one thread is waiting for disk I/O to complete, another thread can be doing some work.  More responsive programs. Example: An event listener polls multiple event sources and handles each event as it arrives. One long event handler prevents other events from being handled. Solution: The event listener passes event handling to a “worker thread” and resumes polling for events.  Potentially simpler program design.

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 31 Challenges of Multithreaded Programming  Nondeterministic order of thread execution. You must ensure that the behavior of your program is not affected by execution order.  Thread synchronization. One thread may need the results computed by another thread.  Access to shared objects. What if two separate threads simultaneously attempt to modify a shared object? _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 32 Challenges, cont’d  Deadlocks Two threads each wait for the other to complete a task and your program waits indefinitely.  Greater overhead. Managing multithreading takes up extra machine cycles. Is it worth it?  Debugging woes. A multithreaded program can be extremely hard to debug.

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 33 Multicores and Multithreaded Programming  Multicore processors are now common.  Multithreaded programming is necessary to take advantage of having multiple cores.  A programmer who only knows how to do single-threaded programming will soon become obsolete. _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 34 Multithreaded Programming, cont’d  Object-oriented programming used to be an advanced topic.  Today, we teach object-oriented programming starting in the beginning programming classes. Languages like Java help make that possible.  Today, multithreaded programming is still an advanced topic. CS 159 Introduction to Parallel Processing _

SJSU Dept. of Computer Science Spring 2014: April 28 CS 152: Programming Language Paradigms © R. Mak 35 Multithreaded Programming, cont’d  Soon, we will teach multithreaded programming starting in the beginning classes.  Better language support is needed, though. _