/* scan2.c: scanner implementation.
 * Recognizes C-type comments
 * Based on a finite-state automaton.
 * No gotos in this version, but state variable instead.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "scan.h"
char * makestr(char ch);
void scan(char *s)
{
   int state = 0;
   char ch;
   strcpy(s, "/*");
   while (state != 4) {
      switch (state) {
      case 0: ch = getchar();
              if (ch == '/') state = 1;
              else state = 0; break;
      case 1: ch = getchar();
              if (ch == '*') state = 2;
              else state = 0; break;
      case 2: ch = getchar();
              strcat(s, makestr(ch));
              if (ch == '*') state = 3;
              else state = 2; break;
      case 3: ch = getchar();
              strcat(s, makestr(ch));
              if (ch == '*') state = 3;
              else if (ch == '/') state = 4;
              else state = 2; break;
      case 4: return; break;
      } /* end of switch */
   } /* end of while */
}
/* makestr: convert a char to a string */
char *makestr(char ch)
{
   char *r = (char *) malloc(2);
   r[0] = ch;
   r[1] = '\0';
   return r;
}



