Computer Science 301 - 2016 - Test on Week 7

TEXTBOOKS MAY NOT BE USED.

Preferably answer this test directly on the computer. Max 20

  • Immediately after logging on, get to the command line level in the usual way.

  • Copy the test kit into a newly created directory/folder in your file space from the web page or by

    cd j:
    md test7
    cd test7
    copy i:\csc301\trans\test7.zip
    unzip test7.zip

  • You will find the executable version of Coco/R and the familiar batch files for running it, the frame files, and the above grammar spread out to make for easier addition of the actions and attributes and with some hints as to where you might add other methods into the parser, as well as a data file for test purposes.

    CALC.ATG CALC.TXT CALC.BAD *.BAT *.FRAME TEST.TXT TEST.DOC

  • You can compile and run the calculator in the usual way:

    cmake calc
    calc calc.txt

    The questions below are to be found in a file TEST.TXT or TEST.DOC (MS word) Preferably answer the test by typing your answers in the obvious blank spaces. If you really don't feel up to this, write them by hand on the test sheet and hand this in instead. Either print the file or e-mail it to me when you are finished.

    In the examination at the end of the year you will be given a fairly big problem to solve 24 hours ahead of the exam proper. The problem might be similar in complexity to the calculator prac that you did last week. At the end of that day you will receive further hints on how to solve the problem. During the exam you will be asked probing questions on the problem and its solution.

    This test should give you an idea of what those sorts of questions might be like.

    A copy of the grammar in the test kit is available for your use and reference. It is very similar (but not identical) to the one that was printed for you and handed back with your pracs on Monday.

    Here is a typical question:

    Suppose you presented the calculator with a sequence of assignment that looked like this

                   A = 12;
                   B = 26;
                   C = A + B
                   print C, A > B;
                   C = A > B;
                   print C , A > B;
                   C = C + 12;
    

    What would it do? If you are not happy with this, how would you improve the system?

    The examiners are looking to see how much insight you have into using a tool like Coco and in designing grammars.

    Good luck

    WHAT ARE YOUR NAME AND STUDENT NUBER

    You will notice that the production for ANumber has actions that catch potential exception that could be raised by Convert.ToInt32(). (Line 249). But the number terminal can consist of nothing but a string of digits. Is it possible for this exception to be thrown (if so how) or is this just an example of over cautious programming?

    How would you modify the definition of the number terminal (line 92) so that the only number that could start with 0 would be the single digit number 0 (that is, forbid numbers like 000067)?

    Would this affect your answer to the previous question? Explain.

    Why do all the productions for the components of an Expression mark their formal parameters with the key word out?

    In line 235 a new Node object is created. What purpose might this serve - given that all the subsequent options for a Factor each construct a node in any case? Just another sloppy bit of code left lying about?

    There is a subtle error in the production for UnaryExp. See if you can spot it and correct it.

    If you study the productions for the components of an expression you will see that they are based on

    
      EqlExp = RelExp { EqlOp RelExp }.
    
      RelExp = AddExp [ RelOp AddExp ] .
    
      AddExp = MultExp { AddOp MultExp } .
    
      MultExp = UnaryExp { MulOp UnaryExp
    

    One of these uses [ ] bracketing, the others use { } bracketing. Are they correct? Explain, or correct any errors that you see.

    Is there any deep significance in the numbers used in the constant declarations in lines 81 - 85? Could one have used any arbitrary numbers? Why introduce these constant definitions in any case?

    Suppose one wished to add a function into the calculator to enable you to evaluate expressions like

    a + sqr(b + c)

    or

    sqr(sqr(34))

    where sqr means "square" and not "square root". Where and how would the grammar need modification? (Give the actual code, and the line numbers where it would need to be inserted.)

    Someone has suggested that the code in lines 101 to 111 should be replaced with

      Assignment                      (. Node rootNode;
                                         string name; .)
      = AVariable<out name>           (. Variable v = Table.Find(name);
                                         if (v == null)  // declaration
                                           v = new Variable(name); .)
      "=" Expression<out rootNode>    (. v.type  = rootNode.type;
                                         v.value = rootNode.value; .)
      SYNC ";" .
    

    What are your thoughts on the matter? Is this better/worse/the same as the original code? Give reasons.

    This week's big bonus. In the discussion on Monday we came to the conclusion that defining a calculator in this way would not afford an opportunity to incorporate "short circuit" semantics into the evaluation of Boolean expressions, because the system had to parse an expression completely, and thus would necessarily evaluate all its components. You were left with a challenge to see whether you could find a way around it.

    I am sure that, like me, you went away and thought about it some more?

    Of course it is quite possible. See if you can sketch out how it can be done, and how we can come up with a system whose semantics match the definition of short circuit semantics

    A OR B = if A then TRUE else B

    A AND B = if A then B else FALSE ( ie if !A then FALSE else B )


    Home  © P.D. Terry