Assemblers and compilers translate for later execution by real hardware or by software interpreters. They are application-specific programs just like any other, best written in HLLs, especially those specific to the application area
simplifies the task of writing machine code programs
|
Formally:
These steps (and some others mentioned below) are known as phases - logical subdivisions of the overall translation task.
special notations e.g. Backus Normal Form (BNF)
In natural languages, sentences/statements only have meaning in a context.
Computers have no common sense or understanding as to what is going on,
so context has to be carefully defined:
Makes it seem as if the high-level language is the machine language.
|
e.g.
/* division by repeated subtraction */
int main (void)
{
int ans=-1, a=99, b=6;
do
{ans=ans+1; a=a-b;}
while (a>=0);
return ans;
}
First 4 steps the same:
main MVN a1, #0 ; a1 = ans
MOV a2, #&63 ; a2 = a
MOV a3, #6 ; a3 = b
L ADD a1, a1, #1
SUBS a2, a2, a3
BPL L
MOV pc, lr
Why interpret?
name identifies an element of the language,
name : something ; defines the left hand name as equal to the right hand something,
| separates alternative definitions,
{ something } means that the enclosed something occurs none or more times,
"text" indicates characters present in an actual program in the language.
program : statement { statement } ;
statement : declarative ";" | executable ";" ;
declarative : type identifier { "," identifier } ;
type : "CARNIV" | "VEGET" | "MALE" | "FEMALE" | "VEGETABLE" ;
executable : "BEGIN" { statement } "END" | action { "AND" action } ;
action : identifier op rest ;
rest : identifier { "WITH" identifier } ;
identifier : letter { letter } ;
op : "EATS" | "IS EATEN BY" | "HATES" | "LOVES" ;
letter : "a" | "b" | . . . | "z" ;
(i.e. all lower case characters of the English alphabet)
New lines and spaces are ignored everywhere (e.g. do not separate words).
Find all the syntax errors in the following ECO programs:
(a) CARNIV lion, tiger, cat, man, dog;
VEGET minnow, man, dog;
lion EATS man WITH dog;
lion WITH tiger EATS man;
minnow IS EATEN BY cat
(b) VEGETABLE onions, leeks;
VEGET fred, albert;
fred HATES onions WITH leeks;
BEGIN fred EATS onions; albert EATS leeks; END
CARNIV lion; albert IS EATEN BY lion AND lion EATS onions; onions IS EATEN BY
fred WITH leeks;
(c) MALE jack; FEMALE joy; CARNIV joy;
jack LOVES joy; joy HATES jack;
jack IS EATEN BY joy; lion EATS jack WITH joy;
lion LOVES jack with joy;
jack AND joy HATES lion;
Find all the static semantic errors in the ECO programs above.