#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char **argv)
{
  int c, lastc;
  static char key[128], *s;
  // a60 single-quote stropping
  for (;;) {
    c = fgetc(stdin);
      if (c == EOF) {
        exit(0);
      }
    for (;;) {
      if (c == '\'') {
        // keyword *OR* exponent
        s = key;
        c = fgetc(stdin);
      if (c == EOF) {
        exit(0);
      }
        if (!isalpha(c)) {
          fprintf(stdout, "@");
          lastc = c;
	} else {
          for (;;) {
            if (c == '\'') break;
            if (isalpha(c) && isupper(c)) c = tolower(c);
            if (c != ' ') *s++ = c; // also should err on non-alpha
            c = fgetc(stdin);
      if (c == EOF) {
        exit(0);
      }
	  }
          *s = '\0';
	  if (strcmp(key, "end") == 0) putchar(';'); // special handling of comments after END would be here
          fprintf(stdout, "%s", key);
          // may need a ';' *before* "end"???
          if (strcmp(key, "end") == 0) {
            do {
              c = fgetc(stdin);
      if (c == EOF) {
        exit(0);
      }
	    } while (c == ' ' || c == '\n');
            if (c == '\'') continue; // "END""ELSE"
            for (;;) { // "END" comment;
              if (c == ';') break;
              if (c == '\'') break;
              if (c == '\n') c = ' '; putchar(c);
              c = fgetc(stdin);
      if (c == EOF) {
        exit(0);
      }
	    }
            if (c == '\'') {
              /*putchar(lastc = ';');*/ // "END" whatever "END" another;
              continue; // handle keyword in outer loop
	    }
            putchar(lastc = c);
            break;
          } else if (strcmp(key, "comment") == 0) {
            for (;;) {
              c = fgetc(stdin);
      if (c == EOF) {
        exit(0);
      }
              if (c == ';') break;
              putchar(c);
	    }
          } else if (strcmp(key, "begin") == 0) {
 	    // output ';'
	  } else break;
          c = lastc = ';';
	}
      }
      if (c == '`') {
         // string - should I swap the string quotes so it is recognised by a60tohtml?
         //          probably better to modify a60tohtml instead 
         do {
            putchar(c); c = fgetc(stdin);
      if (c == EOF) {
        exit(0);
      }
         } while (c != '\'');
      }
      if (isblank(c)) break;
      if (c == '\n') {
	//        if (lastc == ';') break;
	//        c = ';';
        break;
      }
      if (c == '!') c = '#';
#ifdef NEVER
      if (c == '^') {
        do {
          lastc = c = fgetc(stdin);
          if (c == EOF) {
            exit(0);
          }
	} while (c == ' ');
        if (c == '=') {
          putchar(lastc = '<'); c = '>'; // not-equal "|="
        } else {
          putchar(lastc = '~'); // unary not
	}
      }
#endif
      if (isalpha(c)) {
        char variable[1024], *var = variable;
        if (islower(c)) c = toupper(c);
        putchar(c);
        for (;;) {
          lastc = c;
          c = fgetc(stdin);
      if (c == EOF) {
        exit(0);
      }
	  if (isalpha(c) && islower(c)) c = toupper(c);
          if (!(isalpha(c) || isdigit(c) || (c == ' '))) {
            break;
	  }
          if (c == ' ') c = '_';
          *var++ = c;
	}
        while ((var != variable) && (*(var-1) == '_')) var -= 1;
        *var = '\0';
        printf("%s", variable);
        continue; // c is the character after the variable
      }
      putchar(c);
      lastc = c;
      c = fgetc(stdin);
      if (c == EOF) {
        exit(0);
      }
    }
  }

}
