Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 / 45 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2."— Presentation transcript:

1

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

3 2 / 45 Programming Language 2 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

4 3 / 45 Levels of Language in Computing 3

5 4 / 45 Level of Language Low-Level Language  Machine Language, Assembly High-Level Language  Fortran, Cobol, Pascal, C, VB, Java etc…

6 5 / 45 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

7 6 / 45 Language in Computing 6 แบ่งตามกลุ่มของภาษา Programming Language Query Language Markup Language Scripting Language

8 7 / 45 Programming Language 7 ภาษาที่สามารถใช้ควบคุมกำหนดพฤติกรรม การทำงานของคอมพิวเตอร์ (Flow control) โครงสร้างไวยกรณ์ (Syntax) และ ตีความหมาย (Semantic) แต่ละภาษาจะมีโครงสร้างของภาษา รูปแบบ ไวยากรณ์ และคำศัพท์ ที่ไม่เหมือนกัน แต่ หลักการของภาษา จะเหมือนกัน เช่น ภาษา Pascal, C, VB, Java

9 8 / 45 Query Language 8 ใช้สำหรับสอบถามหรือจัดการกับข้อมูลใน DBMS ภาษาที่ได้รับความนิยมสูงสุดคือ ภาษา สอบถามเชิงโครงสร้าง (Structure Query Language: SQL) มีรูปแบบคำสั่งคล้ายกับ ประโยคภาษาอังกฤษ ปัจจุบัน ภาษาสอบถามเชิงโครงสร้าง เป็น ภาษามาตรฐานสำหรับระบบการจัดการ ฐานข้อมูลเชิงสัมพันธ์ (Relational Database management System หรือ RDBMS) ซึ่งเป็นระบบ DBMS แบบที่ใช้กัน แพร่หลายที่สุดในปัจจุบัน

10 9 / 45 Markup Language 9 ภาษาประเภท Markup เป็น ภาษาคอมพิวเตอร์ที่แสดงทั้งข้อมูล และ รูปแบบการแสดงผลเข้าด้วยกัน ได้แก่ HTML, XHTML, XML

11 10 / 45 Scripting Language 10 code ที่เขียนจะถูกตีความ (Interpreted) และ execute ไปทีละคำสั่ง ผ่าน software พวก Script Engine ที่สนับสนุนภาษา script นั้นๆ Scripting Language เป็น interpreted language และต้องอาศัย run บนโปรแกรม อื่น เช่น Java script, PHP

12 11 / 45 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

13 12 / 45 Layered View of Computer The operating system and language implementation are layered over machine interface of a computer

14 13 / 45 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

15 14 / 45 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)

16 15 / 45 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

17 16 / 45 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

18 17 / 45 Source program Lexical analyzer Intermediate code generator (and semantic analyzer) Optimization Symbol table 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 Compilation Pure Interpretation Hybrid Implementation

19 18 / 45 Language Structures 1. Lexical (Token) 2. Syntactic 3. Contextual 4. Semantic

20 19 / 45 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 language int sum; float tax_rate; float taxRate

21 20 / 45 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, cout Prolog; repeat, print JAVA; class, System.out.println

22 21 / 45 1. Lexical Operators Special “keywords” used to identify operations to be performed on operands, e.g. math’s operators. Example: Prolog; +, -, *, /, /\, \/ JAVA; +, -, *, /, &&, ||, ==, !=

23 22 / 45 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

24 23 / 45 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”.

25 24 / 45 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; # comment VB; ‘ comment Prolog; % comment /* comment */

26 25 / 45 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.

27 26 / 45 1. Lexical Layout Example #include 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; }

28 27 / 45 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.

29 28 / 45 2. Syntactic

30 29 / 45 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).

31 30 / 45 4. Semantic The semantic level refers to the final overall meaning of a program.

32 31 / 45 Managing and Reducing Complexity 1. Problem Decomposition 2. Abstraction 3. Contextual Checking

33 32 / 45 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.

34 33 / 45 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.

35 34 / 45 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.

36 35 / 45 Programming Processing 1. Translation 2. Libraries 3. Macro processing 4. Debugging tools 5. Program Management Systems and Environments

37 36 / 45 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.

38 37 / 45 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.

39 38 / 45 1. Translation

40 39 / 45 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.

41 40 / 45 1. Translation

42 41 / 45 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 Defines core input and output functions Defines date and time handling functions

43 42 / 45 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

44 43 / 45 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.

45 44 / 45 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

46 45 / 45 Conclusion and Question


Download ppt "1 / 45 Language Design Principles and Programming Processing 886340 Principles of Programming Languages 2."

Similar presentations


Ads by Google