• No results found

Programming Languages

N/A
N/A
Protected

Academic year: 2021

Share "Programming Languages"

Copied!
41
0
0

Loading.... (view fulltext now)

Full text

(1)

• In the beginning

… To use a computer, you needed to

know how to program it.

• Today

… People no longer need to know how to

program in order to use the computer.

• To see how this was accomplished, lets investigate how

programming languages evolved.

– First Generation

- Machine Language (code)

– Second Generation

- Assembly Language

– Third Generation

- People-Oriented Programming Languages

– Fourth Generation

- Non-Procedural Languages

– Fifth Generation

- Natural Languages

(2)

Assembled, Compiled, or Interpreted

Languages

• All programs must be translated before

their instructions can be executed.

• Computer languages can be

grouped

according to

which translation process is

used to convert the instructions into binary

code:

– Assemblers

– Interpreters

– Compilers

(3)

• Assembled languages:

– Assembler

: a program used to translate Assembly

language programs.

– Produces one line of binary code per original

program statement.

• The entire program is assembled before the program is

sent to the computer for execution.

Assembled, Compiled, or Interpreted

Languages

(4)

• Interpreted Languages:

– Interpreter

: A program used to translate high-level

programs.

– Translates one line of the program into binary code at a

time:

• An instruction is fetched from the original source code.

• The Interpreter checks the single instruction for errors. (If an error is found, translation and execution ceases. Otherwise…)

• The instruction is translated into binary code. • The binary coded instruction is executed.

• The fetch and execute process repeats for the entire program.

Assembled, Compiled, or Interpreted

Languages

(5)

Interpreters

………….... c = a * a; b = c + b; ………. Source code Program input Program output Interpreter Run time Abstract machine

(6)

• Compiled languages:

– Compiler

: a program used to translate high-level

programs.

– Translates the entire program into binary code before

anything is sent to the CPU for execution.

• The translation process for a compiled program:

– First, the Compiler checks the entire program for syntax errors in the original

source code.

– Next, it translates all of the instructions into binary code.

» Two versions of the same program exist: the original source code version, and the binary code version (object code).

– Last, the CPU attempts execution only after the programmer requests that the program be executed.

Assembled, Compiled, or Interpreted

Languages

(7)

Compilers

……….. 00000 01010 11110 01010 ……….. ………….... c = a * a; b = c + b; ……….

Source code Target code

Program input

Program output

Compiler

Translation (compile) time Run time

(8)

Anatomy of a Computer

Program written in a Programming Languages Assembly Language Translation

Compiler

Compilers

(9)

Anatomy of a Computer

Lexical Analyzer (Scanner)

Token Stream

Program (character stream)

Lexical Analyzer (Scanner)

Token Stream

Program (character stream)

Divides the string of input characters into single

(10)

Lexical Analyzer (Scanner)

Num(234) mul_op lpar_op Num(11) add_op

rpar_op

2 3 4 * ( 1 1 + - 2 2 )

(11)

Lexical Analyzer (Scanner)

18..23 + val#ue

Num(234) mul_op lpar_op Num(11) add_op

rpar_op

2 3 4 * ( 1 1 + - 2 2 )

Num(-22)

Not a number

(12)

Anatomy of a Computer

Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)

Token Stream

Parse Tree

Program (character stream)

Syntax Analyzer (Parser)

Token Stream

Parse Tree

(13)

Syntax Analyzer (Parser)

num

+

num

(

)

*

num

<expr> <expr> <expr> <expr> <expr> <expr> <op> <op>

num ‘*’ ‘(‘ num ‘+’ num ‘)’

(14)

Syntax Analyzer (Parser)

int * foo(i, j, k))

int i;

int j;

{

for(i=0; i j) {

fi(i>j)

return j;

}

Extra parentheses

Missing increment

Not an expression

Not a keyword

(15)

Anatomy of a Computer

Intermediate Representation

Semantic Analyzer Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)

Token Stream

Parse Tree

Program (character stream)

Intermediate Representation

Semantic Analyzer

Parse Tree

(16)

Semantic Analyzer

int * foo(i, j, k)

int i;

int j;

{

int x;

x = x + j + N;

return j;

}

Type not declared

Mismatched return type

Uninitialized variable used

Undeclared variable

(17)

Anatomy of a Computer

Code Optimizer

Optimized Intermediate Representation

Intermediate Representation

Semantic Analyzer Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)

Token Stream

Parse Tree

Program (character stream)

Code Optimizer

Optimized Intermediate Representation

Intermediate Representation

(18)

Optimizer

int sumcalc(int a, int b, int N) { int i; int x, t, u, v; x = 0; u = ((a<<2)/b); v = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + v + t*t; v = v + u; } return x; }

