Top Banner
1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2
53

1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

Jan 17, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

1 / 53

Language Design Principles and Programming

Processing

886340Principles of Programming Languages

2

Page 2: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

2 / 53

2

Programming Language

•A notation for communication to a computer what we want to do •A notation system for describing computation in •machine-readable Syntax, Semantics•human-readable form Abstraction

Page 3: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

3 / 53

3

Levels of Language in Computing

Page 4: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

4 / 53

Level of Language

• Low-Level Language Machine Language, Assembly

•High-Level Language Fortran, Cobol, Pascal, C, VB, Java etc…

Page 5: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

5 / 53

Requirement for PL Design

•Readable– comments, names, (…) syntax

•Simple to learn– Orthogonal - small number of concepts combine

regularly and systematically (without exceptions)•Portable

– language standardization•Abstraction

– control and data structures that hide detail•Efficient

Page 6: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

6 / 53

6

Language in Computing

แบ่�งตามกลุ่�มของภาษา • Programming Language • Query Language • Markup Language• Scripting Language

Page 7: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

7 / 53

7

Programming Language

•ภาษาที่��สามารถใช้�ควบ่คมก�าหนดพฤต!กรรมการที่�างานของคอมพ!วเตอร# (Flow control) •โครงสร�างไวยกรณ์# (Syntax) แลุ่ะต�ความหมาย (Semantic)•แต�ลุ่ะภาษาจะม�โครงสร�างของภาษา ร*ปแบ่บ่ไวยากรณ์# แลุ่ะค�าศั-พที่# ที่��ไม�เหม.อนก-น แต�หลุ่-กการของภาษา จะเหม.อนก-น •เช้�น ภาษา Pascal, C, VB, Java

Page 8: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

8 / 53

8

Query Language

•ใช้�ส�าหร-บ่สอบ่ถามหร.อจ-ดการก-บ่ข�อม*ลุ่ใน DBMS •ภาษาที่��ได�ร-บ่ความน!ยมส*งสดค.อ ภาษาสอบ่ถามเช้!งโครงสร�าง (Structure Query Language: SQL) ม�ร*ปแบ่บ่ค�าส-�งคลุ่�ายก-บ่ประโยคภาษาอ-งกฤษ•ป/จจบ่-น ภาษาสอบ่ถามเช้!งโครงสร�าง เป0นภาษามาตรฐานส�าหร-บ่ระบ่บ่การจ-ดการฐานข�อม*ลุ่เช้!งส-มพ-นธ์# (Relational Database management System หร.อ RDBMS) ซึ่4�งเป0นระบ่บ่ DBMS แบ่บ่ที่��ใช้�ก-นแพร�หลุ่ายที่��สดในป/จจบ่-น

Page 9: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

9 / 53

9

Markup Language

•ภาษาประเภที่ Markup เป0นภาษาคอมพ!วเตอร#ที่��แสดงที่-5งข�อม*ลุ่ แลุ่ะร*ปแบ่บ่การแสดงผลุ่เข�าด�วยก-น ได�แก� HTML, XHTML, XML

Page 10: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

10 / 53

10

Scripting Language

•code ที่��เข�ยนจะถ*กต�ความ (Interpreted) แลุ่ะ execute ไปที่�ลุ่ะค�าส-�ง ผ�าน software พวก Script Engine ที่��สน-บ่สนนภาษา script น-5นๆ•Scripting Language เป0น interpreted language แลุ่ะต�องอาศั-ย run บ่นโปรแกรมอ.�น •เช้�น Java script, PHP

Page 11: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

11 / 53

Implementation Methods

•Compilation•Programs are translated into machine language•Use: Large commercial applications

•Pure Interpretation•Programs are interpreted by another program known as an interpreter•Use: Small programs or when efficiency is not an issue

•Hybrid Implementation Systems•A compromise between compilers and pure interpreters•Use: Small and medium systems when efficiency is not the first concern

Page 12: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

12 / 53

Layered View of Computer

The operating system and language implementation are layered over machine interface of a computer

Page 13: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

13 / 53

Compilation

• Translate high-level program (source language) into machine code (machine language)• Slow translation, fast execution• Compilation process has several phases: • lexical analysis: converts characters in the source program into lexical units•syntax analysis: transforms lexical units into parse trees which represent the syntactic structure of program•Semantics analysis: generate intermediate code•code generation: machine code is generated

Page 14: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

14 / 53

Pure Interpretation

•No translation• Easier implementation of programs (run-time errors can easily and immediately be displayed)• Slower execution (10 to 100 times slower than compiled programs)•Often requires more space•Now rare for traditional high-level languages• Significant comeback with some Web scripting languages (e.g., JavaScript, PHP)

Page 15: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

15 / 53

•A compromise between compilers and pure interpreters•A high-level language program is translated to an

intermediate language that allows easy interpretation• Faster than pure interpretation• Examples• Perl programs are partially compiled to detect errors

before interpretation

