Project 4
P3
Interpreter without definition of functions The following sentence is to be interpreted:
2 3 4 + *.
How to do?
• Lexical analysis: we obtain 6 lexemes:
“2” “3” “4” “+” “*” “.”
• Syntactic analysis: there is none!
• Semantic analysis (done here at the same time as execution, lexeme by lexeme).
“2” exists and corresponds to the number 2 (similarly, 3 and 4), 2 is stacked (also 3 and 4) with integer types;
“+” Exists and corresponds to function 0 of the processor, one checks the right type of the parameters of the function + and one executes the function 0 of the processor, etc.
P4
Installing Basic Functions
It is possible to design a function in C language which makes it possible to correctly fill the table of the LAC symbols and the virtual machine VM. Note the declare_base
The + function can then be declared by:
P5
How do I find out if a function is already defined?
If the symbol table is defined by:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 0 1 43 2 1 1 1 1 0 1 4 115 119 97 112 2 0 0 2 0 0 2
L + n1 t1 t2 n2 t3 c a l s w a p n1 t1 t2 n2 t3 t4 c
Let’s look for the + function.
• Start at the last defined address (here 10, beginning of the swap function);
• Weseekifitis+(143)…no?Wetakethevalueofbox9=10-1,itis1(start
of function +), is this +? Yes! Its Nfa is therefore 1, and one calculates its Cfa which is worth 0 – case 8, obtained counting from the Nfa, taking into account the length of the name, the number of input and output parameters…
Let us look for function 1. The same process takes us through address 10, then 1. We arrive at 0 (LAC [0]). The function is therefore not defined!
P6
How to define a processor function? 代码没翻译,要的话问我
P7
How to run?
• “2” is not a function, it is an integer: it is stacked on the stack of data and stack
its type on the stack of types (likewise with 3 and 4);
• “+” Is a function, one checks that the stack of data contains at least two terms,
one checks on the stack of types that they are two objects of type integer;
• WefindtheCfaof+,itis0.WegototheaddressVM[0],wefind0,itis
therefore a basic function, its number is in VM [1], it is 0. We run processor [0];
we place the result type on the type stack and the result on the data stack…
• “*” Is not defined, we generate an error and empty the stacks of data, type and
return…
• We define * and ., and we start again … It must work!
P8
P9
A simple first example
Let us compile the function defined by: 2+. 2 + . ; . Where: is the beginning of the definition, 2+. its name, ; The end of the definition and 2 +: the body of the function …
• The system must be able to tell me that the function takes an integer and does
not return anything and encode it in the symbol table by Xx+1x+2x+3x+4x+5x+6x+7x+8
? 3 50 43 46 1 1 0 y
A l 2 +. N1 t1 n2 c
? Is the address of the last defined name (Nfa).
• And in the virtual machine
Yy+1y+2y+3y+4y+5
1 c1 2 c2 c3 c4
1 indicates that the function is a LAC function, c2 = 0 is the Cfa of +, c3 the Cfa of:, c4 the Cfa of a function (to be defined) (fin) which means that the code ends, c1 The Cfa of a function (to be defined) (lit) which indicates to the executor that the following box is an integer to be deposited on the stack …
P10
How does the new executor work?
So the command 5 2+., 5 is put on the stack, it is necessary to execute the function 2+.
• Its Cfa is y, the 1 in VM [y] signals to the executor that it must operate
otherwise.
• We stack on the return stack y + 1 and execute VM [y + 1] (the function (lit));
• Executing the function (lit), base function with processor [n] (), where n is the
number of the function;
• The return stack (z = y + 2) is stacked, z + 1 is stacked on the return stack and
we execute VM [z + 1] (the + function); …
• The return stack is stacked (Z = y + 4), stack z + 1 on the return stack and execute VM [z + 1] (the function (fin)), it is a basic function, after its execution there is nothing on the stack back, the executor stops!
P11 To do
Program and put in the symbols table and the virtual machine the functions: : Start of compilation;
; End of compilation;
(fin) indicates the end of the code (not used as interpreted);
(lit) as explained above (not usable as interpreted); … the functions that are missing;
The executor!