int sumcalc(int a, int b, int N) { int i; int x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x+4*a/b*i+(i+1)*(i+1); x = x + b*y; } return x; }

(19)

Anatomy of a Computer

Code Optimizer

Code Generator

Optimized Intermediate Representation

Assembly code

Intermediate Representation

Semantic Analyzer Lexical Analyzer (Scanner)

Syntax Analyzer (Parser)

Token Stream

Parse Tree

Program (character stream)

Code Generator

Optimized Intermediate Representation

Assembly code

(20)

Code Generator

sumcalc:

xorl %r8d, %r8d xorl %ecx, %ecx movl %edx, %r9d cmpl %edx, %r8d jg .L7

sall $2, %edi .L5: movl %edi, %eax cltd

idivl %esi

leal 1(%rcx), %edx movl %eax, %r10d imull %ecx, %r10d movl %edx, %ecx imull %edx, %ecx leal (%r10,%rcx),

%eax

movl %edx, %ecx addl %eax, %r8d cmpl %r9d, %edx jle .L5

.L7: movl %r8d, %eax ret

int sumcalc(int a, int b, int N) { int i; int x, t, u, v; x = 0; u = ((a<<2)/b); v = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + v + t*t; v = v + u; } return x; }

(21)

Programming for Everyone

• Several ways to control what your computer does

or the way it accomplishes a particular task:

– Using Macros

– Using HTML to create Web Pages

– Scripting

(22)

Building a Program

• Whatever type of problem needs to be solved, a

careful thought out plan of attack, called an

algorithm, is needed before a computer solution can

be determined.

1) Developing the algorithm.

2) Writing the program.

3) Documenting the program.

(23)
(24)

Introduction to Java technology

Java technology is both:

- a

programming language

and

- a

platform

.

(25)

• Simple

• Object oriented

• Distributed

• Multithreaded

• Dynamic

• Architecture neutral

• Portable

• High performance (JIT)

• Robust (memory)

• Secure (

several layers of defense discuss …

)

The

Java programming language

is a high-level language that can

be characterized by all of the following buzzwords:

(26)

Introduction to Java technology

Simple:

- The fundamental concepts of Java technology are grasped quickly; programmers can be productive from the very beginning.

- Java design team examined many aspects of the "modern" C and C++ languages to determine features that could be eliminated in the context of modern object-oriented programming.

The

Java programming language

is a high-level language that can

be characterized by all of the following buzzwords:

• No More Typedefs, Defines, or Preprocessor • No More Structures or Unions

• No Enums

• No More Functions

• No More Multiple Inheritance • No More Goto Statements

• No More Operator Overloading

• No More Automatic Coercions. You must do so explicitly by using a cast. • No More Pointers

(27)

Object oriented:

To be truly considered "object oriented", a programming language should support at a minimum four characteristics:

- Encapsulation

--implements information hiding and modularity (abstraction)

- Polymorphism

--the same message sent to different objects results in

behavior that's dependent on the nature of the object receiving the message

- Inheritance

--you define new classes and behavior based on existing classes to obtain code re-use and code organization

- Dynamic binding

--objects could come from anywhere, possibly across the network. You need to be able to send messages to objects without having to know their specific type at the time you write your code. Dynamic binding provides maximum flexibility while a program is executing

Introduction to Java technology

The

Java programming language

is a high-level language that can

be characterized by all of the following buzzwords:

(28)

Multithreaded:

Built-in support for

threads

provides Java programmers with a powerful tool to improve interactive performance of graphical applications. If your

application needs to run animations and play music while scrolling the page and downloading a text file from a server,

multithreading

is the way to obtain fast, lightweight concurrency within a single process space. Threads are

sometimes also called lightweight processes or execution contexts.

Introduction to Java technology

The

Java programming language

is a high-level language that can

be characterized by all of the following buzzwords:

(29)

Interpreted and Dynamic:

Introduction to Java technology

The

Java programming language

is a high-level language that can

be characterized by all of the following buzzwords:

• Java source code is stored as text in a .java file.

• The .java file is compiled into .class files (with the same name). • A .class file contains Java bytecodes (instructions).

• The bytecodes are interpreted at run time.

– The Java .class file is the executable code.

Compile Movie.java JVM Running program Movie.class (javac) (java)

(30)

Interpreted and Dynamic:

Introduction to Java technology

The

Java programming language

is a high-level language that can

be characterized by all of the following buzzwords:

(31)

How Does JVM Work?

•The class loader loads (in JVM) all required classes.

–JVM uses a CLASSPATH setting to locate class files.

The classpath can be added in run time by using the java cp or -classpath option.

•JVM Verifier checks for illegal bytecodes.

