CS 3723 Programming Languages
|
(See also previous questions: Fall 2004, Spring 2004.)
Any help would be greatly appreciated.
[This student sent partly transformed code that compiled and produced the correct output and included the following in the middle.]
(stuff that comes before L12) L12: c = r; if (c < 0) c = -c; j = i - 1; L14: if (j < c + 1) { k[c+1] = y; i++; goto L3; } k[j+1] = k[j]; j--; goto L14; } (stuff that comes after L16)
Answer: Maybe this assignment is harder than I thought. I checked your current program, and it does compile and produce what it should when executed. Nevertheless, it seems to be muddled together in some weird way. I didn't realize that you could transform it, while still able to compile, run, and get the right output, and yet have it pretty mixed up.
With it in its current state, I think I'd be rather stuck too! Anyway, don't worry about the function call, but just try to get it as goto-less as possible.
For example, in your transformed code, you have a goto L14 _after_ the goto L3. This seems like it's not going to work out. Here's a hint to start from scratch. The original program looks like:
(stuff involving labels L1 and L2 only) i = 2; L3: if (i > N) goto L16; (stuff involving labels L4-L15 only) i++; goto L3; L16: P = N; (stuff involving labels L17-L24 only)
Now transform it to the following:
(stuff involving labels L1 and L2 only) i = 2; while (i <= N) { (stuff involving labels L4-L15 only) i++; } P = N; (stuff involving labels L17-L24 only)
If you think about it, this has broken the code into 3 distinct pieces, each with labels and gotos confined to that piece. You can go on simplifying from here.
Answer: For full credit you should process the given input, which includes C-style comments and floating point numbers intermingled. Yes, it should be one program. At first I didn't understand you, but now I do. If your program was just looking for the start of a floating point constant, which must be either a digit or a dot, it would of course just read past any start or end of a comment along the way. The problem is that a comment might have a number inside it, and that number must be ignored. (I just added such an example to the input for the recitation. The input now has a number 53.53 that is inside a comment, and so should not be processed.) A program that "scanned through a file and JUST recognized floating point numbers" would not eliminate C-comments automatically, and so would find any numbers inside the comments. For full credit you must add code to eliminate comments. You could use code similar to what I supplied, or write your own.
Also, I would like to know if you grade the time and space complexity of our programs.
Answer:Yes, of course you can use the code I supply for the course. (It would be insane to say you're not to look at it.) In fact you can use any source at all for your code as long as you give a clear and prominent citation of where it came from. In that case the credit will depend on how much you wrote yourself. No citation is needed for the code I have supplied. Remember, though, that you only learn from the code you write yourself. (I just changed the submissions page to give this information.)
About the second question, I'm only worrying about the correctness of your code in this course. This corresponds to much of the real world. Most often time and space concerns are of secondary importance (or less).
Answer: Of course I meant to adapt the given parser to become part of the new one. There are several minor changes needed, and then you need to embed this code into the larger collection of functions.
I think I'm either thinking too hard about this or am "off track" somewhere in my understanding?
Answer: Yes, you are a little bit off track, but not too much. You're very close on this.
First let's look at E. Suppose there is no plus or minus sign, but just other operators. In that case E does _nothing_. E does: "res = T();" and just returns the res. Nothing else. In this case E has no need for a temporary location. What does the res represent? It might be a constant. It might be a single variable. It might be a temporary representing a more complex term. E doesn't know or care. That's true with all of these functions. When they get a return value, they don't care what kind of an expression it was. They process it the same in any case.
In E, you only need a new temporary in case there is a + or -. Say it is a plus. Then E will call T twice, getting back a value each time, call these arg1 and arg2. As far a E is concerned, it thinks the expression looks like something (whose value will be at location arg1) plus something else (whose value will be at location arg2). _Now_ E will get a new temporary. E will load the locations given by arg1 and arg2 into registers. (Of course I mean that E will output MIPS code to do these things at run time.) Then E will output an add instruction to add the two registers, leaving the result in a third register. Finally, E will output code to store the result into the temporary location. If there are no more + or - signs, E will return the index of this temporary location.
So inside E (and T as well) don't get a new temporary unless you need it, that is, inside the loop. Of course you have to increment the temp variable after using it.
Think about A. It calls E and gets back an index (at compile time). A doesn't know or care what kind of an expression it was, whether a digit, a letter, or a complicated expression. A outputs the same two instructions in any of these cases.
double harmonic (int n){ // stuff omitted, but the following for the // recursive call: return 1/n + harmonic(n-1); }
Answer: This is a common problem. In Java (and C/C++) the / operator used with int arguments produces a truncated integer result, so that 1/n will be zero for n > 1.
Answer: I didn't specify the spacing for the horizontal lines on the left, or for the height of the box on the right, or for the spacing between the box and the top or bottom text lines on the right. I assumed that you would make it look "good". On the left, 15 points down from the upper horizontal line to the first text line looks good, but 15 points is too far down for the lower text line on the left. You should just specify it so that it looks the same as the upper spacing.