Title: Real Time Systems
1Troisième Partie Chapitre 1 Les supports à la
programmation
2Programming languages
Source code
C
High Level Language
Assembler
Low Level Languages
Object code
Machine Language
HARDWARE
3Machine Language
4Assembler Language
Label Operation Operands Comments
ORG 100 First address of data memory
ND DAT 2 Number of entered digits, 2 bytes
SC DAT 2 Secret Code as entered, 2 bytes
KFL EQU 1 Keyboard flag
KDA EQU KFL1 Keyboard data
DDA EQU 3 Door data
ORG 10 First address of program memory
BGN COPY 0,ND Initialise number of entered digits
COPY 0,SC Initialise secret code
TFL EQ? KFL,0,TFL Test continuously for key stroke
MUL SC,10,SC Shift SC one digit to the left
ADD SC,KDA,SC Add newly entered digit to SC
COPY 0,KFL Reset Keyboard flag
ADD ND,1,ND Increase number of entered digits
NE? ND,3,TFL Any more digits needed ?
NE? SC,321,BGN Is the entered secret code correct ?
COPY 1,DDA Open the door
JMP BGN Restart everything
END
5High Level language
Main() int sc 0 const int Key 321 bool
KFL 1 int KDA KFL1 bool DDA 3
while (true) for (int nd1ndlt3nd) while
(KFL 0) sc sc10KDA if (sc
Key) DDA 1
KFL
DDA
KDA
6Transforming Source Code
7Translators 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
8Macros
-
- 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,U5,U
- STD 2,U
- MULB 6,U5,U
- ADD 1,U
- STD 1,U
- MULB 6,U5,U
- ADD 1,U
- ...
9Macros 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.
10Macro Expanders
Source
Main module
Macro Library
Macro Expander
Expanded Source Code
Assembler Compiler
Object
11Macros vs. Functions
- FUNCTIONS
- Dynamic Expansion
- Save memory space
- Slow
- Programmer friendly parameter passing not
available in assembler
- MACROS
- Static Expansion
- Waste of memory
- Fast
- Powerful parameter passing, even in assembler
12Translating HLLs
13Syntax 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 !!!
14Syntax Definition Backus Naur Formalism (BNF)
- Terminal Symbols
- Appear literally in program text
- Examples a, 3, END, gt,
- Non-terminal Symbols
- Name given to set of terminal symbols
- Written between lt and gt
- Defined by a metalinguistic expression
- Examples ltLettergt , ltDigitgt,ltExpressiongt,ltifstat
ementgt - Metalinguistic expression
- Formal definition () of a non-terminal symbol
- Contains terminal and non-terminal symbols
- Expresses juxtaposition or choice (the
operator) - Example ltDigitgt 0123456789
15BNF Examples (1)
- ltUpperCaseLettergt ABCDE
XYZ - ltLowerCaseLettergt abcde
xyz - ltLettergt ltUpperCaseLettergt
ltLowerCaseLettergt - ltDigitgt 1234567890
- ltCardinalNumbergt ltDigitgt ltDigitgtltCardinalNum
bergt - ltIntegerNumbergt ltCardinalNumbergt
- ltCardinalNumbergt
- - ltCardinalNumbergt
- ltIdentifiergt ltLettergt
ltIdentifiergtltLettergt ltIdentifiergt ltDigitgt
16ltCardinalNumbergt ltDigitgt
ltDigitgtltCardinalNumbergt
- Is 123 a ltCardinalNumbergt ?
- Is it a ltDigitgt ?
- Certainly not
17ltCardinalNumbergt ltDigitgt
ltDigitgtltCardinalNumbergt
- Is 123 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 23 a ltCardinalNumbergt ?
18ltCardinalNumbergt ltDigitgt
ltDigitgtltCardinalNumbergt
- Is 123 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 23 a ltCardinalNumbergt ?
- Is it a ltDigitgt ?
- Certainly not
19ltCardinalNumbergt ltDigitgt
ltDigitgtltCardinalNumbergt
- Is 123 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 23 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 3 a ltCardinalNumbergt ?
20ltCardinalNumbergt ltDigitgt
ltDigitgtltCardinalNumbergt
- Is 123 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 23 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 3 a ltCardinalNumbergt ?
- Is it a ltDigitgt ?
- YES
21ltCardinalNumbergt ltDigitgt
ltDigitgtltCardinalNumbergt
- Is 123 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 23 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 3 a ltCardinalNumbergt ?
- Is it a ltDigitgt ?
- YES
- As a consequence, 3 is a ltCardinalNumbergt
22ltCardinalNumbergt ltDigitgt
ltDigitgtltCardinalNumbergt
- Is 123 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 23 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 3 a ltCardinalNumbergt ?
- Is it a ltDigitgt ?
- YES
- As a consequence, 3 is a ltCardinalNumbergt
- As a consequence, 23 is a ltCardinalNumbergt
23ltCardinalNumbergt ltDigitgt
ltDigitgtltCardinalNumbergt
- Is 123 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 23 a ltCardinalNumbergt ?
- Is it a ltDigitgt followed by a ltCardinalNumbergt ?
- Is 3 a ltCardinalNumbergt ?
- Is it a ltDigitgt ?
- YES
- As a consequence, 3 is a ltCardinalNumbergt
- As a consequence, 23 is a ltCardinalNumbergt
- As a consequence, 123 is a ltCardinalNumbergt
24ltIdentifiergt ltLettergt ltIdentifiergtltLettergt
ltIdentifiergt ltDigitgt
- Correct identifiers
- Length, Width, Cost, ...
- MySalary, NumberOfStudents, Year1,
- X1, Y2, A1, A5,
- Incorrect identifiers
- My Salary, Number-of-students,
- 1stYear, ...
25ltIdentifiergt ltLettergt ltIdentifiergtltLettergt
ltIdentifiergt ltDigitgt
- Correct identifiers
- Length, Width, Cost, ...
- MySalary, NumberOfStudents, Year1,
- X1, Y2, A1, A5,
- Incorrect identifiers
- My Salary, Number-of-students,
- 1stYear, ...
26Syntax Definition
Non-recursive BNF
Syntax diagram
BNF
Definition
Diagram
Choice
0 or 1 occurrence
0, 1 or many occurrences
27Syntax Definition
Non-recursive BNF
Syntax diagram
BNF
Definition
Diagram
Choice
0 or 1 occurrence
0, 1 or many occurrences
28BNF Examples (2)
- ltUpperCaseLettergt ABCDE
XYZ - ltLowerCaseLettergt abcde
xyz - ltLettergt ltUpperCaseLettergt
ltLowerCaseLettergt - ltDigitgt 1234567890
- ltCardinalNumbergt ltDigitgt ltDigitgt
- ltIntegerNumbergt -ltCardinalNumbergt
- ltIdentifiergt ltLettergt ltLettergtltDigitgt
29Backus 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
30Syntax Definition
Non-recursive BNF
Syntax diagram
BNF
Definition
Diagram
Choice
0 or 1 occurrence
0, 1 or many occurrences
31Syntax DiagramsExamples
32Syntax DiagramsExamples
Digit
ltDigitgt 0123456789
CardinalNumber
ltCardinalNumbergt ltDigitgt ltDigitgt
Digit
Digit
33Syntax DiagramsExamples
IntegerNumber
ltIntegerNumbergt - ltCardinalNumbergt
CardinalNumber
34Syntax DiagramsExamples
Identifier
ltIdentifiergt ltLettergt ltLettergtltDigitgt
35The COMPILER
Source Code (HLL)
36The 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
37The 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.
38Error 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.
39Multi-pass compiler
Source
Lexical analysis
Symbol table
Int.1
Syntax analysis
Int.1
Code generation
Int.1
Object
Optimization
40Single - pass compiler
Source
Symbol table
Object
41Interpreters
Syntax analysis
Source
Lexical analysis
Symbol table
Simulated code execution