#include <stdio.h>
#include <stdlib.h>

#define DEBUG_PARSER 1
#include "teeny.h"

// Reconstitute grammar from tables.
// Parsing code will be very similar, except recursing from PHRASE_BASE
// rather than scanning sequentially from there.

int main(int argc, char **argv)
{
  int p, i, gp, alts;

  p = PHRASE_BASE;
  gp = 0;

  for (;;) { // All phrases in grammar, sequentially
    alts = gram[gp];
    fprintf(stdout, "\nPhrase %s/%d (%d alternatives) = ", phrasename[p-512], p, alts);
    fprintf(stdout, "(pi=%d gp=%d) ", phrase_start[p-512-MAX_BIP], gp);
    gp++; // gp now points to first element (length) of first alt
    fflush(stdout);
    for (i = 0; i < alts; i++) {
      int each, phrases = gram[gp++];
      fprintf(stdout, "\n   Alternative %d: (%d phrases) ", i+1, phrases);
      for (each = 0; each < phrases; each++) {
        int phrase = gram[gp];
        if (phrase < 256) {
          fprintf(stdout, " '%c'", phrase);
        } else if (phrase < 512) {
          fprintf(stdout, " \"%s\"/%d", keyword[phrase-256], phrase-256);
        } else if (phrase < 512+MAX_BIP) {
          fprintf(stdout, " {%s/BIP%d}", phrasename[phrase-512], BIP[phrase-512]);
        } else {
          fprintf(stdout, " <%s/%d>", phrasename[phrase-512], phrase);
        }
        gp++; // move over element
        fflush(stdout);
      }
      // gp now points to first element (length) of next alt, or start of next phrase
    }
    p++;
    fprintf(stdout, "\n");
    if (p == (512+MAX_PHRASE)) break;
  }

  exit(0);
}