CS 3723 Programming Languages
Questions and Answers, Fall 2004

(See also Last semester's questions.)

  1. Question (Fri Aug 27 22:19:31 CDT 2004): For part e and g of Recitation 1, are we allowed to use switch statements, or did you want us to use only if/else loops?

    Answer: Sure, switchs are fine, just no gotos. My examples in Pascal use a case statement, which is the same as switch in C. A switch is just a fancy if-then-else.


  2. Question (Sat Aug 28 13:10:59 CDT 2004): I am working through recitation 1 and have found a slight problem. In part 4, the "C" version of the year-to-roman numeral conversion program, the variable k is only initialized in L2, in the statement k = l. This statement is only executed if n%4 == 1. In these programs, you have n hard-coded to be 13, so of course n%4 is always 1 and we don't really have to worry about it.

    My question is, k is missing an initialization if n were ever an even number. Do you want us to repair this issue for the program that we turn in?

    Answer: The only actual input to the program is the input number to be converted to Roman numerals, here using the variable rom. I didn't use a scanf or such because I'm presenting several different languages. The variable n is used in the algorithm and must be initialized to 13 as a basic part of the algorithm. Initialization to a value besides 13 is not an option. (This only appears to be a problem because of the way I obscured the underlying algorithm, from which I derived this confusing version.)


  3. Question (Sun Aug 29 20:54:32 CDT 2004): Does the Java source program that recovers the hidden loops and the structure of the C programs get rid of the switch statement?

    Answer: Your question is slightly confusing to me. In part e of the submission, you are to submit a "Java source program that recovers the hidden loops and the structure of the C program in Section 4". There is no switch statement in this program to get rid of. The earlier method for getting rid of the gotos does insert a switch, but you are not working with that program. You are working with the original program in Section 4, and you are to try to recover the original program. It might have had various constructs, including loops, if-then-elses, and even switchs. Also, there is no single correct answer to this question, and a given use of gotos and labels might be translated to high-level constructs in many different ways. In particular, a switch can always be represented using an if-then-else, and it is impossible to know what the original might have been.


  4. Question (Thu Sep 9 12:36:35 CDT 2004): Is there a way to print out a shortened version of a double in java? I can't seem to remember how to accomplish this task.

    As an example, for '.6e-2', i can only seem to print out '0.006000000000000002', which is obviously a great deal larger than '0.0060'.

    Answer: The form '0.006000000000000002' is fine as output for the input '.6e-2'. As I'm sure you know, it's actually just a tiny bit larger, but it does have a lot more significant digits. It has this form because the machine you ran it on is representing a double in binary, and it cannot represent 0.6 exactly. Hence the minutely larger value.

    Java (as well as C and C++) does allow you to control the number of digits on output. For example


  5. Question (Thu Sep 9 23:21:21 CDT 2004): When will you allow for submission of recitation 3?

    Answer: The final due date for Recitation 2 is Friday, 10 September 2004, 23:59:59. Recitation 3 is due for full credit the following Monday,13 September 2004, 23:59:59. Normally, on Saturday (this time, Saturday, 11 September 2004, I'll turn off submissions to the previous recitation, and allow submissions to the next one. (I didn't do this last weekend because I was out of town.) The only reason for this is to keep you from getting confused with two submissions that work at the same time. If there is strong feeling about this, I can change the policy.


  6. Question (Fri Sep 10 09:24:10 CDT 2004): I have a question about Recitation 3. In the specification, it says that when it comes to the symbol table: "For an identifier, the scanner should return a "@" character (see below), and set a >>global varible<< equal to an index or pointer giving its location in the symbol table".

    I don't understand the function of this global variable. If we are keeping an Object (array of Strings) holding all the identifier names, shouldn't we just call a method in this object to obtain its location in the symbol table? And if we are keeping global variable for the identifier, should we have one global variable for each possible identifier? I'm most probably understanding this in the wrong way, because it doesn't make any sense ;)

    Then we come to the "Floating points and integers constants" section, and we have pretty much the same situation: After converting the string to an internal double "the scanner should return a "#" character (see below) and set a global variable equal to the value of the constant as a double."

    I guess my whole confussion is: will the scanner be simply outputting the information into STDOUT in the way it is shown in the recitation, or will it be returning this values to another class that will present them in the formatted output? If we actually need to use return, the class will stop execution after the first return, and it should be invoked again to restart. It makes sense that the lexical analyzer calls the scanner in a loop, but so far we have no lexical analyzer, therefore my whole confussion. [Note: the questioner meant syntax analyzer here; "lexical analyser" is another term for "scanner".]

    Answer: I think my description was confusing all right. Let's start at the beginning:


  7. Question (Wed Sep 15 22:01:14 CDT 2004): [Regarding Recitation 4 ...] How did you want us to include part 5.c. in the text file? Part 5.c. is the question asking us to convert the given grammer to a finite state machine.

    Answer: Yeah, I forgot about this. Just put it into "Ascii text" form as well as you can. With just a little work, you can do this: Don't use circles for states, but just the state number, and fake the arrows, as in the comment FSM below. Also, you can write state numbers more than once to make the arrows easier to fill in.


  8. Question (Sat Sep 18 17:57:20 CDT 2004): [Regarding Recitation 4 ...] I have a question on # 5 a & b. I can't figure out what you mean by "Write a regular grammar for [identifiers/integer constants]".

    I was thinking for identifiers to just write: E ---> id and for integer constants to write: E ---> con

    But the above seems a bit too simple... Perhaps you can give some examples of what sentences a grammar for identifiers and a grammar for integer constants each produce.

    Answer: A good question. I meant for you to use individual Ascii characters as terminal symbols. Here is the way I rewrote this part in Recitation 4:

    This is then illustrating that grammars can also handle the things that were handled by finite automata.


  9. Question (Thu Sep 23 09:15:47 CDT 2004): Is your example in the lecture for this recitation supposed to work for the java code reference? The reason I ask is that I copied and pasted the code for Arith0.java into my own directory and it gave me errors. ... (Later email) ... I figured out the problem I was having. In the java source code that you supply on the website, the GetChar class is missing. It is available, however, in the text file with the output.

    Answer: I don't like using the "Keyboard" class from CS 1713, since it is not standard Java, but is just a class for inputting data supplied by one author. For this reason I usually supply my own input classes, which are much simpler because they are not general-purpose.

    I have cleaned up the page Recursive Descent Parsing so that the presence of the other class is clear.

    The latest version of Java is supposed to have standard input classes.


  10. Question (Thu Sep 23 12:36:05 CDT 2004): I am a bit confused about Recitation 5. Are we supposed to construct the parser so that it generates output like the examples you've shown us (such as the run for Arith0.java or the "Parser output" link you gave on the recitation after "Sample Input 2") or are we supposed to construct the parser so that it generates the Fibonacci numbers, as it states the program produces?

    Additionally, if our parser generates output similar to the examples, would it be all right if we add an additional function to print the characters that are skipped in your output examples?

    Answer: You are supposed to produce parser output similar to the examples given, showing the structure of the calls needed to do the parse. (You can just copy my enter and leave functions, or do something else to convince yourself and me that the parser is working.) The sample inputs to your parser are programs in the Tiny® language. Our goal over the next two recitations is to add code to the parser that will translate Tiny programs into MIPS assemply code. These can then be executed in the MIPS simulator, and we will indeed be able to produce the Fibonacci numbers promised for the two sample programs.

    My own sample output does not include all input characters. You can use your own (different) output, and this can include all input characters it you want.


  11. Question (Sat Sep 25 15:54:49 CDT 2004): Your sample output for Recitation 5 has enter: L and leave: L. Where is the L symbol in the grammar table?

    Answer: The sample output is for the source program: Sample Input 2. Just before this link, Recitation 5 says: "Here is roughly what your parser output might look like with the above input (using a slightly different parser)." This was intended to show the general form of the output, but it is not exactly what your parser should produce. In particular, as you note, this parser has a non-terminal L whereas yours should not.


  12. Question (Sat Sep 25 21:38:48 CDT 2004): For symbol G, should the parser only accept numeric digits as input? I am going by the Recitation 5 website. The table reads G is represented by the rule G ---> '>' lower-case ';' but the description reads G is for integer input.

    Answer: The syntax of the G statement is a greater than: '>', followed by a lower-case letter, followed by a semi-colon: ';'. That's all you need to worry about for Recitation 5.

    The semantics of the G statement is to input an integer value (which is typed as input and is not part of the program). This value will be assigned to the variable represented by the "lower-case letter". This construct was partly suggested by the C++ input statement: cin >> x;, which reads in a value to assign to the variable x. However, I wanted only single-character tokens. You will implement the semantics in Recitation 7.

    [This is an interesting question. The grammar rule G ---> '>' lower-case ';' gives the syntax, and specific input for such a statment could look like: > x; This is all at compile-time. After compilation, at run-time an actual integer constant will be handled as input, using a translation of this statement. The integer value will be put into a memory location correponding to the variable x. Notice that at run-time there will usually be no name x, but just a memory location corresponding to the compile-time name x.]


  13. Question (Mon Sep 27 16:04:42 CDT 2004): For Recitation 6, did you want us to read in the text file, then spit out the mips translation into a '.s' file, and then run spim on the '.s' file? For example, read in '< B;', concatenate 'li $v0, 4\n la $a0, 64($s1)\n syscall' to a '.s' file and then run spim on the '.s' file to print the blank?

    Answer: That's right. Handling '< B;' is particularly easy, since it has a fixed translation to MIPS assembly code. Other constructs require dealing with values in variables at run-time; we will go over this in class (and it's on the web site).


  14. Question (Wed Sep 29 20:59:41 CDT 2004): In Recitation 6, I'm having some problems implementing the "mult" aspect of the generated mips code.

    [There follows MIPS code including the instruction:

    which produces an error when run with spim. In fact the error is that mult has only two explicit register operands (see below).]

    Can you offer any guidance for this? I haven't even tried the divide or mod yet, so if you could give some hints on implementing those as well, it would be appreciated.

    Answer: See Appendix A of the third edition of the CS2733 text (on the CD) for a list of MIPS instructions, or else Appendix A (PDF) of the second edition of the text. There you see that mult (to multiply) is a hardware instruction that uses special hardware registers lo and hi, which can be accessed after the instruction using other MIPS instructions (mfhi, move from hi, and mflo, move from lo,and others). This would be kind of messy, but there is a pseudoinstruction that works for our purposes: mul, with three register operands just like add. Similarly there are pseudoinstructions div (for divide) and rem (remainder on division) that also work like add.


  15. Question (Mon Oct 18 19:46:09 CDT 2004): Are you going to post the answers to the Spring '04 midterm? You have the answers to all of the other midterms posted online, but not this one.

    Answer: I didn't get around to posting answers for this one, and there is only one weird question anyway: 6. For Question 6 if you wanted more information you could just download it and run it. (You would also need my GetNext class, which is posted at various places on the class web pages.)


  16. Question (Fri Oct 22 15:00:21 CDT 2004): In QuickSortTest.java in the checkSorted method is the if statement correct? Should it be or

    This is because what if 2 numbers are the same?

    Answer: If any a[i] > a[i+1] then the numbers are not sorted into order (we say "increasing" order, but we really mean "non-decreasing" order). With quicksort, It's all right if some of the numbers are equal. And that still counts as sorted order. (Given the way the random number generator is used, equal numbers have almost no chance of appearing.)


  17. Question (Wed, 27 Oct 2004 09:20:28 (CDT)): For the parts of Recitation 10: that require us to write a function, would you like us to simply "cat -n" the file with the function in it or would you also like us to include a sample run(s) of the function in the text file we are to submit?

    Answer: Of course you must supply sample runs of any functions you submit, as well as a listing of the function. The Submission Guidelines say: "Unless explicitly stated otherwise, programs must always be followed by the results of a run."


  18. Question (Wed, 27 Oct 2004 08:35:01 (CDT)): For part 9 of Recitation 10:, I am experiencing some difficulty. I am able to get the list to print in reverse at the top level only, however instead of printing it just as a neat list, it is adding parenthesis and dot operators. Any idea what I am doing wrong? My code follows.

    Answer: In your particular case, your last line is consing a list with an atom, to produce the final answer. That's not what you want to do.

    For example, suppose your original list is (a b c d). Suppose you are at the last stage of reversing, where you have reversed the cdr to form (d c b) and want to tack and a on at the end. Your code does something like this:

    The result is of course not (D C B A). What you want to do (somehow) is the following at the end:


  19. Question (Mon Nov 1 19:22:51 CST 2004): Re: Lisp with Recitation 10: While running clisp, can I modify the function code and then reload it with the (load "*") command? Or must I restart clisp and then load it?

    Answer: The new loaded definition will overwrite the old one, so you don't need to restart, assuming that you have a separate editor going. Here's an interactive session:


  20. Question (Tue Nov 16 09:36:54 CST 2004): In Recitation 12 my star with inputs (20,6) looks exactly the same as the one with inputs (10,3) and it doesn't look like it's because it's saving an old file, because renaming the pdf doesn't change the output. I also traced the corners of the star and after 10 corners, it started repeating so I can't tell how you got that star to look that way.

    I wanted to let you know that the only way I could get the (20,6) star to look the way you have it online is to have one for loop to draw the 10 sided star, then to rotate by 18 (360/20) and then run the same loop again. Is this ok to do?

    Answer: Yes, that is fine to do. This problem is the reason that I suggested doing the program the way it is described in the writeup: drawing each line separately, or alternatively drawing one line, rotating by 18 (360/20) drawing the same line, and so on for the 20 lines, in the case of a (20,6) star.

    This problem is surely why someone asked in class about PDF not updating. I didn't understand then, but this student was probably drawing a (10,3). Then the student tried a (20,6) as you are doing and produced the same output, so PDF was updating after all.

    If you try do draw a continuous path, this is more complicated because a (20,6) has two separate paths, one like the (10,3), and the other like the (10,3) rotated by 18 degrees. This approach is more complex (although it produces better output), because in the most general case there is no limit to the number of separate paths. So for example, a (9,3) requires 3, a (16,4) requires 4 paths, and so forth. It's possible to write a program to handle this most general case, and still create each path as a complete path, and not as separate lines, but this would be a little more complicated. Instead, as I said above, I just recommended drawing each line separately, and this is fairly easy.


  21. Question (Tue Nov 30 08:32:36 CST 2004): I'm having some difficulty figuring out how to increment the date within the incr_hour function. Either it has some inheritance issue or it just prints out the correct times and doesn't increment the date.

    Answer: I looked up my code for this. I used the Date class just as I gave it to you. Then I defined a Time class that inherits (or extends) Date as I suggested in the write-up. Finally, from within my incr_hour inside Time I was able to call incr_day without any trouble. It worked just as I expected it to: Time inherits the incr_day method from Date. You must be making some other error. Email me you code and I'll look at it.