Anda di halaman 1dari 41

Principles of Programming Languages Introduction

Quan Thanh Tho, Ph.D. CSE HCMUT qttho@cse.hcmut.edu.vn

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
CPU Memory
0101001001101011 1101111001001001

I/O

0001101010101010

Machine Language
Instruction:
Operation Code Operands

10110011010010010011010110110 001

Assembly Language
A := B + C if A = 0 then body MOV r0, B ADD r0, C MOV A, r0 BNE L1 equal 0 ; ; ; ; move B into register r0 add store branch if result not

body
L1:

Language Levels
Natural Language

High-Level

Machine Language

LowLevel

Language Classification

Imperative

von Neumann object-oriented

Fortran, Pascal, Basic, C Smalltalk, Eiffel, C++, Java

Declarative

functional dataflow logic

Lisp, ML, Haskell Id, Val 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 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 program Input Source program Compiler Target program Target program Output

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 Ngn ng lp trnh Cc nguyn l v m hnh Cao Hong Tr, 2004 Programming Languages Design and Implementation, Terrence W. Pratt & Marvin V. Zelkowitz, 2001

References

www.google.com www.wikipedia.com

Assessment

Class exercise/ lab: 10% Assignment: 30% Mid-term: 10~20% Final: 40~50%

Course Web-site

www.cse.hcmut.edu.vn/elearning

Anda mungkin juga menyukai