Branch: Tag:

2002-05-22

2002-05-22 16:45:36 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Removed some code duplication.

Rev: lib/modules/Parser.pmod/LR.pmod/module.pmod:1.4

1:   /* -  * $Id: module.pmod,v 1.3 2002/05/22 16:16:35 grubba Exp $ +  * $Id: module.pmod,v 1.4 2002/05/22 16:45:36 grubba Exp $    *    * A BNF-grammar in Pike.    * Compiles to a LALR(1) state-machine.
986:    }       foreach (indices(conflicts), int|string symbol) { -  /* Initialize some vars here */ +     int reduce_count = 0;    int shift_count = 0; -  int reduce_rest = 0; -  int shift_rest = 0; +     int only_operators = 1;    Priority shift_pri, reduce_pri, pri;    Rule min_rule = 0; -  int conflict_free; +        /* Analyse the items */    /* This loses if there are reduce-reduce conflicts,
1031:    }    }    +  int reduce_rest = 0; +  int shift_rest = 0; +     if (only_operators) {    if (reduce_pri->value > shift_pri->value) {    pri = reduce_pri;
1169:    }    }    -  conflict_free = 0; +  int conflict_free = 0;       if (reduce_rest > 1) {    if (shift_rest) {
1573:    return(0);    }    +  while (1) { +  mixed a; +  +  /* Read some input */    value = scanner();       if (arrayp(value)) {
1582:    input = [string]value;    }    -  while (1) { -  mixed a = state->action[input]; -  -  if (object_program(a) == Rule) { +  while(1) { +  while (object_program(a = state->action[input]) == Rule) {    Rule r = [object(Rule)]a;       if (verbose) {
1593:    rule_to_string(r));    }    +  do {    if (r->action) {    /* REDUCE */    string|function func = 0;
1626:    } else {    value_stack->push(r->action());    } -  } else { -  // Default action. -  if (sizeof(r->symbols)) { - #if 0 -  value_stack->push(value_stack->pop(sizeof(r->symbols))[0]); - #else /* !0 */ -  if (sizeof(r->symbols) > 1) { -  value_stack->quick_pop(sizeof(r->symbols) - 1); +  break; // Break out of the do-while.    } - #endif /* 0 */ -  state = ([array(Kernel)]state_stack->pop(sizeof(r->symbols)))[0]; -  } else { -  value_stack->push(0); +     } -  } -  } else { +     // Default action.    if (sizeof(r->symbols)) { - #if 0 -  value_stack->push(value_stack->pop(sizeof(r->symbols))[0]); - #else /* !0 */ +     if (sizeof(r->symbols) > 1) {    value_stack->quick_pop(sizeof(r->symbols) - 1);    } - #endif /* 0 */ +     state = ([array(Kernel)]state_stack->pop(sizeof(r->symbols)))[0];    } else {    value_stack->push(0);    } -  } +  } while(0);       state_stack->push(state); -  state = [object(Kernel)]state->action[r->nonterminal]; /* Goto */ -  } else if (a) { +  state = [object(Kernel)]state->action[r->nonterminal]; /* Goto */ +  } +  +  if (a) {    /* SHIFT or ACCEPT */    if (input == "") {    /* Only the final state is allowed to shift on ""(EOF) */
1673:    value_stack->push(value);    state_stack->push(state);    state = [object(Kernel)]a; -  -  value = scanner(); -  -  if (arrayp(value)) { -  input = ([array(string)]value)[0]; -  value = ([array(mixed)]value)[1]; +     } else { -  input = [string]value; -  } -  } else { +     /* ERROR */    if (input = "") {    /* At end of file */
1693:    werror("Error: Bad state at EOF -- Throwing \"%O\"\n",    value_stack->pop());    state = [object(Kernel)]state_stack->pop(); +  continue;    } else {    werror("Error: Empty stack at EOF!\n");    return (0);
1705:    lr_error |= ERROR_SYNTAX;       werror("Error: Bad input: \""+input+"\"(\""+value+"\")\n"); -  -  value = scanner(); -  -  if (arrayp(value)) { -  input = ([array(string)]value)[0]; -  value = ([array(mixed)]value)[1]; -  } else { -  input = [string]value; +     }    } -  +  break; /* Break out of the inner while(1) to read more input. */    }    }    }   }