©J.Tiberghien - ULB-VUB Version 2003-2004 1 Troisième Partie Chapitre 1 Les supports à la programmation.

Slides:



Advertisements
Similar presentations
COMPILER CONSTRUCTION
Advertisements

Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00.
CPSC 388 – Compiler Design and Construction
Introduction To Compilers And Phase 1 Inside a compiler. Inside a C-- compiler. The compilation process. Example C-- code. Extended Backus-Naur.
Compilers and Language Translation
CPSC Compiler Tutorial 9 Review of Compiler.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter3: Language Translation issues
Chapter 9 Compilers and Language Translation. The Compilation Process Phase I: Lexical analysis Phase I: Lexical analysis Phase II: Parsing Phase II:
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
CS 330 Programming Languages 09 / 11 / 2007 Instructor: Michael Eckmann.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Compiled by Benjamin Muganzi 3.2 Functions and Purposes of Translators Computing 9691 Paper 3 1.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Invitation to Computer Science 5th Edition
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
CIS Computer Programming Logic
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
COP4020 Programming Languages
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
©J.Tiberghien - ULB-VUB Version 2007 Première partie, chap. 4, page 1 Chapitre 1.4 Langages et outils de programmation.
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.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 3, 09/11/2003 Prof. Roy Levow.
Lexical and Syntax Analysis
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
CPS 506 Comparative Programming Languages Syntax Specification.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
D Goforth COSC Translating High Level Languages.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
Introduction to Code Generation and Intermediate Representations
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.4 Programming Languages and Programming.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Introduction to Compiling
Introduction CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
Chapter 1: Introduction 1 Compiler Designs and Constructions Chapter 1: Introduction Objectives: Course Objectives Introduction Dr. Mohsen Chitsaz.
Compilers I CNS History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
Chapter 2.2 Programming Language Definition. Summary Syntax and Semantics Semantics definition –Denotational Semantics –Elementary actions Syntax definitions.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Language Implementation Overview John Keyser Spring 2016.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Chapter 3 – Describing Syntax
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
Compiler Construction (CS-636)
Compiler Lecture 1 CS510.
Compiler Designs and Constructions
Introduction to System Programming
CS 3304 Comparative Languages
CS 3304 Comparative Languages
Chapter 10: Compilers and Language Translation
Discrete Maths 13. Grammars Objectives
Presentation transcript:

©J.Tiberghien - ULB-VUB Version Troisième Partie Chapitre 1 Les supports à la programmation

©J.Tiberghien - ULB-VUB Version Programming languages Machine Language Assembler C++ Source code Object code High Level Language Low Level Languages HARDWARE

©J.Tiberghien - ULB-VUB Version Data Memory Arithmetic Unit Control Unit Program Memory (from 11) 100: ND 102: SC 1:KFL 2:KDA 3:DDA *0# Machine Language

©J.Tiberghien - ULB-VUB Version Assembler Language LabelOperationOperandsComments ORG100First address of data memory NDDAT2Number of entered digits, 2 bytes SCDAT2Secret Code as entered, 2 bytes KFLEQU1Keyboard flag KDAEQUKFL+1Keyboard data DDAEQU3Door data ORG10First address of program memory BGNCOPY#0,NDInitialise number of entered digits COPY#0,SCInitialise secret code TFLEQ?KFL,#0,TFLTest continuously for key stroke MULSC,#10,SCShift SC one digit to the left ADDSC,KDA,SCAdd newly entered digit to SC COPY#0,KFLReset Keyboard flag ADDND,#1,NDIncrease number of entered digits NE?ND,#3,TFLAny more digits needed ? NE?SC,#321,BGNIs the entered secret code correct ? COPY#1,DDAOpen the door JMPBGNRestart everything END

©J.Tiberghien - ULB-VUB Version KFL = KDA = DDA = *0# 789 Main() {int sc = 0; const int Key = 321; bool *KFL = 1; int *KDA = KFL+1; bool *DDA = 3; while (true) {for (int nd=1;nd<=3;++nd) {while (*KFL == 0); sc = sc*10+*KDA; } if (sc == Key) *DDA = 1; } High Level language

©J.Tiberghien - ULB-VUB Version HARDWARE INTERPRETER Source Code (LLL or HLL) Translator Object Code Transforming Source Code

