RHODES UNIVERSITY

Examinations - January 2017


Computer Science 301 - Paper 1

(Free information made available to the students 16 hours before the formal examination)

Here are suggestions for how the problems posed this morning might be solved. Full details are not given here, but are easily added - simple additions to the list of opcodes in the PVM, and corresponding additions to the code generator. In tomorrow's formal exam you will receive a version of the Parva sources in which these insertions have all been made.

To handle set intersections and symmetric set differences, the production for MultExp can be modified as follows

  MultExp<out int type>                  (. int type2;
                                            int op; .)
  =  Factor<out type>
     { MulOp<out op>
       Factor<out type2>                 (. if (IsArith(type) && IsArith(type2)) {
                                              type = Types.intType;
                                              CodeGen.BinaryOp(op);
                                            }
                                            else if (IsSet(type) && IsSet(type2)) {
                                              switch (op) {
                                                case CodeGen.mul : CodeGen.BinaryOp(CodeGen.isx); break;
                                                case CodeGen.div : CodeGen.BinaryOp(CodeGen.xor); break;
                                                default: SemError("invalid set operation"); break;
                                              }
                                              if (type != type2) type = Types.setType;
                                            }
                                            else {
                                              SemError("arithmetic operands needed");
                                              type = Types.noType;
                                            } .)
     }  .

To clear all elements from a set, a simple ClearStatement can be added to the collection of statements

  ClearStatement                         (. DesType des; .)
  =  "Clear" "(" Designator<out des>     (. if (des.entry.kind != Kinds.Var)
                                              SemError("variable designator required");
                                            if (des.cannotAlter)
                                              SemError("you may not alter this variable");
                                            if (des.type != Types.setType)
                                              SemError("set designator needed");
                                            CodeGen.Clear(); .)
     ")" WEAK ";" .

Finally, to handle function prototypes one can modify the FuncDeclaration production as follows:

  FuncDeclaration
                                         (. StackFrame frame = new StackFrame();
                                            Entry function = new Entry(); .)
  =  "void" Ident<out function.name>     (. function.kind = Kinds.Fun;
                                            function.type = Types.voidType;
                                            function.nParams = 0;
                                            function.firstParam = null;
                                            Entry oldEntry = Table.Find(function.name);
                                            bool completing = !oldEntry.defined;
                                            if (!completing) Table.Insert(function);
                                            Table.OpenScope(); .)
     "(" FormalParameters<function> ")"  (. frame.size = CodeGen.headerSize + function.nParams; .)
     (                                   (. if (completing) {
                                              oldEntry.entryPoint.Here();
                                              oldEntry.defined = true;
                                            }
                                            else {
                                              function.entryPoint = new Label(known);
                                              function.defined    = true;
                                            }
                                            if (function.name.ToUpper().Equals("MAIN")
                                                && !mainEntryPoint.IsDefined()
                                                && function.nParams == 0) {
                                              mainEntryPoint.Here();
                                            } .)
          Body<frame>
       | ";"                             (. function.entryPoint = new Label(!known);
                                            function.defined = false; .)
     )                                   (. Table.CloseScope(); .) .


Home  © P.D. Terry