1 Programming Languages Fundamentals Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.

Slides:



Advertisements
Similar presentations
Programming Languages Third Edition Chapter 6 Syntax.
Advertisements

Introduction to Programming Languages Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
Copyright © 2005 Elsevier Imperative languages Group languages as –imperative von Neumann(Fortran, Pascal, Basic, C) object-oriented(Smalltalk, Eiffel,
Compilers and Language Translation
CS 355 – Programming Languages
CPSC Compiler Tutorial 9 Review of Compiler.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh.
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Overview of Programming Paradigms
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Summer 02-03Programming Language Concepts1 Programming Language Concepts (CS 360) Lecture 1: Overview, Grammars, and Little Languages Jeremy R. Johnson.
Chapter 17 Programming Tools The Architecture of Computer Hardware and Systems Software: An Information Technology Approach 3rd Edition, Irv Englander.
CSE 425: Intro to Programming Languages and their Design A Few Key Ideas No particular language is a prerequisite for this course –However you should be.
PZ01A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ01A -- Introduction Programming Language Design and.
Invitation to Computer Science 5th Edition
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Programming Languages and Design Lecture 1 Introduction to Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern University,
COP4020 Programming Languages
Chapter 1. Introduction.
CS 363 Comparative Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
CSC 338: Compiler design and implementation
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
1 Introduction Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
CPS 506 Comparative Programming Languages Syntax Specification.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
ISBN Chapter 3 Describing Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
Syntax and Semantics CIS 331 Syntax: the form or structure of the expressions, statements, and program units. Semantics: the meaning of the expressions,
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
Languages and Compilers
Introduction CPSC 388 Ellen Walker Hiram College.
Programming Languages
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
ISBN Chapter 3 Describing Syntax and Semantics.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
1 CS Programming Languages Class 04 September 5, 2000.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Advanced programming language theory. week 2. Attribute grammars and semantics.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Describing Syntax and Semantics
CS 326 Programming Languages, Concepts and Implementation
课程名 编译原理 Compiling Techniques
Lecture 2: General Structure of a Compiler
COP4020 Programming Languages
Programming Languages 2nd edition Tucker and Noonan
Principles of Programming Languages
강의 내용 및 방법 접근방법 리포트 시험 Lambda Calculus, Proof of Correctness
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Presentation transcript:

1 Programming Languages Fundamentals Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM

2 Contact Trần Giang Sơn

3 References Ngôn ngữ lập trình – các mô hình và nguyên lý, Cao Hoàng Trụ, Khoa Khoa học máy tính, Đại học Bách Khoa, 1992 Programming languages: design and implementation, second edition, Terrence W.Partt, Printice - Hall International Editions Fundamentals of programming languages, Ellis Horowits, 1983, Computer Science Press

4 Contents Evolution and classification Formal syntax and semantics Compilation and interpretation

5 Machine Language CPU I/O Memory

6 Machine Language Operation CodeOperands Instruction:

7 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:

8 Language Levels Natural Language Machine Language Low-Level High-Level

9 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

10 What Makes a Good Language? Programming environment Portability of programs Cost of use  program execution  program translation  program creation, testing, use  program maintenance

11 Language Classification Imperative  von NeumannFortran, Pascal, Basic, C  object-orientedSmalltalk, Eiffel, C++, Java Declarative  functionalLisp, ML, Haskell  dataflowId, Val  logicProlog, VisiCalc

12 Von Neumann Languages Most familiar and successful Imperative statements Modification of variables Fortran, Pascal, Basic, C, …

13 Object-Oriented Languages Imperative statements Message passing among objects Smalltalk, Eiffel, C++, Java

14 Functional Languages Recursive definition of functions (lambda calculus) Expressions of function composition Lisp, ML, Haskell

15 Logic Languages Logical facts and rules (predicate logic) Computation as theorem proving Prolog, VisiCalc

16 Dataflow Languages Computation as token flow among nodes Inherently parallel model Id, Val

17 Contents Evolution and classification Formal syntax and semantics Compilation and interpretation

18 Formal Syntax and Semantics Computer languages must be precise Both their form (syntax) and meaning (semantics) must be specified without ambiguity Both programmers and computers can tell what a program is supposed to do

19 Formal Syntax Abstract syntax Context-free grammars Backus-Naur formalism (BNF) Syntax diagrams Derivations and parse trees

20 Abstract Syntax Syntactic class Syntactic form

21 Example: Expressions Syntactic class: Eexpression Iidentifier Cconstant Ooperator Syntactic form: E = I | C | E O E | (E)

22 Example: Expressions a * (2 + b) E O E

23 Example: Expressions a - b - c E O E

24 Abstract Syntax Advantage: simple Disadvantages:  No terminal symbols defined  Ambiguous

25 Context-Free Grammars Start symbol Non-terminals Terminals Productions A   1 |  2 | … |  n (Noam Chomsky, 1959)

26 Example: Unsigned Integers

27 Example: Unsigned Integers Start symbol Non-terminals, Terminals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Productions  |

28 Backus-Naur Formalism ::= | (John Backus, 1960)

29 Example: Expressions 12 * 3 + 4

30 Example: Expressions Start symbol Non-terminals,,,,, Terminals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, , *, /

31 Example: Expressions Productions:  |  | ( )  + |    | 

32 Syntax Diagrams term expressionterm_opterm expression

33 Derivations   +   + 4    12  3 + 4

34 Parse Trees *

35 Parse Trees *

36 Formal Semantics Operational semantics Denotational semantics Axiomatic semantics

37 Operational Semantics A virtual computer to execute a program. A set of formally defined operations to specify how the internal state of the virtual computer may change.

38 Denotational Semantics Each program construct is a function that maps an input to an output. A program is a composition of functions.

39 Axiomatic Semantics The effect of a statement is defined via its precondition and postcondition. A set of axioms and rules to define the effect of program constructs.

40 Axiomatic Semantics {P} S {Q} preconditionstatementpostcondition

41 Axiomatic Semantics {P x  E } x := E {P} Axiom:

42 Axiomatic Semantics {x  2} x := x + 1 {x  3} E = x + 1 P = x > 3 P x  E = x + 1 > 3 = x > 2

43 Axiomatic Semantics if ({P} S 1 {Q})  ({Q} S 2 {R}) then {P} S 1 ; S 2 {R} Rule:

44 Contents Evolution and classification Formal syntax and semantics Compilation and interpretation

45 Compilation and Interpretation Compiler Source programTarget program InputOutput Interpreter Source programOutput Input

46 Compilation and Interpretation Interpreter: better flexibility and diagnostics Compiler: better performance

47 Phases of Compilation Scanner (lexical analysis) Parser (syntactic analysis) Semantic analysis Machine-independent code optimisation Target code generation Machine-specific code optimization Character stream Token stream Parse tree Intermediate code Optimised intermediate code Target code Optimised target code

48 Phases of Compilation Scanner (lexical analysis) Parser (syntactic analysis) c := a + b  7 id 1 := id 2 + id 3  7

49 Phases of Compilation Parser (syntactic analysis) id 1 := id 2 + id 3  7 := id 1 id 2 id 3 +  7

50 Phases of Compilation Semantic analysis CNV (7,, t 1 )  (id 3, t 1, t 2 ) + (id 2, t 2, t 3 ) ASS (t 3,, id 1 ) := id 1 id 2 id 3 +  7

51 Phases of Compilation CNV (7,, t 1 )  (id 3, t 1, t 2 ) + (id 2, t 2, t 3 ) ASS (t 3,, id 1 ) Machine-independent code optimisation  (id 3, 7.0, t 1 ) + (id 2, t 1, id 1 )

52 Phases of Compilation MOV reg, id 3 MUL reg, 7.0 ADD reg, id 2 MOV id 1, reg  (id 3, 7.0, t 1 ) + (id 2, t 1, id 1 ) Target code generation

53 Exercises Define a formal syntax for a simple language supporting only the assignment statement and arithmetic expressions. Write the derivation and draw the parse tree of ‘ c := (a + b)  7 ’ using the defined syntax.