Вы находитесь на странице: 1из 39

Principles of Programming Languages

Introduction
Outline

 An introductory tour
 Kinds of languages
 Objectives
 Compiler and Interpreter
Language Tour
dosseg
.model small
.stack 100h

.data
hello_message db 'Hello, World!',0dh,0ah,'$'

.code
main proc
mov ax,@data
mov ds,ax

mov ah,9
mov dx,offset hello_message
int 21h

mov ax,4C00h
int 21h
main endp
end main
C

#include <stdio.h>

main()
{
for(;;)
{
printf ("Hello World!\n");
}
}
C++
#include <iostream>

int main()
{
std::cout << "Hello, world!\n";
}
C#

class HelloWorld
{
static void Main () {
System.Console.Write("Hello World!");
}
}
HTML

<HTML>
<HEAD>
<TITLE>Hello, World Page!</TITLE>
</HEAD>

<BODY>
Hello, World!
</BODY>
</HTML>
Java

class HelloWorld {
public static void main (String args[]) {
for (;;) {
System.out.print("Hello World ");
}
}
}
Javascript
<TITLE>
Hello World in JavaScript
</TITLE>
<SCRIPT>
document.write ("Hello, world!")
</SCRIPT>
LATEX

\documentclass{article}
\begin{document}
\begin{center}
\Huge{HELLO WORLD}
\end{center}
\end{document}
Lisp
(DEFUN HELLO-WORLD ()
(PRINT (LIST 'HELLO 'WORLD)))

(LOOP (FORMAT T "~%Hello World"))

(while t
(message "Hello World")
(message "") ; force emacs to update the display
)
Prolog
hello :-
printstring("HELLO WORLD!!!!").

printstring([]).
printstring([H|T]) :- put(H), printstring(T).
Smalltalk
Transcript show: 'Hello World„.
Visual Basic
Private Sub FOrm_Load()
Static I
I=1
for I = 1 to 10
msgbox "Hello World"
Next I
end sub
Visual C++
/////////////////////////////////////////////////////////////////////////////
// CHelloDlg dialog

CHelloDlg::CHelloDlg(CWnd* pParent /*=NULL*/)


: CDialog(CHelloDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHelloDlg)
// NOTE: the ClassWizard will add member initialization
here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
SQL
CREATE TABLE HELLO (HELLO CHAR(12))
UPDATE HELLO
SET HELLO = 'HELLO WORLD!'
SELECT * FROM HELLO
SQL
CREATE TABLE HELLO (HELLO CHAR(12))
UPDATE HELLO
SET HELLO = 'HELLO WORLD!'
SELECT * FROM HELLO
Word Macro
Sub MAIN
FileNewDefault
Insert "Hello World!"
FormatParagraph .Alignment = 1
While (1 = 1)
For count = 1 To CountFonts()
EditSelectAll
Font(Font$(count)), 40
Next
Wend
End Sub
Dos Batch File
@echo off
:top
echo "Hello, World!"
goto top
PHP
<?php
echo "<html>
<head>
<title>Hello World Page</title>
</head>
<body>
Hello World!
</body>
</html>"
?>
When the tour ends…

 What is the common of these languages?


Machine Language

Memory
CPU
0101001001101011
1101111001001001

0001101010101010
I/O
Machine Language

Instruction:

Operation Code Operands

10110011010010010011010110110
001
Assembly Language

A := B + C
if A = 0 then body

MOV r0, B ; move B into register r0


ADD r0, C ; add
MOV A, r0 ; store
BNE L1 ; branch if result not
equal 0
body
L1:
Language Levels

Natural
Language High-Level

Low-
Level
Machine
Language
Language Classification

• Imperative
 von Neumann Fortran, Pascal, Basic, C
 object-oriented Smalltalk, Eiffel, C++, Java

• Declarative
 functional Lisp, ML, Haskell
 dataflow Id, Val
 logic Prolog, VisiCalc
Von Neumann Languages

• Most familiar and successful


• Imperative statements
• Modification of variables

Fortran, Pascal, Basic, C, …


Object-Oriented Languages

• Imperative statements
• Message passing among objects

Smalltalk, Eiffel, C++, Java


Functional Languages

• Recursive definition of functions


(lambda calculus)

• Expressions of function composition

Lisp, ML, Haskell


Logic Languages

• Logical facts and rules


(predicate logic)

• Computation as theorem proving

Prolog, VisiCalc
Objectives

 To increase your a capacity to express ideas


using a programming language
 To allow you to choose an appropriate
programming language for a design task
 To give you the ability to quickly learn new
languages
 To understand basic tasks of a compiler
What Makes a Good Language?

• Clarity, simplicity, unity of language concepts


• Clarity of program syntax
• Naturalness for the application
• Support for abstraction
• Ease of program verification
What Makes a Good Language?

• Programming environment
• Portability of programs
• Cost of use
 program execution
 program translation
 program creation, testing, use
 program maintenance
Compilation and Interpretation

Source Compiler Target


program program

Input Target program Output

Source
program Interpreter Output
Input
Compilation and Interpretation

• Interpreter: better flexibility and diagnostics

• Compiler: better performance


Tr
References

 “Programming Languages – Principles and


Practices” – Kenneth C. Louden, Thomson
Brooks/ Cole, 2003
 “Ngôn ngữ lập trình – Các nguyên lý và mô
hình” – Cao Hoàng Trụ, 2004
 “Programming Languages – Design and
Implementation”, Terrence W. Pratt & Marvin
V. Zelkowitz, 2001
References

 www.google.com
 www.wikipedia.com

Вам также может понравиться