Chapter 1 - Introduction

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
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.
1 Language definition Syntax: the structure of a program. Usually given a formal (i.e., mathematical) definition using a context-free language. (Lexical.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
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.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
High-Level Programming Languages
Reasons to study concepts of PL
Programming Languages Structure
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Louden, Programming Languages
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
CS 331, Principles of Programming Languages Introduction.
Overview. Copyright © 2006 The McGraw-Hill Companies, Inc. Chapter 1 Overview A good programming language is a conceptual universe for thinking about.
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.
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
Imperative Programming
High-Level Programming Languages: C++
ProgrammingLanguages Programming Languages Computational Paradigms.
(1.1) COEN 171 Programming Languages Winter 2000 Ron Danielson.
COP4020 Programming Languages
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.
Principles of Programming Languages Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Introduction.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
1 Introduction Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
ProgrammingLanguages Programming Languages Language Definition, Translation and Design.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
Chapter 3 - Language Design Principles
Computer Programming CONTENTS Introduction to Operating Systems Introduction to programming languages Introduction to perl programming language Programming.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
int k = Integer.MAX_VALUE; k++; int k = Integer.MAX_VALUE; k++; What happens when the following code executes? byte b = someFile.readByte(); b = (byte)(b.
CPS120: Introduction to Computer Science Variables and Constants.
C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
1-1 1 Introduction  Programming linguistics: concepts and paradigms syntax, semantics, and pragmatics language processors.  Historical development of.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Programming Language Paradigms ITSK2314 Lecture 3.
a medium allowing humans and computers to communicate an abstraction of the real world a notation for expressing algorithms the set of all syntactically.
Programming Language History and Evolution
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Programming Languages 2nd edition Tucker and Noonan
Functional Programming
Why study programming languages?
PROGRAMMING LANGUAGES
Representation, Syntax, Paradigms, Types
Chap. 6 :: Control Flow Michael L. Scott.
CSE 3302 Programming Languages
FP Foundations, Scheme In Text: Chapter 14.
Representation, Syntax, Paradigms, Types
Chap. 6 :: Control Flow Michael L. Scott.
Programming Languages 2nd edition Tucker and Noonan
High Level Programming Languages
Representation, Syntax, Paradigms, Types
Principles of Programming Languages
Representation, Syntax, Paradigms, Types
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

Chapter 1 - Introduction 4/23/2017 Chapter 1 - Introduction Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden © Kenneth C. Louden, 2003

What is a Programming Language? Definition: A programming language is a notational system for describing computation in machine-readable and human-readable form. Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages 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 1 K. Louden, Programming Languages

What is needed for Turing completeness? Virtually nothing: A programming language is Turing complete provided it has integer variables and arithmetic and sequentially executes statements, which include assignment, selection (if) and loop (while) statements. Even if statements are unnecessary (see exercises). Chapter 1 K. Louden, Programming Languages

Machine-readability: Also not a huge requirement: 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. (See Chapter 4.) Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages 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. Chapter 1 K. Louden, Programming Languages

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: Perl is very writable, but try to read it! Readability is the real goal: many people are going to have to read your program after you have written it. Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages Abstractions: Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages 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. Functional: passive data, but no sequential control; all action by function evaluation (“call”), particularly recursion. No variables! Chapter 1 K. Louden, Programming Languages

Language Paradigms (cont.): Logic: Assertions are the basic data; logic inference the basic control. Again, no sequential operation. 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). Chapter 1 K. Louden, Programming Languages

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

Are functional languages Turing-complete? Previous theorem on Turing-completeness (slide 4) depends on the existence of variables and loops. Functional programs do not have variables or loops. Can all computation be expressed? Yes!: A programming language is Turing complete if it has integer values, arithmetic functions on those values, and if it has a mechanism for defining new functions using existing functions, selection, and recursion. Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages Examples in three languages (Euclid’s gcd algorithm): Compute the greatest common divisor of two integers input by the user, and print the result. For example, the gcd of 15 and 10 is 5. Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages C (Figure 1.7, p. 26): #include <stdio.h> int gcd(int u, int v) /* “functional” version */ { if (v == 0) return u; else return gcd (v, u % v); /* “tail” recursion */ } main() /* I/O driver */ { int x, y; printf("Input two integers:\n"); scanf("%d%d",&x,&y); printf("The gcd of %d and %d is %d\n", x,y,gcd(x,y)); return 0; Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages Java (Figure 1.9, p. 27): import java.io.*; class IntWithGcd { public IntWithGcd( int val ) { value = val; } public int getValue() { return value; } public int gcd ( int v ) { int z = value; /* “imperative” version */ int y = v; while ( y != 0 ) { int t = y; y = z % y; z = t; } return z; private int value; Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages Java (continued): class GcdProg /* driver */ { public static void main (String args[]) { System.out.println("Input two integers:"); BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); try /* must handle I/O exceptions */ { IntWithGcd x = /* create an object */ new IntWithGcd(Integer.parseInt(in.readLine())); int y = Integer.parseInt(in.readLine()); System.out.print("The gcd of " + x.getValue() + " and " + y + " is "); System.out.println(x.gcd(y)); } catch ( Exception e) { System.out.println(e); System.exit(1); } } Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages Scheme (Figure 1.5, p. 17): (define (gcd u v) (if (= v 0) u (gcd v (modulo u v)))) Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages Scheme (Figure 11.7, p. 486): (define (euclid) ; sequential! (display "enter two integers:") (newline) ; goes to next line on screen (let ((u (read)) (v (read))) (display "the gcd of ") (display u) (display " and ") (display v) (display " is ") (display (gcd u v)) (newline))) Chapter 1 K. Louden, Programming Languages

Paradigm use is rarely “pure”: The C program defined the 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 1 K. Louden, Programming Languages

Examples of languages that are pure (mostly): Imperative: (old) FORTRAN Functional: Haskell Object-oriented: Smalltalk Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages 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 (Chapter 13). Semantics can have a static component: type checking, definition checking, other consistency checks prior to execution. Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages Language translation Compiler: two-step process that translates source code into target code; then the user executes the target code. Interpreter: one-step process in which the source code is executed directly. Hybrids are also possible (Java). Chapter 1 K. Louden, Programming Languages

K. Louden, Programming Languages Error classification Lexical: character-level error, such as illegal character (hard to distinguish from syntax). 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. Chapter 1 K. Louden, Programming Languages

Notes on error reporting A compiler will report lexical, syntax, and static semantic errors. It cannot report dynamic semantic errors. 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 1 K. Louden, Programming Languages

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

K. Louden, Programming Languages Language design Good, consistent set of abstractions. Tie-in to other technology (or clear single goal): C : Unix Java : Internet C++ : most efficient OO language Now also: Ease of interface with other languages and systems Good API libraries Chapter 1 K. Louden, Programming Languages