1 440 Review 2014. 2 Topics Covers everything – in slides – from the very beginning. Language classification, history, and comparison How to describe.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
CS 31003: Compilers Introduction to Phases of Compiler.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
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.
CPSC Compiler Tutorial 9 Review of Compiler.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
The Analytical Engine Module 6 Program Translation.
The Evolution of Programming Languages
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CS 415: Programming Languages Chapter 1 Aaron Bloomfield Fall 2005.
Programming Languages CPS120: Introduction to Computer Science Lecture 5.
Invitation to Computer Science 5th Edition
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.
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
COP4020 Programming Languages
Chapter 1. Introduction.
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.
Programming language history Created by wordle.net, from the text in this slide.
COMPUTER PROGRAMS AND LANGUAGES Chapter 4. Developing a computer program Programs are a set (series) of instructions Programmers determine The instructions.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
1 440 Midterm 2012 fall. Given the following two classes: public class A { void doSomething () { System.out.println("A"); } public class B extends A{
Programming Languages –14 David Watt (Glasgow) Steven Wong (Singapore) Moodle : Computing Science → Level 3 → Programming Languages 3 © 2012 David.
Chapter 1 - Introduction
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.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Chapter 6 Programming Languages © 2007 Pearson Addison-Wesley. All rights reserved.
1 440 Sample questions 2009 fall. 2 Which of the following is not an OO programming language? –Java –C++ –Smalltalk –Simula 67 –None of the above.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1 Topics Covers everything –1) in slides –2) from the very beginning. Language classification, history, and comparison Programming paradigms –Structured.
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
1 Programming Languages Fundamentals Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
CPS 506 Comparative Programming Languages Syntax Specification.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Semantics In Text: Chapter 3.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
Languages and Compilers
Programming Languages
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
a medium allowing humans and computers to communicate an abstraction of the real world a notation for expressing algorithms the set of all syntactically.
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Component 1.6.
Concepts of Programming Languages
Why study programming languages?
Computer Programming.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Programming Languages and Compilers (CS 421)
Presentation transcript:

1 440 Review 2014

2 Topics Covers everything – in slides – from the very beginning. Language classification, history, and comparison How to describe a (programming) language – Syntax (not covered) – Semantics ( Axiomatic semantics) Programming paradigms – Structured programming vs. Unstructured programming – Imperative programming vs. Declarative programming – Functional programming (Scheme, lambda calculus, XSLT, Mapreduce) – Logic programming (Prolog, Horn Clause) – Object-oriented programming (polymorphism, persistence) – Aspect Oriented Programming (AspectJ) Concepts and programming

3 Types of questions Multiple choices; true/false; filling in blanks. Running results of programs; Modify programs; Short explanations; Others.

4 Which of the following is not an OO programming language? – Java – C++ – Smalltalk – Simula 67 – None of the above.

5

6 The following figure is used to explain which language: – Prism language – Requirement language – Aspect oriented language – Persistent programming language – Business process language

7 Which of the following language is not a declarative language? – Java – Prolog – SQL – Scheme – None of the above. Imperative: a programming paradigm that describes computation in terms of a program state and statements that change the program state. A program is "declarative" if it describes what something is, rather than how to create it. – Imperative programs make the algorithm explicit and leave the goal implicit; – Declarative programs make the goal explicit and leave the algorithm implicit. Examples of declarative languages: – Functional programming languages, Logic programming languages, SQL.

8 Which of the following is a script language? – Java – C# – PHP – Prolog – Scheme – None of the above. Scripting: connecting diverse pre-existing components to accomplish a new related task. – Favor rapid development over efficiency of execution; – Often implemented with interpreters rather than compilers; – Strong at communication with program components written in other languages.

9 BNF was first used to describe the syntax of which language: – C – Fortran – Algol – COBOL – LISP – None of the above

