Download presentation
Presentation is loading. Please wait.
Published byGrant Hunter Modified over 9 years ago
1
©J.Tiberghien - ULB-VUB Version 2003-2004 1 Troisième Partie Chapitre 1 Les supports à la programmation
2
©J.Tiberghien - ULB-VUB Version 2003-2004 2 Programming languages Machine Language Assembler C++ Source code Object code High Level Language Low Level Languages HARDWARE
3
©J.Tiberghien - ULB-VUB Version 2003-2004 3 Data Memory Arithmetic Unit Control Unit Program Memory (from 11) 100: ND 102: SC 1:KFL 2:KDA 3:DDA 456 123 *0# 789 10 0100 11 10 0102 12 40 1012 13 22 10210102 1420102 2 16 20 1011 17 41 101312 18 41 232110 19 10 1 3 15 10 0 1 20 47 10 Machine Language
4
©J.Tiberghien - ULB-VUB Version 2003-2004 4 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
5
©J.Tiberghien - ULB-VUB Version 2003-2004 5 KFL = KDA = DDA = 456 123 *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
6
©J.Tiberghien - ULB-VUB Version 2003-2004 6 HARDWARE INTERPRETER Source Code (LLL or HLL) Translator Object Code Transforming Source Code
7
©J.Tiberghien - ULB-VUB Version 2003-2004 7 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
8
©J.Tiberghien - ULB-VUB Version 2003-2004 8 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...
9
©J.Tiberghien - ULB-VUB Version 2003-2004 9 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.
10
©J.Tiberghien - ULB-VUB Version 2003-2004 10 Macro Expanders Source Object Macro Expander Main module Macro Library Assembler Compiler Expanded Source Code
11
©J.Tiberghien - ULB-VUB Version 2003-2004 11 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
12
©J.Tiberghien - ULB-VUB Version 2003-2004 12 HARDWARE INTERPRETER Source Code (HLL) COMPILER Object Code Translating HLLs
13
©J.Tiberghien - ULB-VUB Version 2003-2004 13 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 !!!
14
©J.Tiberghien - ULB-VUB Version 2003-2004 14 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
15
©J.Tiberghien - ULB-VUB Version 2003-2004 15 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 ::= | ::= | + | - ::= | |
16
©J.Tiberghien - ULB-VUB Version 2003-2004 16 ::= | Is 123 a ? Is it a ? Certainly not
17
©J.Tiberghien - ULB-VUB Version 2003-2004 17 Is 123 a ? Is it a followed by a ? Is 23 a ? ::= |
18
©J.Tiberghien - ULB-VUB Version 2003-2004 18 Is 123 a ? Is it a followed by a ? Is 23 a ? Is it a ? Certainly not ::= |
19
©J.Tiberghien - ULB-VUB Version 2003-2004 19 Is 123 a ? Is it a followed by a ? Is 23 a ? Is it a followed by a ? Is 3 a ? ::= |
20
©J.Tiberghien - ULB-VUB Version 2003-2004 20 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 ::= |
21
©J.Tiberghien - ULB-VUB Version 2003-2004 21 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 ::= |
22
©J.Tiberghien - ULB-VUB Version 2003-2004 22 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 ::= |
23
©J.Tiberghien - ULB-VUB Version 2003-2004 23 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 ::= |
24
©J.Tiberghien - ULB-VUB Version 2003-2004 24 ::= | | Correct identifiers –Length, Width, Cost,... –MySalary, NumberOfStudents, Year1, … –X1, Y2, A1, A5, … Incorrect identifiers –My Salary, Number-of-students, … –1stYear,...
25
©J.Tiberghien - ULB-VUB Version 2003-2004 25 ::= | | Correct identifiers –Length, Width, Cost,... –MySalary, NumberOfStudents, Year1, … –X1, Y2, A1, A5, … Incorrect identifiers –My Salary, Number-of-students, … –1stYear,...
26
©J.Tiberghien - ULB-VUB Version 2003-2004 26 Syntax Definition BNF Non-recursive BNF Syntax diagram Definition Choice 0 or 1 occurrence 0, 1 or many occurrences ::= Diagram [] {}
27
©J.Tiberghien - ULB-VUB Version 2003-2004 27 Syntax Definition BNF Non-recursive BNF Syntax diagram Definition Choice 0 or 1 occurrence 0, 1 or many occurrences ::= Diagram [ ] {}
28
©J.Tiberghien - ULB-VUB Version 2003-2004 28 ::= 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)
29
©J.Tiberghien - ULB-VUB Version 2003-2004 29 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
30
©J.Tiberghien - ULB-VUB Version 2003-2004 30 Syntax Definition BNF Non-recursive BNF Syntax diagram Definition Choice 0 or 1 occurrence 0, 1 or many occurrences ::= Diagram [] {}
31
©J.Tiberghien - ULB-VUB Version 2003-2004 31 Syntax Diagrams Examples UpperCaseLetter ABCDEVWXYZ Letter LowerCaseLetter abcdevwxyz UpperCaseLetter LowerCaseLetter
32
©J.Tiberghien - ULB-VUB Version 2003-2004 32 Syntax Diagrams Examples Digit 01234 56789 CardinalNumber ::= { } ::= 0|1|2|3|4|5|6|7|8|9 Digit
33
©J.Tiberghien - ULB-VUB Version 2003-2004 33 Syntax Diagrams Examples IntegerNumber ::= [ + | - ] CardinalNumber + -
34
©J.Tiberghien - ULB-VUB Version 2003-2004 34 Syntax Diagrams Examples Identifier ::= { | } Letter Digit Letter
35
©J.Tiberghien - ULB-VUB Version 2003-2004 35 Source Code (HLL) The COMPILER Object Code HARDWARE COMPILER = Lexical analyzer Syntax analyzer Code generator Code optimizer
36
©J.Tiberghien - ULB-VUB Version 2003-2004 36 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
37
©J.Tiberghien - ULB-VUB Version 2003-2004 37 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.
38
©J.Tiberghien - ULB-VUB Version 2003-2004 38 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.
39
©J.Tiberghien - ULB-VUB Version 2003-2004 39 Multi-pass compiler Source Object Lexical analysis Symbol table Int.1 Syntax analysis Code generation Optimization
40
©J.Tiberghien - ULB-VUB Version 2003-2004 40 Single - pass compiler Source Object Symbol table Syntax analysis Code generation Lexical analysis
41
©J.Tiberghien - ULB-VUB Version 2003-2004 41 Interpreters Source Symbol table Syntax analysis Simulated code execution Lexical analysis
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.