Scanner assignment

Description

Write the scanner, the first phase of your compiler.  The scanner converts an input program into tokens and rewrites the program with some tokens modified.  If the input program is a correct C program, the output program should also be a correct program that has identical behavior.  The specific requirements for code generation are: For example, the following program should be converted as follows.

example program :  foo.c

    #include <stdio.h>
    #define read(x) scanf("%d\n", &x)
    #define write(x) printf("%d\n", x)

   // function foo
    void foo() {
        int a;
        read(a);
        write(a);
    }

    int main() {
        foo();
    }

running your scanner:

% scanner foo.c

generated program (no formatting needed): foo_gen.c

    #include <stdio.h>
    #define read(x) scanf("%d\n", &x)
    #define write(x) printf("%d\n", x)

   // function foo
   void cs254foo() {  int cs254a;  read(cs254a);  write(cs254a);  }  int main() { cs254foo(); }

Your scanner will be tested by whether the generated program gives the same execution result as the input program.

Recommended implementation:

With this interface, you can scan through and print a program as follows:

    Scanner scanner("test1.c");  // Initialize the scanner.
    While (scanner.HasMoreTokens()) {
        Token t = scanner.GetNextToken(); 
        if (t.GetTokenType()==ID && t.GetTokenName()!="main")
               ... ... // print token with cs254 attached
        else
               t.Print();
    }