Building Interpreters
description
Transcript of Building Interpreters
![Page 1: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/1.jpg)
Building Interpreters
Mooly Sagiv
html://www.cs.tau.ac.il/~msagiv/courses/wcc13.html
Chapter 4
1
![Page 2: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/2.jpg)
Structure of a simple compiler/interpreter
Lexical
analysis
Syntax
analysis
Context
analysis
Intermediate code
(AST)
Code
generation
Interpretation
Symbol Table
Runtime System
Design
PL dependent PL+pardigm dependent
Machine dependent
2
![Page 3: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/3.jpg)
Types of Interpreters
• Recursive– Recursively traverse the tree– Uniform data representation– Conceptually clean– Excellent error detection– 1000x slower than compiler
• Iterative– Closer to CPU– One flat loop– Explicit stack– Good error detection– 30x slower than compiler– Can invoke compiler on code fragments
3
![Page 4: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/4.jpg)
Input language (Overview)
• Fully parameterized expressions
• Arguments can be a single digit
expression digit | ‘(‘ expression operator expression ‘)’
operator ‘+’ | ‘*’
digit ‘0’ | ‘1’ | ‘2’ | ‘3’ | ‘4’ | ‘5’ | ‘6’ | ‘7’ | ‘8’ | ‘9’
4
![Page 5: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/5.jpg)
#include "parser.h"
#include "backend.h"static int Interpret_expression(Expression *expr) { switch (expr->type) { case 'D': return expr->value; break; case 'P': { int e_left = Interpret_expression(expr->left); int e_right = Interpret_expression(expr->right); switch (expr->oper) { case '+': return e_left + e_right; case '*': return e_left * e_right; }} break; }}void Process(AST_node *icode) { printf("%d\n", Interpret_expression(icode));}
5
![Page 6: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/6.jpg)
AST for (2 * ((3*4)+9))
P
*
oper
typeleft right
P
+
P
*
D
2
D
9
D
4
D
3 6
![Page 7: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/7.jpg)
Uniform self-identifying data representation
• The types of the sizes of program data values are not known when the interpreter is written
• Uniform representation of data types– Type– Size
• The value is a pointer
7
![Page 8: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/8.jpg)
Example: Complex Number
3.0
4.0
re:
im:
8
![Page 9: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/9.jpg)
9
![Page 10: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/10.jpg)
Status Indicator
• Direct control flow of the interpreter
• Possible values– Normal mode– Errors– Jumps– Exceptions– Return
10
![Page 11: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/11.jpg)
Example: Interpreting C Return
PROCEDURE Elaborate return with expression statement (RWE node):
SET Result To Evaluate expression (RWE node . expression);
IF Status . mode /= Normal mode: Return mode;
SET Status . mode To Return mode;
SET Status . value TO Result;
11
![Page 12: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/12.jpg)
Interpreting If-Statement
12
![Page 13: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/13.jpg)
Symbol table
• Stores content of variables, named constants, …• For every variable V of type T
– A pointer to the name of V
– The file name and the line it is declared
– Kind of declaration
– A pointer to T
– A pointer to newly allocated space
– Initialization bit
– Language dependent information (e.g. scope)
13
![Page 14: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/14.jpg)
Summary Recursive Interpreters
• Can be implemented quickly– Debug the programming language
• Not good for heavy-duty interpreter– Slow– Can employ general techniques to speed the
recursive interpreter• Memoization• Tail call elimination• Partial evaluation
14
![Page 15: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/15.jpg)
Memoization
int fib(int n) { if (n == 0) return 0 ; if (n==1) return 1; return fib(n-1) + fib(n-2) ; }
int sfib[100] = {-1, -1, …, -1}int fib(int n) { if (sfib[n] > 0) return sfib[n]; if (n == 0) return 0 ; if (n==1) return 1; sfib[n] = fib(n-1) + fib(n-2) ; return sfib[n]; }
15
![Page 16: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/16.jpg)
Tail Call Elimination
void a(…) { … b(); }void b(){code;}
void a(…) { … code; }void b(){code;}
16
![Page 17: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/17.jpg)
Tail Call Elimination
void a(int n) { code if (n > 0) a(n-1); }
void a(int n) { loop: code if (n > 0) { n = n -1 ; goto loop }
17
![Page 18: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/18.jpg)
Partial Evaluation
• Partially interpret static parts in a program
• Generates an equivalent program
Partial EvaluatorProgram Program’
Input 1Input 2
18
![Page 19: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/19.jpg)
Example
int pow(int n, int e)
{
if (e==0)
return 1;
else return n * pow(n, e-1);
}
e=4
int pow4(int n)
{
return n * n * n *n;
}
19
![Page 20: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/20.jpg)
Example2
Bool match(string, regexp)
{
switch(regexp) {
….
}
}
regexp=a b* 20
![Page 21: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/21.jpg)
Partial Evaluation Generalizes Compilation
Partial EvaluatorInterpreter Program
AST ProgramInput
21
![Page 22: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/22.jpg)
But ….
22
![Page 23: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/23.jpg)
Iterative Interpretation
• Closed to CPU
• One flat loop with one big case statement
• Use explicit stack– Intermediate results– Local variables
• Requires fully annotated threaded AST– Active-node-pointer (interpreted node)
23
![Page 24: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/24.jpg)
Demo Compiler
24
![Page 25: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/25.jpg)
Threaded AST• Annotated AST• Every node is connected to the immediate
successor in the execution• Control flow graph
– Nodes• Basic execution units
– expressions– assignments
– Edges• Transfer of control
– sequential– while– …
25
![Page 26: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/26.jpg)
Threaded AST for (2 * ((3*4)+9))
P
*
oper
typeleft right
P
+
P
*
D
2
D
9
D
4
D
3
Dummy_node Start
26
![Page 27: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/27.jpg)
Demo Compiler
27
![Page 28: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/28.jpg)
C Examplewhile ((x > 0) && (x < 10))
{
x = x + y ;
y = y – 1 ;
}
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
T
exitF
28
![Page 29: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/29.jpg)
Threading the AST(3.2.1)
• One preorder AST pass
• Every type of AST has its threading routine
• Maintains Last node pointer – Global variable
• Set successor of Last pointer when node is visited
29
![Page 30: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/30.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
30
![Page 31: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/31.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
31
![Page 32: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/32.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
32
![Page 33: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/33.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
33
![Page 34: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/34.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
34
![Page 35: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/35.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
35
![Page 36: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/36.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
36
![Page 37: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/37.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
37
![Page 38: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/38.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
T
38
![Page 39: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/39.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
T
39
![Page 40: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/40.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
T
40
![Page 41: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/41.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
T
41
![Page 42: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/42.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointer
main
T
42
![Page 43: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/43.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointermain
T
43
![Page 44: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/44.jpg)
while
andseq
ass
ass
id+
x
idx
id
y
>
idx
const0
<
idx
const
10
+
idy
const
1
idy
Last node pointermain
First node pointer
T
44
![Page 45: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/45.jpg)
Demo Compiler
45
![Page 46: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/46.jpg)
Conditional Statement
if
condthen_part else_part
Last node pointer
46
![Page 47: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/47.jpg)
Conditional Statement
if
condthen_part else_part
Last node pointer
End_If
T F
47
![Page 48: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/48.jpg)
Iterative Interpretation
• Closed to CPU
• One flat loop with one big case statement
• Use explicit stack– Intermediate results– Local variables
• Requires fully annotated threaded AST– Active-node-pointer (interpreted node)
48
![Page 49: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/49.jpg)
Demo Compiler
49
![Page 50: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/50.jpg)
Conditional Statements
50
![Page 51: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/51.jpg)
Storing Threaded AST
• General Graph
• Array
• Pseudo Instructions
51
![Page 52: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/52.jpg)
Threaded AST as General Graph
condition
statement 1
IF
statement 2
statement 3
statement 4END
If 52
![Page 53: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/53.jpg)
Threaded AST as Array
condition
IF
statement 1
statement 2
statement 3
statement 4
53
![Page 54: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/54.jpg)
Threaded AST as Pseudo Instructions
condition
IFFALSE
statement 1
statement 2statement 3
statement 4
JUMP
54
![Page 55: Building Interpreters](https://reader036.fdocuments.net/reader036/viewer/2022081503/56813959550346895da0fb29/html5/thumbnails/55.jpg)
Iterative Interpreters (Summary)
• Different AST representations
• Faster than recursive interpreters– Some interpretative overhead is eliminated
• Portable
• Secure
• Similarities with the compiler
55