Hybrid Implementation Systems

Page 16: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

16 / 53

• Examples• Initial implementations of Java were hybrid; the

intermediate form, byte code, provides portability to any machine that has a byte code interpreter and a run-time system (together, these are called Java Virtual Machine)

Hybrid Implementation Systems

Page 17: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

17 / 53

Source program

Lexical analyzer

Intermediate code generator

(and semantic analyzer)Optimization

Symboltable

Code generator

Computer

Lexical units

Parse tree

Intermediate code

Machine language

Input data

Results

Syntax analyzer

Source program

Input data

Results

Lexical analyzer

Intermediate code generator

Lexical units

Parse tree

Intermediate code

Syntax analyzer

Interpreter

CompilationPure Interpretation

Hybrid Implementation

Page 18: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

18 / 53

Language Structures

1. Lexical (Token)2. Syntactic3. Contextual4. Semantic

Page 19: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

19 / 53

1. Lexical

Identifiers•Names chosen to represent data items •Name variables, method, class, functions and

procedures, etc.•Considerations:•Case sensitivity•Number of characters

Example: C languageint sum;float tax_rate;float taxRate

Page 20: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

20 / 53

1. Lexical

Keywords•Names chosen by the language designer to

represent facets of particular language constructs which cannot be used as identifiers (sometimes referred to as reserved words).

Example: C++; for, while, do, cin, coutProlog; repeat, printJAVA; class, System.out.println

Page 21: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

21 / 53

1. Lexical

Operators•Special “keywords” used to identify operations to be performed on operands, e.g. math’s operators.

Example: Prolog; +, -, *, /, /\, \/JAVA; +, -, *, /, &&, ||, ==, !=

Page 22: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

22 / 53

Separators• Punctuation marks used to group together sequences of tokens that

have a “unit” meaning.

•When outputting text it is often desirable to include punctuation, where these are also used (within the language) as separators we must precede the punctuation character with what is called an escape character (usually a backslash ‘\’).• Example: C

int a; for (a=1; a<10 ; a++){

printf("\"Hello world\"");printf("\n");

}

1. Lexical

Page 23: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

23 / 53

1. Lexical

Literals•Denote direct values, can be:•Numeric, e.g. 1, -123, 3.14, 6.02e23. Character, e.g. ‘a’.String, e.g. “Some text”.

Page 24: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

24 / 53

1. Lexical

Comments•A good program is one that is understandable. •We can increase understandability by including

meaningful comments into our code.•Comments are omitted during processing.•The start of a comment is typically indicated by a

keyword (e.g. comment) or a separator, and may also be ended by a separator.

Example: Python; # commentVB; ‘ commentProlog; % comment /* comment */

Page 25: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

25 / 53

1. Lexical

Layout•Generally speaking layout (indentation etc.) is

unimportant in the context of programming languages.•White space (spaces, tabs and new lines) are usually

ignored.•A good layout does however enhance readability,

and consequently understand ability.•A good layout can also reduce the risk of

programming errors.

Page 26: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

26 / 53

1. Lexical

LayoutExample#include <stdio.h> int main( ) {int num1, num2, sum; printf("Enter two integers: "); scanf("%d %d",&num1,&num2); sum=num1+num2; printf("Sum: %d /n", sum); return 0; }

Page 27: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

27 / 53

2. Syntactic

•The syntactic level describes the way that program statements are constructed from tokens.•This is always very precisely defined in terms of a

context free grammar.•The best known examples are BNF (Backus Naur

Form) or EBNF (Extended Backus Naur Form).•Syntax may also be described using a syntax tree.

Page 28: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

28 / 53

2. Syntactic

Page 29: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

29 / 53

3. Contextual

• The contextual level of analysis is concerned with the “context” in which program statements occur.• Program statements usually contain identifiers whose value

is dictated by earlier statements (especially in the case of the imperative or OO paradigms). • Context also determines whether a statement is “legal” or

not (context conditions – a data item must “exist” before it can be used).

Page 30: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

30 / 53

4. Semantic•The semantic level refers to the final overall meaning

of a program.

Page 31: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

31 / 53

Managing and Reducing Complexity

1. Problem Decomposition2. Abstraction3. Contextual Checking

Page 32: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

32 / 53

1. Problem Decomposition

•Divide and conquer (divide et impera).•Problem decomposition hinges on procedures,

recursion and parameter passing , and can be applied in most (high level) programming languages.

Page 33: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

33 / 53

2. Abstraction

• Ignoring irrelevant detail in a safe way (information hiding).•Requires the use of an “interface” to abstract away

from lower level detail. •Abstraction is typically facilitated through the use of

packages or modules.

Page 34: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

34 / 53

3. Contextual Checking

•Contextual checking is concerned with the contextual correctness of program code.• Ideally we would like to check for (and eradicate) all

possible “run time” errors, however contextual checking is a difficult undertaking and in some cases (e.g. recursion) completely impractical.•Contextual checking consists (typically) of parameter