–This validation ensures that there are no memory access violations or other illegal actions performed.

•JVM Verifier executes bytecodes.

–JVM may invoke a Just-In-Time (JIT) compiler.

•Memory Manager releases memory used by the dereferenced object back to the OS.

–JVM handles Garbage collection.

(32)

Benefits of Just-In-Time (JIT) Compilers

JIT compilers:

- Improve performance

- Are useful if the same bytecodes are executed repeatedly

- Translate bytecodes to native instruction

- Optimize repetitive code, such as loops

- Use Java HotSpot VM for better performance and

reliability

(33)

Architecture neutral:

Introduction to Java technology

The

Java programming language

is a high-level language that can

be characterized by all of the following buzzwords:

The solution that the Java system adopts to solve the binary-distribution problem is a "binary code format" that's independent of hardware

architectures, operating system interfaces, and window systems.

The format of this system-independent binary code is

architecture

neutral

. If the Java run-time platform is made available for a given hardware and software environment, an application written in Java can then execute in that environment without the need to perform any special porting work for that application.

(34)

Introduction to Java technology

The

Java programming language

is a high-level language that can

be characterized by all of the following buzzwords:

Portable:

The primary benefit of the interpreted byte code approach is that compiled Java language programs are

portable

to any system on which the Java

interpreter and run-time system have been implemented.

Java specifies the sizes of all its primitive data types and the behavior of arithmetic on them. Here are the data types:

(35)

The

Java platform

:

Introduction to Java technology

A

platform

is the hardware or software environment in which a program runs. Most platforms can be described as a combination of the operating system

and underlying hardware.

The Java platform differs from most other platforms in that it's a software-only platform that runs on top of other hardware-based platforms.

(36)

Using Java with Enterprise

Internet Computing

Web server Application server Presentation Business logic • Servlets • JavaServer Pages (JSPs) • Enterprise JavaBeans (EJB) • CORBA Client Data

(37)

Using the Java Virtual Machine

Operating system

JVM

Application

Running Java Applications

All Java applications run within a Java Virtual Machine (JVM).

JVM is invoked differently depending on whether the Java program is an application or an applet.

(38)

Java Software Development Kit

Sun Java J2SE (known as JDK and Java SDK)

provides:

– Compiler (javac)

– Core class library

• classes.zip • rt.jar

– Debugger (jdb)

– Bytecode interpreter: The JVM (java)

– Documentation generator (javadoc)

– Java Archive utility (jar)

– Others

(39)

Typical Java Development

Environment

Integrated development environments (IDEs)

Provide tools that support the software-development

process, including editors for writing and editing

programs and debuggers for locating

logic errors

—errors

that cause programs to execute incorrectly.

Popular IDEs

Eclipse (www.eclipse.org)

NetBeans (www.netbeans.org)

JBuilder (www.codegear.com)

JCreator (www.jcreator.com)

BlueJ (www.blueJ.org)

jGRASP (www.jgrasp.org)

(40)

Using the Appropriate Development Kit

Java2 comes in three sizes:

• J2ME

(Micro Edition): geared toward developing

applications for small, memory-constrained devices, such

as cell phones, pagers and PDAs.

• J2SE

(Standard Edition): Complete ground-up

development environment for the Internet

• J2EE

(Enterprise Edition): Everything in the J2SE plus,

geared toward developing large-scale, distributed

(41)

lolo.java

package ex4d; // collection of classes of similar functionality

import java.util.* ; // import for date class

Interface Monster {

void menace(); }

interface DangerousMonster extends Monster {

void destroy(); }

public class lolo {

static void w (Lethal l) { l.kill(); }

public static void main (String[] args) {

DangerousMonster barney = new DragonZilla(); w(vlad);

} }

References

Related documents

The ordinal regression method was used to evaluate the relationship between ICT performance of the overall production procedures in Greek SMEs (enhancement of

described in proof of retrievability (PoR) model, where spot checking and error correcting codes are used to ensure both possession and retrievability of data

All participants included in the study were part of the ongoing, nationwide, multicentre Finnish Diabetic Nephropathy Study (FinnDiane), with the main aim of identifying

n  Sequential programming languages are essentially the λ-calculus, extended with predefined constructs, constants, types, and syntactic sugar. n  Ocaml is close to

 Language Translators: convert programs written in programming language into machine language.  Example of programming languages: C++, Java, COBOL,

It is important that the computation just returns the value, so, for example, it is not legal for the stateful return function to modify memory. Examples of return were given in

“In the plan of care for the Medicare payment episode for which this assessment will define a case-mix group, what is the indicated need for therapy visits (total of reasonable

Because most new programming languages with advanced-dispatching mechanisms provide a compiler that produces intermediate code of an established programming language, the debugger