410 likes | 499 Vues
Troisième Partie Chapitre 1 Les supports à la programmation. Programming languages. Source code. C++. High Level Language. Assembler. Low Level Languages. Object code. Machine Language. HARDWARE. Data Memory. 100: ND. 102: SC. Arithmetic Unit. 1:KFL . 3:DDA. 1. 2. 3.
E N D
Troisième Partie Chapitre 1 Les supports à la programmation
Programming languages Source code C++ High Level Language Assembler Low Level Languages Object code Machine Language HARDWARE
Data Memory 100: ND 102: SC Arithmetic Unit 1:KFL 3:DDA 1 2 3 Control Unit 2:KDA 4 5 6 Program Memory (from 11) 7 8 9 10 0 100 10 * 0 # 11 0 102 10 12 1 0 12 40 13 102 10 102 22 14 20 102 2 102 15 0 1 10 16 101 1 101 20 17 101 3 12 41 18 2 321 10 41 19 1 3 10 20 10 47 Machine Language
1 2 3 4 5 6 7 8 9 * 0 # High Level language 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; } } KFL = DDA = KDA =
Source Code (LLL or HLL) Translator Object Code INTERPRETER HARDWARE Transforming Source Code
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 Translators vs. Interpreters
… 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 ... MULB MDEF 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 ... Macros
/* The Program: */ main(void) { # include “one” } /* file one */ printf(“ from 1.\n”); # include “two” /* file two */ printf(“ from 2.\n”); from 1. from 2. Macros in HLL
Macro Expanders Source Main module Macro Library Macro Expander Expanded Source Code Assembler Compiler Object
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 Macros vs. Functions
Source Code (HLL) COMPILER Object Code INTERPRETER HARDWARE Translating HLLs
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 !!! Syntax and Semantics
Terminal Symbols Appear literally in program text Examples : “a”, “3”, “END”, “>”, “:=” Non-terminal Symbols Name given to set of terminal symbols Written between < and > Defined by a metalinguistic expression Examples : <Letter> , <Digit>,<Expression>,<ifstatement> Metalinguistic expression Formal definition (::=) of a non-terminal symbol Contains terminal and non-terminal symbols Expresses juxtaposition or choice (the | operator) Example : <Digit> ::= 0|1|2|3|4|5|6|7|8|9 Syntax Definition :Backus Naur Formalism (BNF)
<UpperCaseLetter> ::= A|B|C|D|E| |X|Y|Z <LowerCaseLetter> ::= a|b|c|d|e| |x|y|z <Letter> ::= <UpperCaseLetter> | <LowerCaseLetter> <Digit> ::= 1|2|3|4|5|6|7|8|9|0 <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber> <IntegerNumber> ::= <CardinalNumber> | + <CardinalNumber> | - <CardinalNumber> <Identifier> ::= <Letter> | <Identifier><Letter> | <Identifier> <Digit> BNF Examples (1)
Is 123 a <CardinalNumber> ? Is it a <Digit> ? Certainly not <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber>
Is 123 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 23 a <CardinalNumber> ? <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber>
Is 123 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 23 a <CardinalNumber> ? Is it a <Digit> ? Certainly not <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber>
Is 123 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 23 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 3 a <CardinalNumber> ? <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber>
Is 123 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 23 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 3 a <CardinalNumber> ? Is it a <Digit> ? YES <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber>
Is 123 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 23 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 3 a <CardinalNumber> ? Is it a <Digit> ? YES As a consequence, 3 is a <CardinalNumber> <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber>
Is 123 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 23 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 3 a <CardinalNumber> ? Is it a <Digit> ? YES As a consequence, 3 is a <CardinalNumber> As a consequence, 23 is a <CardinalNumber> <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber>
Is 123 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 23 a <CardinalNumber> ? Is it a <Digit> followed by a <CardinalNumber> ? Is 3 a <CardinalNumber> ? Is it a <Digit> ? YES As a consequence, 3 is a <CardinalNumber> As a consequence, 23 is a <CardinalNumber> As a consequence, 123 is a <CardinalNumber> <CardinalNumber> ::= <Digit> | <Digit><CardinalNumber>
Correct identifiers Length, Width, Cost, ... MySalary, NumberOfStudents, Year1, … X1, Y2, A1, A5, … Incorrect identifiers My Salary, Number-of-students, … 1stYear, ... <Identifier> ::= <Letter> | <Identifier><Letter> | <Identifier> <Digit>
Correct identifiers Length, Width, Cost, ... MySalary, NumberOfStudents, Year1, … X1, Y2, A1, A5, … Incorrect identifiers My Salary, Number-of-students, … 1stYear, ... <Identifier> ::= <Letter> | <Identifier><Letter> | <Identifier> <Digit>
Syntax Definition Non-recursive BNF Syntax diagram BNF ::= ::= Definition Diagram Choice 0 or 1 occurrence [] 0, 1 or many occurrences {}
Syntax Definition Non-recursive BNF Syntax diagram BNF ::= ::= Definition Diagram Choice 0 or 1 occurrence [ ] 0, 1 or many occurrences {}
<UpperCaseLetter> ::= A|B|C|D|E| |X|Y|Z <LowerCaseLetter> ::= a|b|c|d|e| |x|y|z <Letter> ::= <UpperCaseLetter> | <LowerCaseLetter> <Digit> ::= 1|2|3|4|5|6|7|8|9|0 <CardinalNumber> ::= <Digit> {<Digit>} <IntegerNumber> ::= [+|-]<CardinalNumber> <Identifier> ::= <Letter> {<Letter>|<Digit>} BNF Examples (2)
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. Backus Naur Formalism (BNF) Terminal symbol Non-terminal symbol
Syntax Definition Non-recursive BNF Syntax diagram BNF ::= ::= Definition Diagram Choice 0 or 1 occurrence [] 0, 1 or many occurrences {}
UpperCaseLetter • LowerCaseLetter y A B C D E V W X Y a b c d e v w x Z z UpperCaseLetter LowerCaseLetter Syntax DiagramsExamples • Letter
6 8 5 4 3 7 1 0 9 2 Digit Syntax DiagramsExamples Digit <Digit> ::= 0|1|2|3|4|5|6|7|8|9 CardinalNumber <CardinalNumber> ::= <Digit> {<Digit>} Digit Digit
+ - Syntax DiagramsExamples IntegerNumber <IntegerNumber> ::= [ + | - ] <CardinalNumber> CardinalNumber
Letter Digit Letter Syntax DiagramsExamples Identifier <Identifier> ::= <Letter> {<Letter>|<Digit>}
Lexical analyzer Syntax analyzer Code generator Code optimizer COMPILER = Object Code HARDWARE The COMPILER Source Code (HLL)
Recognizes lexical tokens such as: Identifiers Numbers : -200, 6.3E23, … Reserved words : for; if; Multi-character symbols : /*; */; == ; != ; ... Prepares the symbol table with all identifiers The Lexical Analyzer
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. The Syntax Analyzer(= syntax parser)
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. Error detection and correction
Multi-pass compiler Source Lexical analysis Symbol table Int.1 Syntax analysis Int.1 Code generation Int.1 Object Optimization
Syntax analysis Lexical analysis Code generation Single - pass compiler Source Symbol table Object
Interpreters Syntax analysis Source Lexical analysis Symbol table Simulated code execution