Question: Expanding on Example 1.25, trace an interpretation of the gcd program on the inputs 12 and 8. Which syntax tree nodes are visited, in which
Expanding on Example 1.25, trace an interpretation of the gcd program on the inputs 12 and 8. Which syntax tree nodes are visited, in which order?
Example 1.25
Many interpreters use an annotated syntax tree to represent the running program: “execution” then amounts to tree traversal. In our GCD program, an interpreter would start at the root of Figure 1.6 and visit, in order, the statements on the main spine of the tree. At the first “:=” node, the interpreter would notice that the right child is a call: it would therefore call the getint routine (found in slot 3 of the symbol table) and assign the result into i (found in slot 5 of the symbol table). At the second “:=” node the interpreter would similarly assign the result of getint into j. At the while node it would repeatedly evaluate the left (“=”) child and, if the result was true, recursively walk the tree under the right (if) child.
Finally, once the while node’s left child evaluated to false, the interpreter would move on to the final call node, and output its result. In many compilers, the annotated syntax tree constitutes the intermediate form that is passed from the front end to the back end. In other compilers, semantic analysis ends with a traversal of the tree (typically single pass) that generates some other intermediate form. One common such form consists of a control flow graph whose nodes resemble fragments of assembly language for a simple idealized machine. We will consider this option further in Chapter 15, where a control flow graph for our GCD program appears in Figure 15.3. In a suite of related compilers, the front ends for several languages and the back ends for several machines would share a common intermediate form.
![]()
program while (5) call (6) call call (3) (3) (4) (5) (5) (6) Index Symbol 1 type type func: void (5) (6) 2 int :(1) (2) func : (2) (1) (2) (2) 3 getint 4 putint (5) (6) (6) (5) i 6.
Step by Step Solution
3.43 Rating (162 Votes )
There are 3 Steps involved in it
program call 3 call getint return return 5 return assign 12 ... View full answer
Get step-by-step solutions from verified subject matter experts
