SI 413 Fall 2015: Homework 7

advertisement
SI 413 Fall 2015: Homework 7
Your name:
Due: Wednesday, October 28
Problem
Instructions: Review the course honor policy for written
homeworks.
1
This cover sheet must be the front page of what you hand in.
Fill out the left column in the table to the right after we go
over each problem in class, according to the rubric below.
2
This rubric is also on the website, in more detail, under
“Admin”→“Grading Rubrics”.
3
Make sure all problems are submitted IN ORDER.
• 5: Solution is completely correct, concisely presented,
and neatly written.
• 4: The solution is mostly correct, but one or two minor
details were missed, or the presentation could be better.
• 3: The main idea is correct, but there are some significant mistakes. The presentation is somewhat sloppy or
confused.
• 2: A complete effort was made, but the result is mostly
incorrect. There may be some basic misunderstandings
of the topic or the problem.
• 1: The beginning of an attempt was made, but the work
is clearly incomplete.
• 0: Not submitted.
Comments or suggestions for the instructor:
What other students did you work with?
Citations (be specific about websites):
1
4
Self-assessment
Final assessment
1
Draw an AST
Here is a program written in Python. Draw its AST.
x = 10
while x > 0:
print ( x )
x = x - 3
print (" done ")
2
2
Undraw an AST
Write a program in the language of your choosing that would, when compiled (or interpreted) cause the compiler (or
interpreter) of that program to generate the following AST:
Figure 1: AST for HW 7
(Be sure to specify which language you have chosen!)
3
3
Sethi-Ullman Algorithm
This homework assignment gives you a chance to use the Sethi-Ullman algorithm on an Abstract Syntax Tree (AST).
Here is the AST:
Figure 2: o:=(a/4+b/3)*(a+b+c+d);
Mark the nodes of the tree with the values described by the Sethi-Ullman algorithm, beginning with 0 or 1 for leaf nodes
(as we discussed in class). Traverse the tree methodically, labeling the intermediate nodes according to the algorithm until
you have covered the entire tree.
4
Code Generation
The second part of the algorithm describes the order of tree traversal for code generation - taking the branch with the
higher count value first. Using our simple assembler code, generate code for this AST. Reminder: the instructions take
the form: opcode $r arg
The opcodes are: LOAD, STOR, ADD, SUB, MUL, DIV, MOD and $r means a register, like $0 or $1 and arg means the
other argument to the opcode which can be another register, or a constant, or a memory location (e.g. M25). Example
instruction: LOAD $0 M12 to load memory location 12 into register 0. Write neatly!
4
Download