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
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
3 / 53
3
Levels of Language in Computing
4 / 53
Level of Language
• Low-Level Language Machine Language, Assembly
•High-Level Language Fortran, Cobol, Pascal, C, VB, Java etc…
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
6 / 53
6
Language in Computing
แบ่�งตามกลุ่�มของภาษา • Programming Language • Query Language • Markup Language• Scripting Language
7 / 53
7
Programming Language
•ภาษาที่��สามารถใช้�ควบ่คมก�าหนดพฤต!กรรมการที่�างานของคอมพ!วเตอร# (Flow control) •โครงสร�างไวยกรณ์# (Syntax) แลุ่ะต�ความหมาย (Semantic)•แต�ลุ่ะภาษาจะม�โครงสร�างของภาษา ร*ปแบ่บ่ไวยากรณ์# แลุ่ะค�าศั-พที่# ที่��ไม�เหม.อนก-น แต�หลุ่-กการของภาษา จะเหม.อนก-น •เช้�น ภาษา Pascal, C, VB, Java
8 / 53
8
Query Language
•ใช้�ส�าหร-บ่สอบ่ถามหร.อจ-ดการก-บ่ข�อม*ลุ่ใน DBMS •ภาษาที่��ได�ร-บ่ความน!ยมส*งสดค.อ ภาษาสอบ่ถามเช้!งโครงสร�าง (Structure Query Language: SQL) ม�ร*ปแบ่บ่ค�าส-�งคลุ่�ายก-บ่ประโยคภาษาอ-งกฤษ•ป/จจบ่-น ภาษาสอบ่ถามเช้!งโครงสร�าง เป0นภาษามาตรฐานส�าหร-บ่ระบ่บ่การจ-ดการฐานข�อม*ลุ่เช้!งส-มพ-นธ์# (Relational Database management System หร.อ RDBMS) ซึ่4�งเป0นระบ่บ่ DBMS แบ่บ่ที่��ใช้�ก-นแพร�หลุ่ายที่��สดในป/จจบ่-น
9 / 53
9
Markup Language
•ภาษาประเภที่ Markup เป0นภาษาคอมพ!วเตอร#ที่��แสดงที่-5งข�อม*ลุ่ แลุ่ะร*ปแบ่บ่การแสดงผลุ่เข�าด�วยก-น ได�แก� HTML, XHTML, XML
10 / 53
10
Scripting Language
•code ที่��เข�ยนจะถ*กต�ความ (Interpreted) แลุ่ะ execute ไปที่�ลุ่ะค�าส-�ง ผ�าน software พวก Script Engine ที่��สน-บ่สนนภาษา script น-5นๆ•Scripting Language เป0น interpreted language แลุ่ะต�องอาศั-ย run บ่นโปรแกรมอ.�น •เช้�น Java script, PHP
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
12 / 53
Layered View of Computer
The operating system and language implementation are layered over machine interface of a computer
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
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)
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
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
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
18 / 53
Language Structures
1. Lexical (Token)2. Syntactic3. Contextual4. Semantic
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
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
21 / 53
1. Lexical
Operators•Special “keywords” used to identify operations to be performed on operands, e.g. math’s operators.
Example: Prolog; +, -, *, /, /\, \/JAVA; +, -, *, /, &&, ||, ==, !=
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
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”.
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 */
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.
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; }
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.
28 / 53
2. Syntactic
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).
30 / 53
4. Semantic•The semantic level refers to the final overall meaning
of a program.
31 / 53
Managing and Reducing Complexity
1. Problem Decomposition2. Abstraction3. Contextual Checking
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.
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.
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.
35 / 53
Programming Processing
1. Translation2. Libraries3. Macro processing4. Debugging tools5. Program Management Systems and Environments
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.
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.
38 / 53
1. Translation
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.
40 / 53
1. Translation
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
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
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.
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
45 / 53
Comparison of
programming languages
46 / 53
Differences Between C And C++
http://studytipsandtricks.blogspot.com/2012/05/15-most-important-differences-between-c.html
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++.
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.
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.
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.
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.
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.
53 / 53
Conclusionand
Question