©J.Tiberghien - ULB-VUB Version Translators vs. Interpreters Translators –Translate the entire program at once –Program execution very fast –Poor run-time error messages Interpreters –Translate and execute statement after statement –Very slow execution –Good run-time error messages

©J.Tiberghien - ULB-VUB Version Macros … LDA+7,U LDB+5,U MUL STD+2,U LDA+6,U LDB+5,U MUL ADD+1,U STD+1,U LDA+6,U LDB+5,U MUL ADD+1,U... MULBMDEF LDA&1 LDB&2 MUL MEND … MULB+7,U;+5,U STD+2,U MULB+6,U;+5,U ADD+1,U STD+1,U MULB+6,U;+5,U ADD+1,U...

©J.Tiberghien - ULB-VUB Version Macros in HLL /* The Program: */main(void) { # include “one” } /* file one */printf(“ from 1.\n”); # include “two” /* file two */printf(“ from 2.\n”); from 1. from 2.

©J.Tiberghien - ULB-VUB Version Macro Expanders Source Object Macro Expander Main module Macro Library Assembler Compiler Expanded Source Code

©J.Tiberghien - ULB-VUB Version Macros vs. Functions MACROS –Static Expansion –Waste of memory –Fast –Powerful parameter passing, even in assembler FUNCTIONS –Dynamic Expansion –Save memory space –Slow –Programmer friendly parameter passing not available in assembler

©J.Tiberghien - ULB-VUB Version HARDWARE INTERPRETER Source Code (HLL) COMPILER Object Code Translating HLLs

©J.Tiberghien - ULB-VUB Version Syntax and Semantics Syntax = form Semantics = meaning Example –My beer is on the table Syntax : OK Semantics : OK –My table is on the beer Syntax : OK Semantics : You had too many beers !!!

©J.Tiberghien - ULB-VUB Version Syntax Definition : Backus Naur Formalism (BNF) Terminal Symbols –Appear literally in program text –Examples : “a”, “3”, “END”, “>”, “:=” Non-terminal Symbols –Name given to set of terminal symbols –Written between –Defined by a metalinguistic expression –Examples :,,, Metalinguistic expression –Formal definition (::=) of a non-terminal symbol –Contains terminal and non-terminal symbols –Expresses juxtaposition or choice (the | operator) –Example : ::= 0|1|2|3|4|5|6|7|8|9

©J.Tiberghien - ULB-VUB Version BNF Examples (1) ::= A|B|C|D|E| |X|Y|Z ::= a|b|c|d|e| |x|y|z ::= | ::= 1|2|3|4|5|6|7|8|9|0 ::= | ::= | + | - ::= | |

©J.Tiberghien - ULB-VUB Version ::= | Is 123 a ? Is it a ? Certainly not

©J.Tiberghien - ULB-VUB Version Is 123 a ? Is it a followed by a ? Is 23 a ? ::= |

©J.Tiberghien - ULB-VUB Version Is 123 a ? Is it a followed by a ? Is 23 a ? Is it a ? Certainly not ::= |

©J.Tiberghien - ULB-VUB Version Is 123 a ? Is it a followed by a ? Is 23 a ? Is it a followed by a ? Is 3 a ? ::= |

©J.Tiberghien - ULB-VUB Version Is 123 a ? Is it a followed by a ? Is 23 a ? Is it a followed by a ? Is 3 a ? Is it a ? YES ::= |

©J.Tiberghien - ULB-VUB Version Is 123 a ? Is it a followed by a ? Is 23 a ? Is it a followed by a ? Is 3 a ? Is it a ? YES As a consequence, 3 is a ::= |

©J.Tiberghien - ULB-VUB Version Is 123 a ? Is it a followed by a ? Is 23 a ? Is it a followed by a ? Is 3 a ? Is it a ? YES As a consequence, 3 is a As a consequence, 23 is a ::= |

©J.Tiberghien - ULB-VUB Version Is 123 a ? Is it a followed by a ? Is 23 a ? Is it a followed by a ? Is 3 a ? Is it a ? YES As a consequence, 3 is a As a consequence, 23 is a As a consequence, 123 is a ::= |

©J.Tiberghien - ULB-VUB Version ::= | | Correct identifiers –Length, Width, Cost,... –MySalary, NumberOfStudents, Year1, … –X1, Y2, A1, A5, … Incorrect identifiers –My Salary, Number-of-students, … –1stYear,...

©J.Tiberghien - ULB-VUB Version ::= | | Correct identifiers –Length, Width, Cost,... –MySalary, NumberOfStudents, Year1, … –X1, Y2, A1, A5, … Incorrect identifiers –My Salary, Number-of-students, … –1stYear,...

©J.Tiberghien - ULB-VUB Version Syntax Definition BNF Non-recursive BNF Syntax diagram Definition Choice 0 or 1 occurrence 0, 1 or many occurrences ::= Diagram [] {}

©J.Tiberghien - ULB-VUB Version Syntax Definition BNF Non-recursive BNF Syntax diagram Definition Choice 0 or 1 occurrence 0, 1 or many occurrences ::= Diagram [ ] {}

©J.Tiberghien - ULB-VUB Version ::= A|B|C|D|E| |X|Y|Z ::= a|b|c|d|e| |x|y|z ::= | ::= 1|2|3|4|5|6|7|8|9|0 ::= { } ::= [ +|- ] ::= { | } BNF Examples (2)

©J.Tiberghien - ULB-VUB Version Backus Naur Formalism (BNF) Terminal Symbols Non-terminal Symbols Metalinguistic expression –Railroad diagrams with semaphores requiring specific symbols to grant passage. –The defined non-terminal symbol leaves all its symbols, in the order of occurrence, at appropriate semaphores and reaches the exit with none left. Terminal symbol Non-terminal symbol

©J.Tiberghien - ULB-VUB Version Syntax Definition BNF Non-recursive BNF Syntax diagram Definition Choice 0 or 1 occurrence 0, 1 or many occurrences ::= Diagram [] {}

©J.Tiberghien - ULB-VUB Version Syntax Diagrams Examples UpperCaseLetter ABCDEVWXYZ Letter LowerCaseLetter abcdevwxyz UpperCaseLetter LowerCaseLetter

©J.Tiberghien - ULB-VUB Version Syntax Diagrams Examples Digit CardinalNumber ::= { } ::= 0|1|2|3|4|5|6|7|8|9 Digit

©J.Tiberghien - ULB-VUB Version Syntax Diagrams Examples IntegerNumber ::= [ + | - ] CardinalNumber + -

©J.Tiberghien - ULB-VUB Version Syntax Diagrams Examples Identifier ::= { | } Letter Digit Letter

©J.Tiberghien - ULB-VUB Version Source Code (HLL) The COMPILER Object Code HARDWARE COMPILER = Lexical analyzer Syntax analyzer Code generator Code optimizer

©J.Tiberghien - ULB-VUB Version The Lexical Analyzer Recognizes lexical tokens such as: –Identifiers –Numbers : -200, 6.3E23, … –Reserved words : for; if; –Multi-character symbols : /*; */; == ; != ;... Prepares the symbol table with all identifiers

©J.Tiberghien - ULB-VUB Version The Syntax Analyzer (= syntax parser) Recognizes non-terminal symbols such as: –a program : –a variable declaration : –a function declaration : –an expression Can be generated automatically by a parser generator, starting from the BNF syntax definitions Represents a program by a syntactical tree and a symbol table.

©J.Tiberghien - ULB-VUB Version Error detection and correction Both the lexical and syntactical analyzer should check the source code against syntax errors. When an error has been found, the syntax analyzer should –generate an error message and stop or –generate an error message –try to correct by guessing the programmers intentions –resume the syntax analysis A compiler should never generate any code as long as syntax errors are present in the source code, even if the syntax analyzer has corrected them in order to continue the analysis.

©J.Tiberghien - ULB-VUB Version Multi-pass compiler Source Object Lexical analysis Symbol table Int.1 Syntax analysis Code generation Optimization

©J.Tiberghien - ULB-VUB Version Single - pass compiler Source Object Symbol table Syntax analysis Code generation Lexical analysis

©J.Tiberghien - ULB-VUB Version Interpreters Source Symbol table Syntax analysis Simulated code execution Lexical analysis