/* recursive parser */
#define GNC()   ch=*++txt
#define NO()    printf("error\n"), exit(1)
#define RULE(n) printf("r%d\n", n)

static char *txt;
static char ch;

static void P(void);       /* C forward declarations */
static void D(void);
static void C(void);
static void N(void);
static void B(void);

static void D(void) {
  C();
  RULE(2);
  while (ch=='|') {
    GNC();                    /* discard | */
    C();
    RULE(1);
  }
}
static void C(void) {
  N();
  RULE(4);
  while (ch=='&') {
    GNC();                    /* discard & */
    N();
    RULE(3);
  }
}
static void N(void) {
  if (ch == '~') {
    GNC();                    /* discard ~ */
   B();
    RULE(5);
  } else {
   B();
    RULE(6);
  }
}
static void B(void) {
  switch (ch) {
  case 't':
    GNC();                    /* discard t */
    RULE(7);
    break;
  case 'f':
    GNC();                   /* discard f */
    RULE(8);
    break;
  case '(':
    GNC();                    /* discard ( */
    D();
    if (ch != ')') NO();
    GNC();                    /* discard ) */
    RULE(9);
    break;
  default:
    NO();
    break;
  }
}
static void P(void) {
  D();
  if (ch != 0) NO();          /* null terminated */
  RULE(0);
}
int main(int argc, char **argv) {
  txt = argv[1];
  ch  = *txt;                 /* first char */
  P();
  return 0;
}

Created: March 29, 2001
Last modified: May 29, 2007