CS75 Spring 2007
Project 2: Error Handling


Below are some examples of C-- code that can be used to test the error handling of your parser. In some cases, your parser should be able to recover from the error and continue parsing. In other more problematic cases, the parser can immediately quit.

  1. C-- code

    int main() {
      int x;
      int y;
      
      x = 0
      y = 5;
    
      while (x < y) {
        write x
        writeln;
        x = x + * 1;
      }
    }
    

    Parser output

    Error on line 6: expected semi, but found id
      y = 5;
    
    Error on line 10: expected semi, but found writeln
        writeln;
    
    Error on line 11: invalid expression
        x = x + * 1;
    
    Parser found 3 errors.
    
  2. C-- code

    int x;
    
    int double(int y) {
      return 2*y;
    }
    
    int main() {
      read x;
      write double(x);
      return 1
    

    Parser output

    Error on line 12: expected semi, but found done
    
    Error on line 12: unexpected end of file, missing closing brace
    
    Parser found 2 errors.
    
  3. C-- code

    int x;
    
    int double(int y) {
      return 2*y;
    
    
    int main() {
      read x;
      write double(x);
      return 1;
    }
    

    Parser output

    Error on line 7: invalid expression
    int main() {
    
    Error on line 7: unexpected end of function, missing closing brace
    
    Parser found 2 errors.
    
  4. C-- code

    int x;
    char ;
    
    int main() {
        x = 10;
        y = 'a';
        write x;
        write y;
    }
    

    Parser output

    Error on line 2: missing identifier in declaration
    char ;
    
    Parser found 1 error.
    
  5. C-- code

    int x;
    char y
    
    int main() {
        x = 10;
        y = 'a';
        write x;
        write y;
    }
    

    Parser output

    Error on line 4: expected lparen, but found int
    int main() {
    
    Parser found 1 error.