#include <perms.h>
void Oldps(_imp_string S) {
  _imp_string Date(void);
  int I;
  int J;
  int K;
  int Ss;
  int Nbip;
  _imp_string St49;
  _imp_string St50;
  _imp_string St51;
  void Define(_imp_string S);
  int Clett[1001 /*0:1000*/];
  int Symbol[1701 /*1300:3000*/];
  int Cc[301 /*0:300*/];
  int Cnext;
  int Dnext;
  int Cnum;
  int Dnum;
  int Alt;
  int Def;
  int Asl;
  int Nidflag;
  int Kk[200 /*1001:1200*/];
  int Dword[200 /*1001:1200*/];
  int Dlett[1001 /*0:1000*/];
  int Cword[200 /*1:200*/];
  auto void Readstring(int Terminator);
  auto void Record(int Word, int Lett, int *Num, int *Next);
  auto void Lookup(int Word, int Lett, int First, int Last, int Insert);
  auto void Pddict(void);
  auto void Printstr(int Cc, int Ptr);
  auto void Pcdict(void);
  if (!_imp_resolve(S, St49, _imp_str_literal(","), St50, _imp_str_literal(","),
                    St51)) {
    Printstring(_imp_str_literal("PARAMS?????"));
    Newline();
    return;
  }
  Selectinput(49);
  Selectoutput(51);
  Asl = 1300;
  Cnext = 0;
  Dnext = 0;
  Cnum = 0;
  Nidflag = 0;
  Dnum = 1000;
  Read(Nbip);
Nexts:
  Readsymbol(I);
  if (I == 'D') {
    Readsymbol(I);
    Readstring(')');
    Record(Dword, Dlett, Dnum, Dnext);
    goto Nexts;
  }
  if (I == 'P') goto Phrs;
  if (I == 'E') goto Eend;
  goto Nexts;
Phrs:
  Newlines(2);
  Printsymbol(I);
  Readsymbol(I);
  Printsymbol(I);
  Readstring(')');
  Printstr(Cc, 0);
  Printsymbol(')');
  Lookup(Dword, Dlett, 1001 + Nbip, Dnum, 0);
  Kk[I] = Asl;
  Def = Asl;
  Alt = Asl + 1;
  Asl += 2;
Nextp:
  Readsymbol(I);
  Printsymbol(I);
  if (I == '(') goto Biporphr;
  if (I == '\'') goto Lit;
  if (I == ',') {
    Symbol[Alt] = Asl;
    Alt = Asl;
    Asl++;
    goto Nextp;
  }
  if (I == ';') {
    Symbol[Alt] = Asl;
    Symbol[Def] = Asl;
    goto Nexts;
  }
  if (I == '0') {
    Symbol[Asl] = 1000;
    Asl++;
    goto Nextp;
  }
  if (I == '*') {
    Symbol[Asl] = 999;
    Asl++;
    goto Nextp;
  }
  goto Nextp;
Biporphr:
  Readstring(')');
  Printstr(Cc, 0);
  Printsymbol(')');
  Lookup(Dword, Dlett, 1001, Dnum, 0);
  Symbol[Asl] = I;
  Asl++;
  goto Nextp;
Lit:
  Readstring('\'');
  Printstr(Cc, 0);
  Printsymbol('\'');
  Lookup(Cword, Clett, 1, Cnum, 1);
  Symbol[Asl] = I;
  Asl++;
  goto Nextp;
Eend:
  Pcdict();
  Pddict();
  for (I = 1300; I <= Asl - 1; I++) {
    if (1 <= Symbol[I] && Symbol[I] <= Cnum) Symbol[I] = Cword[Symbol[I]];
    if (1001 + Nbip <= Symbol[I] && Symbol[I] <= Dnum)
      Symbol[I] = Kk[Symbol[I]];
  }
  Ss = Kk[Dnum];
  Newline();
  for (K = 1; K <= 2; K++) {
    if (K == 2) Selectoutput(50);
    Printstring(_imp_join(
        _imp_str_literal("! PRODUCED BY OLDPS FROM "),
        _imp_join(St49, _imp_join(_imp_str_literal(" ON "), Date()))));
    Printstring(_imp_str_literal("\n%CONSTBYTEINTEGERARRAY CLETT(0:"));
    Write(Cnext - 1, 1);
    Printstring(_imp_str_literal(")="));
    for (I = 0; I <= Cnext - 1; I++) {
      Write(Clett[I], 3);
      J = ',';
      if (I == Cnext - 1) J = ';';
      Printsymbol(J);
      if (!(I - (I / 14) * 14)) Newline();
    }
    Newlines(2);
    Newlines(2);
    Printstring(_imp_str_literal("\n%CONSTINTEGERARRAY SYMBOL(1300:"));
    Write(Asl - 1, 1);
    Printstring(_imp_str_literal(")="));
    for (I = 1300; I <= Asl - 1; I++) {
      Write(Symbol[I], 5);
      J = ',';
      if (I == Asl - 1) J = ';';
      Printsymbol(J);
      if ((I - 1299) - ((I - 1299) / 10) * 10 == 1) Newline();
    }
    Newlines(2);
    Printstring(_imp_str_literal("%CONSTINTEGER SS="));
    Write(Ss, 1);
    Newlines(2);
    if (Nidflag) return;
  }
  Selectoutput(99);
  Printstring(_imp_str_literal("NO ERRORS\n"));
  return;
  void Readstring(int Terminator) {
    int Under;
    Under = 0;
    J = 0;
  Nexts:
    Readsymbol(I);
    if (I == '%') {
      Under = 128;
      goto Nexts;
    }
    if ('A' <= I && I <= 'Z')
      I += Under;
    else
      Under = 0;
    if (I == 32) goto Nexts;
    if (I == Terminator) {
      Cc[0] = J;
      return;
    }
    J++;
    Cc[J] = I;
    goto Nexts;
  }
  void Record(int Word, int Lett, int *Num, int *Next) {
    *Num = *Num + 1;
    Word = *Next;
    for (I = 0; I <= Cc[0]; I++) Lett = Cc[I];
    *Next = *Next + Cc[0] + 1;
  }
  void Lookup(int Word, int Lett, int First, int Last, int Insert) {
    I = First;
    while (I <= Last) {
      J = Word;
      for (K = 0; K <= Lett; K++)
        if (Lett != Cc[K]) goto Nxt;
      return;
    Nxt:
      I++;
    }
    if (!Insert) {
      Printstring(_imp_str_literal(
          "\n****************PHRASE NOT IN DICTIONARY***********   "));
      Printstr(Cc, 0);
      Nidflag++;
      return;
    }
    Record(Word, Lett, Cnum, Cnext);
    I = Cnum;
  }
  void Pcdict(void) {
    int J;
    int K;
    int L;
    Newpage();
    Printstring(_imp_str_literal(" KEY TO LITERAL DICTIONARY (CLETT)\n"));
    for (J = 1; J <= Cnum; J++) {
      K = Cword[J];
      Write(K, 4);
      Spaces(2);
      Printstr(Clett, K);
      Spaces(17 - Ss);
      if (!(J & 3)) Newline();
    }
  }
  void Printstr(int Cc, int Ptr) {
    int I;
    int J;
    int K;
    int Del;
    Ss = Cc;
    K = Ss;
    Del = 0;
    for (I = 1; I <= K; I++) {
      J = Cc;
      if (Del == 0 && J > 128) {
        Ss++;
        Del = 1;
        Printsymbol('%');
      }
      Printsymbol(J);
    }
  }
  void Pddict(void) {
    int J;
    Newlines(4);
    Printstring(_imp_str_literal(" KEY TO MAIN TABLE (SYMBOL)\n"));
    for (J = 1001; J <= Dnum; J++) {
      if (J <= 1000 + Nbip)
        Write(J, 4);
      else
        Write(Kk[J], 4);
      Spaces(2);
      Printstr(Dlett, Dword[J]);
      Spaces(17 - Ss);
      if (!((J - 1000) & 3)) Newline();
    }
  }
}
