©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