10 ALGOL (ALGOrithmic Language) de facto standard way to report algorithms in print Designed to improve Fortran John Backus developed the Backus Normal/Naur Form method of describing programming languages. ALGOL 60 inspired many languages that followed it "ALGOL 60 was a great improvement on its successors.“ The full quote is "Here is a language so far ahead of its time, that it was not only an improvement on its predecessors, but also on nearly all its successors" --C. A. R Hoare procedure Absmax(a) Size:(n, m) Result:(y) Subscripts:(i, k); value n, m; array a; integer n, m, i, k; real y; comment The absolute greatest element of the matrix a, of size n by m is transferred to y, and the subscripts of this element to i and k; begin integer p, q; y := 0; i := k := 1; for p:=1 step 1 until n do for q:=1 step 1 until m do if abs(a[p, q]) > y then begin y := abs(a[p, q]); i := p; k := q end end Absmax

11 Which of the following is not part of a compiler? – Scanner; – Parser; – Code generator; – Optimizer; – Interpreter; – None of the above.

12 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) Classification by implementation methods

13 When you run the following Java program: public class Hello { public static void main(String [ ] a){ System.out.println("Hello"); } How many classes will be loaded into the system? – One – Two – Three – Hundreds

14 Run java -verbose sol:~/440>java -verbose Hello [Opened /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Opened /usr/jdk/instances/jdk1.5.0/jre/lib/jsse.jar] [Opened /usr/jdk/instances/jdk1.5.0/jre/lib/jce.jar] [Opened /usr/jdk/instances/jdk1.5.0/jre/lib/charsets.jar] [Loaded java.lang.Object from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.io.Serializable from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.lang.Comparable from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.lang.CharSequence from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.lang.String from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.lang.reflect.GenericDeclaration from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.lang.reflect.Type from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.lang.reflect.AnnotatedElement from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.lang.Class from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.lang.Cloneable from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.lang.ClassLoader from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.lang.System from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] … (hundreds of related classes) [Loaded Hello from file:/global/fac2/jlu/440/] Hello [Loaded java.lang.Shutdown from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] [Loaded java.lang.Shutdown$Lock from /usr/jdk/instances/jdk1.5.0/jre/lib/rt.jar] public class Hello { public static void main(String [] a){ System.out.println("Hello"); }

15 A program written in an interpreted language is usually – slow to run; – slow to develop; – slow to compile; – None of the above.

16 Interpreted language Programs are executed from source form, by an interpreter. – many languages have both compilers and interpreters, including Lisp, BASIC, and Python. Disadvantages: – Much slower –Real time translation; –Initially, interpreted languages were compiled line-by-line; each line was compiled as it was about to be executed, and if a loop or subroutine caused certain lines to be executed multiple times, they would be recompiled every time. – Require more space. –Source code, symbol table, … Advantage of interpreted languages – Easy implementation of source-level debugging operations, because run-time errors can refer to source-level units –E.g., if an array index is out of range, the error message can easily indicate the source line and the name of the array. – It can take less time to interpret it than the total time required to compile and run it. This is especially important when prototyping and testing code when an edit-interpret-debug cycle can often be much shorter than an edit-compile-run-debug cycle. (e.g., csh) Classification by implementation methods

17 Compared with declarative languages, which of the following is not true for imperative languages: – Can have side effects; – More efficient to run; – Closer to computer; – Harder to implement the language; – None of the above. True or false: Declarative languages are more difficult to implement than imperative languages.

18 Why is it difficult to implement declarative languages? By definition, it specifies what the task is, not the way how to solve it. Consider the following query: – customers(id, name, phone) – Orders(o-id, c-id, product, price, date) SELECT product, price, date FROM customers, orders WHERE customers.id = orders.c-id AND customers.name=“john” It declares what we want. Does not specify how to implement it. – e.g. which condition to run first? There are many different ways to implement. – A naïve one would be very expensive (construct the Cartesian product of the two tables, join two ids first) would be very expensive; Query engine (compiler) will take care of these implementation issue. Conclusions: – Declarative programming focus on higher level of abstraction; – It is more difficult to implement.

19 True or false: Imperative languages are very high level programming languages. though t Languages machine High Level Language Assembly Language Machine Language Very high level Language Closer to humans

20 Which of the following is not a technology for distributed objects? – CORBA – Java RMI – EJB – AspectJ

21 True or false: A compiler translates source code of one high level language to another high level language. Compilation: translating high-level program (source language) into machine code (machine language) – Slow translation, fast execution High level to high level language translation: programming language transformation

22 Which of the following is the rule for if statement? None of above

23 Which of the following is the rule for while statement?

24 Given the following Hoare triple {true} f := 1; n:=10; while (n != 0) do ( f := n*f; n:=n-1; ) {f=10!} Write its loop invariant. You don’t need to write the proof of the program.

25 Solution: n f *1 8 9*10*1 7 8*9*10*1 … P is f=10!/n!

26 Given the following Hoare triple {true} x := 0; f := 1; while ( x < n ) do (x := x + 1; f := f * x;) {f=n!} Write its loop invariant. f=x! ∧ x<=n

27 Prove that the following Hoare triple is true. You should write down the details of derivation. In each step please write the rule name that is used. {x=2  y=0} x:=y; y:=x; {x=0  y=0} Solution: {x=0  y=0 [x/y]} y:=x; {x=0  y=0}, by assignment Axiom {x=0  x=0} y:=x; {x=0  y=0}, by simplification a){x=0} y:=x; {x=0  y=0}, by simplification {x=0 [y/x]} x:=y; {x=0}, by assignment axiom b){y=0} x:=y; {x=0}, by simplification By sequential composition rule and a), b), c) {y=0} x:=y; y:=x; {x=0  y=0} d)x=2  y=0  y=0, by logic by Consequence rule and c), d), we can get d){x=2  y=0} x:=y; y:=x; {x=0  y=0}