and identifier validation.

Page 35: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

35 / 53

Programming Processing

1. Translation2. Libraries3. Macro processing4. Debugging tools5. Program Management Systems and Environments

Page 36: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

36 / 53

1. Translation

•A program written in a high level language (source code) can only be run in its machine code equivalent format. •There are two ways of achieving this:•Compilation, and Interpretation.

Page 37: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

37 / 53

1. Translation

Compilation •Compilation requires the use of a special program

(called a compiler) that translates source code into object code.•Sometimes the object code cannot be directly

executed. •Various library files must be “linked in” using another

special program called a linker, which produces executable code.•Again various contextual checks are made during

compilation.

Page 38: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

38 / 53

1. Translation

Page 39: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

39 / 53

1. Translation

Interpretation• Interpretation requires the use of a special program that reads and reacts to source code. •Such a program is called an interpreter.•During interpretation run-time errors may be detected and “meaningful” error messages produced.

Page 40: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

40 / 53

1. Translation

Page 41: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

41 / 53

2. Libraries

•Libraries (in computer programming terms) contain chunks of precompiled (object) code for various functions and procedures that come with a programming language that requires compilation.

• For example functions and procedures to facilitate I/O.• In C• <stdio.h> Defines core input and output functions• <time.h> Defines date and time handling functions

Page 42: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

42 / 53

3. Macro processing

•During macro preprocessing all occurrences of the name within the program are replaced by the string before interpretation/compilation takes place. •Use of macros offers the advantage of enhanced

readability.•However it is also argued that a well designed

language should not require the use of macros!

• Example: C#define PI 3.141#define NUM 500

Page 43: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

43 / 53

4. Debugging tools

•To assist in error detection many debugging tools exist.•Some of these allow the user to analyze the core

dump that occurs in the event of a fatal error. (A core dump describes the “state” of a program when a fatal error occurs).•Others allow programmers to step through and

execute a program line by line to support analysis of its execution.

Page 44: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

44 / 53

5. Program Management Systems and Environments

• some vendors have combined a text editor with a compiler/interpreter into a single dedicated programming environment for the production of code in a particular programming language.•Such environments include a program management

systems and other “administrative” tools (e.g. version control).• IDE (Integrated Development Environment )•Eclipse•Code::Block•NetBeans

Page 45: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

45 / 53

Comparison of 

programming languages

Page 46: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

46 / 53

Differences Between C And C++

http://studytipsandtricks.blogspot.com/2012/05/15-most-important-differences-between-c.html

Page 47: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

47 / 53

Differences Between C And C++

Basic Introduction:• C++ is derived from C Language. It is a Superset of C. • Earlier C++ was known as C with classes. • In C++, the major change was the addition of classes

and a mechanism for inheriting class objects into other classes. •Most C Programs can be compiled in C++ compiler. • C++ expressions are the same as C expressions. •All C operators are valid in C++.

Page 48: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

48 / 53

Differences Between C And C++

C C++1. C is Procedural Language. 1. C++ is non Procedural i.e

Object oriented Language.2. No virtual Functions are present in C

2. The concept of virtual Functions are used in C++.

3. In C, Polymorphism is not possible.

3. The concept of polymorphism is used in C++.Polymorphism is the most Important Feature of OOPS.

Page 49: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

49 / 53

Differences Between C And C++

C C++4. Operator overloading is not possible in C.

4. Operator overloading is one of the greatest Feature of C++.

5. Top down approach is used in Program Design.

5. Bottom up approach adopted in Program Design.

6. No namespace Feature is present in C Language.

6. Namespace Feature is present in C++ for avoiding Name collision.

7. Multiple Declaration of global variables are allowed.

7. Multiple Declaration of global varioables are not allowed.

Page 50: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

50 / 53

Differences Between C And C++

C C++8. scanf() Function used for Input.printf() Function used for output.

8. Cin>> Function used for Input.Cout<< Function used for output.

9. Mapping between Data and Function is difficult and complicated.

9. Mapping between Data and Function can be used using "Objects"

10. In C, we can call main() Function through other Functions

10. In C++, we cannot call main() Function through other functions.

Page 51: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

51 / 53

Differences Between C And C++

C C++11. C requires all the variables to be defined at the starting of a scope.

11. C++ allows the declaration of variable anywhere in the scope i.e at time of its First use.

12. No inheritance is possible in C.

12. Inheritance is possible in C++

13. In C, malloc() and calloc() Functions are used for Memory Allocation and free() function for memory Deallocating.

13.In C++, new and delete operators are used for Memory Allocating and Deallocating.

Page 52: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

52 / 53

Differences Between C And C++

C C++14. It supports built-in and primitive data types.

14. It support both built-in and user define data types.

15. In C, Exception Handling is not present.

15. In C++, Exception Handling is done with Try and Catch block.

Page 53: 1 / 53 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2.

53 / 53

Conclusionand

Question