860 likes | 935 Views
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2. (2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 +
E N D
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. 2 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. 1 2 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. 3 1 2 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. 3 4 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. – 1 3 4 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. – 1 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. – 1 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. – 1 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. – 1 5 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. – 1 5 2 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. – 1 7 5 2 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. – 1 – 7 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. – 1 – 7 6 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. – 1 – 1 – 7 6 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 This form, often called an Abstract Syntax Tree, abstracts away from the details of the textual representation (in this case: parentheses) and explicitly represents the structure of the expression. It is, for instance, very easy to compute the value of the expression from this form. 1 – 1 – 1 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 A program that performs this computation is an interpreter of arithmetic expressions : given an expression, it “executes” it by finding its value. 1 interpreters, T-diagrams
(2 + 1 – 4) * ( – (5 + 2) + 6) * – + + 4 – 6 2 1 + 5 2 A program that performs this computation is an interpreter of arithmetic expressions : given an expression, it “executes” it by finding its value. It would not be too difficult to extend the interpreted language with variables, assignments, conditional statements and the like. 1 interpreters, T-diagrams
For example, we could represent a := 2; b := a * 2; if b > 4 then c := b else c := a fi as a tree (for the statements and expressions) and an array (for the variables): ; := ; v[0] 2 := if v[1] * > := := v[0] 2 v[1] 4 v[2] v[1] v[2] v[0] v interpreters, T-diagrams
For example, we could represent a := 2; b := a * 2; if b > 4 then c := b else c := a fi as a tree (for the statements and expressions) and an array (for the variables): ; := ; v[0] 2 := if v[1] * > := := v[0] 2 v[1] 4 v[2] v[1] v[2] v[0] “Execution” would consist in traversing the tree, updating the values of the variables in the array etc. v interpreters, T-diagrams
For example, we could represent a := 2; b := a * 2; if b > 4 then c := b else c := a fi as a tree (for the statements and expressions) and an array (for the variables): ; := ; v[0]2 := if v[1] * > := := v[0] 2 v[1] 4 v[2] v[1] v[2] v[0] “Execution” would consist in traversing the tree, updating the values of the variables in the array etc. 2 v interpreters, T-diagrams
For example, we could represent a := 2; b := a * 2; if b > 4 then c := b else c := a fi as a tree (for the statements and expressions) and an array (for the variables): ; := ; v[0] 2 := if v[1] * > := := v[0] 2 v[1] 4 v[2] v[1] v[2] v[0] “Execution” would consist in traversing the tree, updating the values of the variables in the array etc. 2 v interpreters, T-diagrams
For example, we could represent a := 2; b := a * 2; if b > 4 then c := b else c := a fi as a tree (for the statements and expressions) and an array (for the variables): ; := ; v[0] 2 := if v[1] * > := := v[0] 2 v[1] 4 v[2] v[1] v[2] v[0] “Execution” would consist in traversing the tree, updating the values of the variables in the array etc. 2 v 4 interpreters, T-diagrams
For example, we could represent a := 2; b := a * 2; if b > 4 then c := b else c := a fi as a tree (for the statements and expressions) and an array (for the variables): ; := ; v[0] 2 := if v[1] * > := := v[0] 2 v[1] 4 v[2] v[1] v[2] v[0] “Execution” would consist in traversing the tree, updating the values of the variables in the array etc. 2 4 v 4 interpreters, T-diagrams
For example, we could represent a := 2; b := a * 2; if b > 4 then c := b else c := a fi as a tree (for the statements and expressions) and an array (for the variables): ; := ; v[0] 2 := if v[1] * > := := v[0] 2 v[1] 4 v[2] v[1] v[2] v[0] “Execution” would consist in traversing the tree, updating the values of the variables in the array etc. 2 4 v interpreters, T-diagrams
For example, we could represent a := 2; b := a * 2; if b > 4 then c := b else c := a fi as a tree (for the statements and expressions) and an array (for the variables): ; := ; v[0] 2 := if v[1] * > := := v[0] 2 v[1]4 v[2] v[1] v[2] v[0] “Execution” would consist in traversing the tree, updating the values of the variables in the array etc. 2 4 v false interpreters, T-diagrams
For example, we could represent a := 2; b := a * 2; if b > 4 then c := b else c := a fi as a tree (for the statements and expressions) and an array (for the variables): ; := ; v[0] 2 := if v[1] * > := := v[0] 2 v[1] 4 v[2] v[1] v[2]v[0] “Execution” would consist in traversing the tree, updating the values of the variables in the array etc. 2 4 2 v interpreters, T-diagrams
For example, we could represent a := 2; b := a * 2; if b > 4 then c := b else c := a fi as a tree (for the statements and expressions) and an array (for the variables): ; := ; v[0] 2 := if v[1] * > := := v[0] 2 v[1] 4 v[2] v[1] v[2] v[0] “Execution” would consist in traversing the tree, updating the values of the variables in the array etc. Notice that this does not involve translating the interpreted program to machine code! 2 4 2 v
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 LOA L1 …. L1 LIT 2 … … interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 LOA L1 ADD L2 …. L1 LIT 2 L2 LIT 1 … … interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 LOA L1 ADD L2 SUB L3 …. L1 LIT 2 L2 LIT 1 L3 LIT 4 … … interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 LOA L1 ADD L2 SUB L3 STO MUL – 1 …. L1 LIT 2 L2 LIT 1 L3 LIT 4 … … interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 LOA L1 ADD L2 SUB L3 STO MUL – 1 LOA L4 …. L1 LIT 2 L2 LIT 1 L3 LIT 4 L4 LIT 5 … … interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 LOA L1 ADD L2 SUB L3 STO MUL – 1 LOA L4 ADD L5 …. L1 LIT 2 L2 LIT 1 L3 LIT 4 L4 LIT 5 L5 LIT 2 … … interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 LOA L1 ADD L2 SUB L3 STO MUL – 1 LOA L4 ADD L5 XOR ONES ADD ONE …. L1 LIT 2 L2 LIT 1 L3 LIT 4 L4 LIT 5 L5 LIT 2 … ONES LIT #11111 ONE LIT 1 … interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 LOA L1 ADD L2 SUB L3 STO MUL – 1 LOA L4 ADD L5 XOR ONES ADD ONE ADD L6 …. L1 LIT 2 L2 LIT 1 L3 LIT 4 L4 LIT 5 L5 LIT 2 L6 LIT 6 ONES LIT #11111 ONE LIT 1 … interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 LOA L1 ADD L2 SUB L3 STO MUL – 1 LOA L4 ADD L5 XOR ONES ADD ONE ADD L6 STO MUL – 2 …. L1 LIT 2 L2 LIT 1 L3 LIT 4 L4 LIT 5 L5 LIT 2 L6 LIT 6 ONES LIT #11111 ONE LIT 1 … interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 LOA L1 ADD L2 SUB L3 STO MUL – 1 LOA L4 ADD L5 XOR ONES ADD ONE ADD L6 STO MUL – 2 CAL MUL …. L1 LIT 2 L2 LIT 1 L3 LIT 4 L4 LIT 5 L5 LIT 2 L6 LIT 6 ONES LIT #11111 ONE LIT 1 … interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 Instead of an interpreter of expressions, our program would now become a compiler of expressions. interpreters, T-diagrams
We could also translate our expression from a tree to some other language, for example by converting it to an A1 program that would print out the value. * – + + 4 – 6 2 1 + 5 2 Instead of an interpreter of expressions, our program would now become a compiler of expressions. Interpreters and compilers are the two kinds of translators of programming languages (interpretation involves only translation to a convenient internal form). interpreters, T-diagrams
The important thing to remember is this: • a compiler translates a program to another language (usually a simpler one) interpreters, T-diagrams
The important thing to remember is this: • a compiler translates a program to another language (usually a simpler one); • an interpreter executes the program without such translation (though the program is usually converted to some internal representation). interpreters, T-diagrams
So now we can have a single program that works in three phases: • it converts the expression (or program) to a tree; • it converts the tree to RPN; • it executes the RPN. • Phases 1 and 2 constitute a compiler. Phase 3 is interpretation/emulation. • We could also have to or even three different programs that do the same thing. • The important thing to remember is this: • a compiler translates a program to another language (usually a simpler one); • an interpreter executes the program without such translation (though the program is usually converted to some internal representation). • Nowadays many languages are implemented by a combination of compilation and • interpretation: interpreters, T-diagrams
So now we can have a single program that works in three phases: • it converts the expression (or program) to a tree; • it converts the tree to RPN; • it executes the RPN. • Phases 1 and 2 constitute a compiler. Phase 3 is interpretation/emulation. • We could also have to or even three different programs that do the same thing. • The important thing to remember is this: • a compiler translates a program to another language (usually a simpler one); • an interpreter executes the program without such translation (though the program is usually converted to some internal representation). • Nowadays many languages are implemented by a combination of compilation and • interpretation: • the program is translated to the code of a virtual machine that is designed to make such translation particularly convenient; interpreters, T-diagrams