Download Introduction Slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

C syntax wikipedia , lookup

Subroutine wikipedia , lookup

Compiler wikipedia , lookup

Logic programming wikipedia , lookup

C++ wikipedia , lookup

History of compiler construction wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Control flow wikipedia , lookup

Go (programming language) wikipedia , lookup

Reactive programming wikipedia , lookup

Parsing wikipedia , lookup

Functional programming wikipedia , lookup

Programming language wikipedia , lookup

Object-oriented programming wikipedia , lookup

Structured programming wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
COMP 205
Survey of Computer Languages
Dr. Chunbo Chu
About me
• Lead Faculty in Computer Science
• Education in Computer Science
– BS (1997)
– MS (2000)
– PhD (2008)
• Specialty and Interests
– Distributed Systems, Networking, CS Education
About you…
About COMP205
• Overview of the concepts and practice
with several programming languages
• The Syllabus
Error in gradebook
• Assignment 13-2 :Working with Prolog is missing
• 25 points
About COMP205
• Structure
– Module 1 (week 1)
Introduction to Programming Languages:
HTML/XML/XSLT
– Module 2 (week 2-6)
Markup and Scripting Languages:
JavaScript/PHP
– Module 3 (week 7-8)
Scripting Languages: Perl, Ruby
– Midterm (week 8)
– Module 4 (week 9-10)
Microsoft .Net and C#
– Module 5 (week 11-14)
Non-Imperative Languages: Lisp, Prolog
– Final (week 15)
About COMP205
• Outcomes
– Write simple programs in a variety of
languages
•
•
•
•
•
HTML/XML/XSLT
JavaScript/PHP
Perl/Ruby
MS .Net/C#
Lisp/Prolog
– Compare and contrast language paradigms
(imperative, functional, declarative).
– Demonstrate the ability to move between
programming languages.
About COMP205
• Virtual Machine DVD
– VMware Workstation: desktop-based
virtualization program that permits a guest
operating system to run as an application
under your host operating system.
– Virtual Machine for COM205
• Submit your assignment in Drop Box
• Turnitin.com
– class: COMP205 V1FF Summer 2009
– class ID: 2711966
– enrollment password: COMP205Su09
Module 1
• Outcomes:
– Describe language paradigms.
– Explore the history of programming
languages.
– Examine the general features of
programming languages.
Computer languages
• A notational system for describing
computation in machine-readable and
human-readable form
– Machine-readable: the existence of a (more
or less) linear-time translation algorithm;
the syntax must be given by a context-free
grammar.
• Computation Described by a Turing
Machine - a very simple computer that
can carry out all known computations
Language Paradigms
• Imperative
– traditional sequential programming (passive
data, active control). Characterized by
variables, assignment, and loops.
– Procedural:
– Object-oriented: data-centric, data controls
its own use, action by request to data
objects. Characterized by messages,
instance variables, and protection.
Language Paradigms
• Declarative
– Logic and functional paradigms share this
property: state “what” needs computing, not
“how” (sequence).
– Functional: passive data, but no sequential
control; all action by function evaluation (“call”),
particularly recursion. No variables!
– 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.
• ……
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
13
C:
#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
14
Java:
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
15
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
16
Scheme:
(define (gcd u v)
(if (= v 0) u
(gcd v (modulo u v))))
Chapter 1
K. Louden, Programming
Languages
17
Scheme:
(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
18
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
19
Examples of languages that are
pure (mostly):
• Imperative: (old) FORTRAN
• Functional: Haskell
• Object-oriented: Smalltalk
Chapter 1
K. Louden, Programming
Languages
20
Evaluation Criteria
• Usability: how easy it is to read, write, and
change programs
• Support for abstraction: the ability to define
and then use complicated structures or
operations in ways that allow many of the
details to be ignored.
• Orthogonality: components are
independent of each other and they
behave in the same way in any
circumstance.
• Reliability: behaves as advertised and
produces results the software engineer
expects
Evaluation Criteria
• Efficiency: it can produce programs that
are consistent with system
specifications
• Portability: if the source code can be
moved from one platform to another
without modification.
• Cost: availability of software engineers
who know the language, usability,
reliability, efficiency, and portability.
Language Discussion
• Create a list of the languages you know
• Group them into:
–
–
–
–
–
Interactive
Compiled
Paradigm
Old
New
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 component: type
checking, definition checking, other
consistency checks prior to execution.
Chapter 1
K. Louden, Programming
Languages
24
Syntax
• Syntax is the structure of a language, i.e., the form
that each program or source code file must take.
• Since the early 1960s, syntax has been given as a
set of grammar rules in a form developed by Noam
Chomsky, John Backus, and Peter Naur. (Contextfree grammar, Backus Naur Form [BNF].)
• Syntax includes the definition of the words, or tokens,
of the language, which can be called its lexical
structure.
• Both lexical and syntactic structure have precise
mathematical definitions that every computer scientist
should know.
Chapter 4
K. Louden, Programming
Languages
25
Grammar and BNF
<program> -> begin <stmts> end
<stmts> -> <stmt> | <stmt> ; <stmts>
<stmt>-><var> = <expr>
<var> ->a | b | c | d
<expr> -><term> + <term> | <term> - <term> |
<term>
<term> -> <var> | const
begin a = a + c end
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
27
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
28
Attributes
• Properties of language entities, especially
identifiers used in a program.
• Important examples:
–
–
–
–
–
Value of an expression
Data type of an identifier
Maximum number of digits in an integer
Location of a variable
Code body of a function or method
Chapter 5
K. Louden, Programming
Languages
29
Attributes
• Declarations ("definitions") bind attributes to
identifiers.
• Different declarations may bind the same
identifier to different sets of attributes.
Binding times can vary widely:
• Value of an expression: during execution or
during translation (constant expression).
• Data type of an identifier: translation time (Java)
or execution time (Smalltalk, Lisp).
• Maximum number of digits in an integer:
language definition time or language
implementation time.
• Location of a variable: load or execution time.
• Code body of a function or method: translation
time or link time or execution time.
Chapter 5
K. Louden, Programming
31
New Assignments
• Assignment 1-1: Survey of Languages
• Assignment 1-2: Programming Language
History Paper
– Turnitin.com
– Drop box