//remember to modify for <!...> and <?...>
#include <stdio.h>
#include <stdlib.h>

#define DEBUG_PARSER 1
#include "grammar.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, "\n//\\\\ P<%s> = ", phrasename[p-512]);
    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//\\\\   ");
      for (each = 0; each < phrases; each++) {
        int phrase = gram[gp];
        if (phrase < 256) {
          fprintf(stdout, " '%c'", phrase);
        } else if (phrase < 512) {
          fprintf(stdout, " \"%s\"", keyword[phrase-256]);
        } else if (phrase < 512+MAX_BIP) {
          fprintf(stdout, " <%s>", phrasename[phrase-512]);
        } else {
          fprintf(stdout, " <%s>", phrasename[phrase-512]);
        }
        gp++; // move over element
        fflush(stdout);
      }
      if (i+1 < alts) fprintf(stdout, ","); else fprintf(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);
}