Chapter 1Louden, Programming Languages1 For Ruby (and most other languages we will cover), you are given a ticket. We can’t possibly cover everything you.

Slides:



Advertisements
Similar presentations
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Introduction to Programming Languages Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Programming Types of Testing.
1 Language definition Syntax: the structure of a program. Usually given a formal (i.e., mathematical) definition using a context-free language. (Lexical.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
you admittance to the “show”.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
The Analytical Engine Module 6 Program Translation.
High-Level Programming Languages
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Programming Languages Structure
ISBN Lecture 01 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Lecture 01 Topics Motivation Programming.
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
Louden, Programming Languages
C++ fundamentals.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CS102 Introduction to Computer Programming
COMP313A Programming Languages Introduction. More Housekeeping Stuff Reading Material Textbook –Programming Languages: Principles and Practice by Kenneth.
Introducing Java.
CSE 425: Intro to Programming Languages and their Design A Few Key Ideas No particular language is a prerequisite for this course –However you should be.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CS 355 – Programming Languages
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
High-Level Programming Languages: C++
(1.1) COEN 171 Programming Languages Winter 2000 Ron Danielson.
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
COP4020 Programming Languages
Chapter 1. Introduction.
CS 363 Comparative Programming Languages
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
Chapter 1 - Introduction
Software Overview. Why review software? Software is the set of instructions that tells hardware what to do The reason for hardware is to execute a program.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
CPS 506 Comparative Programming Languages Syntax Specification.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
Programming Languages
Introduction to OOP CPS235: Introduction.
CPS120: Introduction to Computer Science Variables and Constants.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Visit for more Learning Resources
Why study programming languages?
Chapter 1 Introduction.
CS 3304 Comparative Languages
PROGRAMMING LANGUAGES
Chapter 1 Introduction.
Representation, Syntax, Paradigms, Types
Programming Languages 2nd edition Tucker and Noonan
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Reasons To Study Programming Languages
Presentation transcript:

Chapter 1Louden, Programming Languages1 For Ruby (and most other languages we will cover), you are given a ticket. We can’t possibly cover everything you need to know, but we have allowed you admittance to the “show”. You know how to build a project. You can run hello world. You understand key differences. You know where the documentation is. What you do with your admission ticket is up to you! Note – for resumes, “exposure” is likely the correct terminology

© Kenneth C. Louden, 2003 Adapted by Vicki Allan Chapter 1 - Introduction Programming Languages: Principles and Practice, 2nd Ed.

Chapter 1Louden, Programming Languages3 Course Motivation Why are programming languages the way they are? How are particular language features implemented/supported? Terminology for communication

Chapter 1Louden, Programming Languages4 Course Motivation cont… understand the underlying ideas of the main programming paradigms know more about the huge variety of programming languages understand how the syntax and semantics of languages can be defined precisely. have a deeper understanding of the history and rationale behind languages like C++.

Chapter 1Louden, Programming Languages5 Relationship between thought and language. The Sapir-Whorf hypothesis in linguistics states that the structure of one's native-tongue influences the way one's mind perceives the world. It has found at best very limited experimental support, at least in its strong form. One study has shown that subjects in memory tests are more likely to remember a given color if their nativelanguage includes a word for that color. Example – if you had no identified concept of recursion, how would that affect the ability to reason about it? Array accessing – what is concept? First programming language affects ability to think in Object Oriented terms

Chapter 1Louden, Programming Languages6 Why study programming languages? Understanding of terminology Increased capacity to express ideas improved background for choosing language increased ability to learn and retain new languages - something to hang on to Better understanding of significance of implementation. Efficiency is key – not just ease of programming. Ability to design new languages - or user interface

Chapter 1Louden, Programming Languages7 Example Beginning students – always wanted to know specific answers:Can I do X? What happens if I do Y? Often hadn’t tried the specific test, but could reason about it from general knowledge of implementation. Ex: What happens if I try to return a reference to a local variable? Is this a compile time or run time issue? How will the system respond?

Chapter 1Louden, Programming Languages8 Programming languages bridge the human- computer semantic gap Human: Interested in modeling the real world More interested in what computer should do than how Computer: Only data it can manipulate is sequences of zeros and ones. Understands low-level “how” instructions.

Chapter 1Louden, Programming Languages9 What are programming languages… High-level languages bridge the human- computer semantic gap by providing a higher level notation that can still be executed by computer Definition: A programming language is a notational system for describing computation in machine-readable and human-readable form.

Chapter 1Louden, Programming Languages10 Computation: Described by a Turing Machine - a very simple computer that can carry out all known computations (albeit not very efficiently). A programming language is Turing complete if it can be used to describe any computation performed by a Turing Machine.

Chapter 1Louden, Programming Languages11 Turing Machine – 1936 Alan Turing "If your state is 42 and the symbol you see is a '0' then replace this with a '1', move one symbol to the right, and assume state 17 as your new state.“ a Turing machine consists of: –A tape which is divided into cells. Each cell contains a symbol from some finite alphabet. The alphabet contains a special blank symbol and one or more other symbols. The tape is assumed to be arbitrarily extendible to the left and to the right, –A head that can read and write symbols on the tape and move left and right. –A state register stores the state of the Turing machine. –An action table (or transition function) that tells the machine what symbol to write, how to move the head ('L' for one step left, and 'R' for one step right) and what its new state will be, given the symbol it has just read on the tape and the state it is currently in.

Chapter 1Louden, Programming Languages12 What is needed for Turing completeness? integer variables arithmetic sequentially execution of statements, which include assignment, selection (if) and loop (while) statements.

Chapter 1Louden, Programming Languages13 Machine-readability: Basically, the existence of a (more or less) linear-time translation algorithm. –Usually boils down to: The syntax must be given by a context- free grammar. We will discuss context-free grammars. Basically, the language must have rules which aid parsing.

Chapter 1Louden, Programming Languages14 Human-readability: This is the real issue! Virtually all the complex details of a programming language are there to (supposedly) enhance human readability. Still not very well understood. Is strongly dependent on good choice of abstractions. –Abstraction is the result of generalization by reducing the information content of a concept,typically to retain only information which is relevant for a particular purpose. For example, abstracting a leather soccer ball to a ball retains only the information on general ball attributes and behavior.

Chapter 1Louden, Programming Languages15 What about human “writability??” Aren’t programming languages there to promote the writing of programs, not the reading of them? Nonsense! Writability is a hacker’s goal. Readability is the real goal: many people are going to have to read your program after you have written it. –What is the relationship between readability and writability?

Chapter 1Louden, Programming Languages16 Abstractions:

Chapter 1Louden, Programming Languages17 Computational Paradigms Programming languages began by imitating the operations of a computer. It is not surprising that the kind of computer for which they were written had significant effect on their design. –variables representing memory –assignment to change values –sequential execution of statements

Chapter 1Louden, Programming Languages18 Language Paradigms: Imperative (procedural): traditional sequential programming (passive data, active control). Characterized by variables, assignment, and loops. Object-oriented: data-centric, data controls its own use. Action by request to data objects. Characterized by messages, instance variables, and protection. Extension of imperative paradigm. Functional: passive data, but no sequential control; all action by function evaluation (“call”), particularly recursion. No local variables! Similar to mathematics. Ex. Haskell

Chapter 1Louden, Programming Languages19 Language Paradigms: Example Haskell (functional style): fact n = if n == 0 then 1 else n * fact (n-1) square x = x * x squarelist lis = if (null lis) then lis else square (head lis): squarelist (tail lis)

Chapter 1Louden, Programming Languages20 Language Paradigms (cont.): Logic: Assertions are the basic data; logic inference the basic control. Again, no sequential operation. Similar to mathematics. Ex Prolog ex: I am your sister if I am female and we have common parents. Parallel: well, maybe not really a paradigm, but some think so. Again, no sequential operation. “Declarative”: Logic and functional paradigms share this property: state “what” needs computing, not “how” (sequence). Ex. Prolog

Chapter 1Louden, Programming Languages21 Languages and paradigms Imperative: C, Pascal, core Ada, FORTRAN Functional: Lisp (Scheme), ML, Haskell Object-oriented: C++, Java, Smalltalk, Ruby Logic: Prolog Parallel: Java (threads), Ada (tasks)

Chapter 1Louden, Programming Languages22 Paradigm use is rarely “pure”: The C program (in text) defined gcd function in a purely functional style, even though C is mainly imperative. The Java program used some imperative code to compute the gcd, and was not completely object- oriented (integers aren’t objects). The Scheme code used sequencing to do I/O, an imperative feature.

Chapter 1Louden, Programming Languages23 Examples of languages that are pure (mostly): Imperative: (old) FORTRAN Functional: Haskell Object-oriented: Ruby

Chapter 1Louden, Programming Languages24 Language definition Syntax: the structure of a program. Usually given a formal (i.e., mathematical) definition using a context-free language. (Lexical structure - the structure of the words or tokens - uses regular expressions.) Semantics: the actual result of execution. Usually described in English, but can be done mathematically. Semantics can have a static (compile time) component: type checking, definition checking, other consistency checks prior to execution. What are dynamic (run time) components of semantics?

Chapter 1Louden, Programming Languages25 Language translation Compiler: two-step process that 1. translates source code into executable code; 2. the user executes the executable code. compiler run sourceexecutable inputs outputs

Chapter 1Louden, Programming Languages26 Language translation Interpreter: one-step process in which the source code is executed directly. Hybrids are also possible (Java). run source inputs outputs Java compiler sourcebytecode inputs outputs machine dependent interpreter Bytecode is composed of instructions that have been brought to the lowest level possible without making them machine dependent.

Chapter 1Louden, Programming Languages27 Compilation, Interpretation, and Hybrid systems Consider this piece of code: public class Test { public static void main(String args[]) { int i; i = 2; i = i + 7; } }

Chapter 1Louden, Programming Languages28 If we were to compile it, we would create a file of machine instructions that would only work for one architecture. If we were to interpret it, our interpreter would have to be able to understand the high level code AND would repeatedly parse it (if the code was in a loop or called multiple times). Parsing is the process of analyzing a text, made of a sequence of tokens (for example, words), to determine its grammatical structure with respect to a given grammar. tokens When we use the hybrid approach of Java, we produce the file Test.class, which is a binary file that's not readable by most humans. We can convert the file to a readable form with the javap tool as shown here:

Chapter 1Louden, Programming Languages29 C:\ > javap -c Test Method void main(java.lang.String[]) 0 iconst_2 // Put integer 2 on stack 1 istore_1 // Store the top stack value at location 1 2 iload_1 // Put the value at location 1 on stack 3 bipush 7 // Put the value 7 on the stack 5 iadd // Add two top stack values together 6 istore_1 // Sum, on top of stack, stored 7 return // Finish processing

Chapter 1Louden, Programming Languages30 Although the bytecode cannot access registers or directly reference memory locations and must obey various other restrictions, the actual JVM (java virtual machine) program can use whatever techniques are convenient to use for a particular platform. As long as the Java bytecode sees only a JVM specification compliant system, the JVM programmer has broad discretion for its implementationJVM specification

Chapter 1Louden, Programming Languages31 Language Implementation Steps Compilation: lexical analysis: characters grouped into logical chunks (keywords, constants, etc) syntax analysis: figure out what it means - usually use parse trees (grammars to define). Like diagraming sentences. I have a picture of John at my home – what is meant? What does “at my home” modify? optimization - to make smaller or faster linking: supplying missing addresses to system code load module: user code augmented with system code

Chapter 1Louden, Programming Languages32 Language Implementation Steps (cont) Pure Interpretation: no translation phase - fetch, decode, and execute the source code (not machine code) Advantages/Disadvantages 1. easy to write debugger - as source lines are unchanged 2. execution is times slower; statement decoding is bottleneck 3. better for language with simple structure - as not so slow to decode 4. natural for some kinds of features - like dynamic binding of type. Ex: c = c+b If c may be integer, string, or a set, how can we know what code to generate? 5. Nice for executing user produced code at runtime Ex. boolean expression is easy to evaluate if known at compile time, but NOT if produced at run time.

Chapter 1Louden, Programming Languages33 What is meant by dynamic binding? Girls choice dance: –Will you go with Sofie? (early binding) –Will you go with Sofie/Ann/Betty (whoever shows up at your door)? (delayed binding) –No specific partner assigned, but will change throughout the night. (changing binding) Lots of the interesting issues involve binding times.

Chapter 1Louden, Programming Languages34 Class binding (compiled language) Figure out the class to be invoked Determine which method signature to use If there is more than one matching signature, the one that is most specific is chosen. Example: doit(Object o) or doit(ColoredPoint p) or doit (Point p) A method is applicable if –the number of parameters matches –the type of each actual argument can be converted to the type of the corresponding parameter. A method is accessible - we know about the method

Chapter 1Louden, Programming Languages35 Error classification Lexical: character-level error, such as illegal character (hard to distinguish from syntax error). Syntax: error in structure (e.g., missing semicolon or keyword). Static semantic: non-syntax error prior to execution (e.g., undefined vars, type errors). Dynamic semantic: non-syntax error during execution (e.g., division by 0). Logic: programmer error, program not at fault.

36 Rectangle Triangle Polygon class Polygon{ int numVertices; float *xCoord, float *yCoord; public: void set(float *x, float *y, int nV); String display(String s) { return “POLYGON” + s; float area(); }; class Rectangle: public Polygon{ public: float area(); String display(String s) { return “RECTANGLE” + s … ; }; class Triangle: public Polygon{ public: float area() {…} String display(String s) { return “TRIANGE” + s …; }; Inheritance Concept

Which display do you want? Can you tell at compile time? Could there be several possible matches? Chapter 1Louden, Programming Languages37

Chapter 1Louden, Programming Languages38 Notes on error reporting A compiler will report lexical, syntax, and static semantic errors. It cannot report dynamic semantic errors; job of runtime system. An interpreter will often only report lexical and syntax errors when loading the program. Static semantic errors may not be reported until just prior to execution. Indeed, most interpreted languages (e.g. Lisp, Smalltalk) do not define any static semantic errors. No translator can report a logic error.

Chapter 1Louden, Programming Languages39 Find examples of syntax, semantic and logic errors. public int gcd ( int v# ) { int z = value y = v; while ( y >= 0 ) { int t = y; y = z % y; z = t; } return y; }

Chapter 1Louden, Programming Languages40 Sample Errors (Java): public int gcd ( int v# ) // lexical bad # { int z = value // syntax - missing ; y = v; // static semantic - y undefined while ( y >= 0 ) { int t = y; y = z % y; z = t; // dynamic semantic - division by zero } return y; // logic - should return z }