Program Translation.: Idea: Use The Computer Itself To Ease The Programmer's Work
Program Translation.: Idea: Use The Computer Itself To Ease The Programmer's Work
Because of that
the size and the complexity of the programs were severe limited the debugging was very difcult task in general, development of the programs was very difcult and error prone. Idea: use the computer itself to ease the programmers work by translation from a more human-readable form of the program into executable binary code.
Compilers vs Interpreters.
Compilers vs Interpreters.
Pre-processing.
Before we go into some details of compilation process, let us discuss relevant components of programming languages
is not a correct fragment of code, although lexically it is OK. At the same time
is a correct fragment.
means Check if m
Example.
Pascal program fragment:
if testval > 5 then setval := 20 else for i := 1 to 20 do begin valuej := 2*i; writeln (i, valuej) end ;
then else
10
Example(cont.)
if testval > 5 then setval := 20 else for i := 1 to 20 do begin valuej := 2*i; writeln (i, valuej) end ;
for
then else
setval
5 20
to
end condition
init.condition
statement
11
do
One of the ways of describing the grammar of a high-level programming language is an (informal) narrative description of the language. Example: Pascal is written free form with no required layout. Tokens are separated by spaces, tabs, carriage returns, or comments. The basic Pascal structure is the block. A block consists of the following components: label declarations constant denitions
12
13
Grammars.
The grammar is a set of such rules. Any grammar dene correct sentences (programs) in
terms of elementary parts symbols. When one denes a grammar for a programming language tokens are considered as such elementary symbols.
14
15
Syntax diagrams.
if
if-then-else-statement
boolean expression
then else
statement
statement
16
17
Backus-Naur form.
if-then-else-statement if boolean-expression
statement
18
Notation is dened, or replaced by OR, i.e A B means select either A or B name symbols symbols symbols nonterminal symbol terminal symbols optional symbols the symbols are repeated 0 or more times
Examples (Pascal):
simple-statement letter
19
Bakus-Naur (cont.)
20
The compilation process is normally broken down into the four major steps.
lexical analysis
semantic analysis
symbol table
tokens
21
Lexical analysis.
22
Syntax analysis.
23
Code generation.
24
Code optimization.
Example (Java) :
= b + 1 ve times !
25
1. Lexical analysis. Source text of program is converted into a sequence of lexemes (tokens), representing distinct elements of the program, e.g. number denotations, variable identiers, key words. The information on the type of every lexem is stored in the symbol table. 2. Syntax analysis (parsing). Program is analysed to uncover the grammar from of the constructions used. These are examined so as to t them into the syntactical rules of programs in the language. The outcome is to group and restructure the lexems in a way which identies the kinds of programs constructs, such as statements, etc, being used. 3. Code generation and optimization. Finally, the program constructions identied in the syntax analysis are converted into machine-code sequences. Memory locations and registers are assigned to the various data objects: variables, arrays, etc. The code is analysed to determine if there any ways to reduce the amount of code, to eliminate repeated operations, to reorganize parts of the program to execute faster and more efciently.
26