CSE 109 Test 2  Wednesday  16 November 2005
<<<<<<<<<<<<<<<<<<<SUGGESTED ANSWERS>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1.  Write the MACH1 code that corresponds to the VE program below. Recall
    MACH1 operators: read(0), write(1), load(2), store(3), clear(4), add(5),
    sub(6), mult(7), div(8), addi(9), subi(10), multi(11), divi(12),
    jumpl(13), jumpg(14), halt(15).

    x
    begin
    read x
    write 10
    if x<=15 goto g
    write x
  g: halt
    end
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    0 0              0
    3 10     read x  1
    2 11             2
    1 0     write 10 3
    2 10             4
    6 12     x-15    5
    13 9             6
    2 10             7
    1 0     write x  8
    15 0    halt     9
    0  0      x     10
    0 10            11
    0  15           12
    E
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2. Write a program that lists the strings entered on the command line and
   then states which correspond to an existing file.  Thus, if the executable
   file for the program is stored in a.out and the files a, b, and c exist
   in the directory, then the command
       a.out a d p q
   would display something like
      The file 'a' exists
      The file 'd' does not exist
      The file 'p' does not exist
      The file 'q' does not exist
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#include <fstream.h>
int main(int ct,char **arg)
{ifstream f;
 for(int j=1; j<ct; j++)
 {f.open(arg[j]);
  cout<<"The file '"<<arg[j]<<"'";
  if(f.good())
   {f.close();
    cout<<" exists\n";
   }
  else
    cout<<" does not exist\n";
 }
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3. Write a program that assumes class Lex from program 4 and determines
   whether a single line of input from the console obeys the syntax given
   by the following diagram, where [] indicate a rectangle and () indicate
   an oval.
             ----------->[IDENT]---->[IDENT]---->(:)---------+
                  |                                          |
                  +------>(WRITE)---->[IDENT]-------------------->
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#include "lex.h"

void check(bool b)
{if(!b)
  {cerr<<"\nFailure to parse\n";
   exit(1);
  }
}

int main()
{Lex lex(cin,cout);
 int tok;
 tok=lex.next();
 switch(tok)
 {case Lex::IDENT: check(lex.next()==Lex::IDENT);
                   check(lex.next()==Lex::COLON);
                   break;
  case Lex::WRITE: check(lex.next()==Lex::IDENT);
                   break;
  default: check(false);
 }
 check(lex.next()==Lex::EOLN);
 cout<<"\nSuccessful parse\n";
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
4.  Write an EASY program that reads ints and displays those between
    8 and 12 inclusive and that stops when an int less than 0 is
    encountered.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    x
    begin
 loop:
    read x
    if x<0 goto stop
    if x<8 goto loop
    if x>12 goto loop
    write x
    goto loop
 stop:
    halt
    end
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