28 True or false: Modern high level programming languages removed GOTO statement, hence they can’t describe some computational tasks that could have been described using GOTO statement.

29 Structured programming Any program can be goto-free (1966, Böhm and Jacopini, CACM) – any program with gotos could be transformed into a goto-free form involving only –Sequential composition –choice (IF THEN ELSE) and –loops (WHILE condition DO xxx), –possibly with duplicated code and/or the addition of Boolean variables (true/false flags). C S Y N S2S2 C S1S1 Y N S1S2 Classification by programming paradigms

30 If we can prove the Hoare triple {P} S {Q}, we can say that program S is totally correct for the pre-condition P and pos-condition Q. {P} S {Q} means “if we assume that P holds before S starts executing, then Q holds at the end of the execution of S” – I.e., if we assume P before execution of S, Q is guaranteed after execution of S To prove total correctness we also have to prove that the loop terminates

31 List four different language paradigms and give at least one example language for each paradigm. Aspect-oriented programming addresses the problem of crosscutting concerns. What are crosscutting concerns and what are the two main problems of crosscutting concerns?

32 Given the following partial program, fill in the missing part so that it will print the following: Hello Harry, having fun? public class MessageCommunicator { public static void deliver(String person, String message) { System.out.print(person + ", " + message); }} public class Test { public static void main(String[] args) { MessageCommunicator.deliver("Harry", "having fun?"); }} public aspect HindiSalutationAspect { pointcut sayToPerson(String person) : call(void MessageCommunicator.deliver(____________ )) && args(____________________________); void around(String person) : sayToPerson(person) { proceed(______________); }}

//Listing 2.4 HindiSalutationAspect.java public aspect HindiSalutationAspect { pointcut sayToPerson(String person): call(void helloV3.MessageCommunicator.deliver(String, String)) && args(person, String); void around(String person) : sayToPerson(person) { proceed(person + " -ji"); } 33

34 For our Account example in this course, suppose the main method will run the following two statements in sequel. The initial account balance is zero. account.credit(100); account.debit(20); What will be the account balance for the following advice? void around(): call(* *.credit(..)){ proceed();} What will be the balance if we run the following advice instead? void around(float amount): call(* *.credit(float)) && args(amount) { proceed(amount+1000); } What will be the balance for the following advice without including the previous two advices: – void around(): call(* *.debit(..)){ }

Functional and logic programming (and everything else) will be tested again 35