pike.git / lib / modules / Parser.pmod / LR.pmod / module.pmod

version» Context lines:

pike.git/lib/modules/Parser.pmod/LR.pmod/module.pmod:1:   /* -  * $Id: module.pmod,v 1.10 2002/06/14 14:32:04 nilsson Exp $ +  * $Id: module.pmod,v 1.11 2003/08/22 14:25:57 nilsson Exp $    *    * A BNF-grammar in Pike.    * Compiles to a LALR(1) state-machine.    *    * Henrik Grubbström 1996-11-24    */      #pike __REAL_VERSION__      /*! LALR(1) parser generator
pike.git/lib/modules/Parser.pmod/LR.pmod/module.pmod:424:    multiset(int|string) set = (<>);       foreach (items, Item i) {    if (i->offset != sizeof(i->r->symbols)) {    set[i->r->symbols[i->offset]] = 1;    }    }       report(NOTICE, "goto_set", "=> (< %s >)",    map(indices(set), symbol_to_string) * ", "); -  return (set); +  return set;    }       //! Generates the state reached when doing goto on the specified symbol.    //! i.e. it compiles the LR(0) state.    //!    //! @param symbol    //! Symbol to make goto on.    Kernel do_goto(int|string symbol)    {    multiset(Item) items;
pike.git/lib/modules/Parser.pmod/LR.pmod/module.pmod:614:    foreach (r->symbols, int|string symbol) {    res += ({ symbol_to_string(symbol), " " });    }    } else {    res += ({ "/* empty */" });    }    res += ({ "\n" });    }    res += ({ "\n" });    } -  return (res * ""); +  return res * "";    }       string cast_to_string()    {    return _sprintf();    }       //! Implements casting.    //!    //! @param type    //! Type to cast to.    mixed cast(string type)    { -  if (type == "string") { -  return(_sprintf()); +  if (type == "string") +  return _sprintf(); +  error("Cast to %s not supported\n", type);    } -  throw(({ sprintf("Cast to %s not supported\n", type), backtrace() })); -  } +        /* Here come the functions that actually do some work */       //! Sets the priority of a terminal.    //!    //! @param terminal    //! Terminal to set the priority for.    //! @param pri_val    //! Priority; higher = prefer this terminal.    void set_priority(string terminal, int pri_val)
pike.git/lib/modules/Parser.pmod/LR.pmod/module.pmod:1010:    if (i->offset < sizeof(i->r->symbols)) {    if (go_through(i->next_state, item_id + 1, current_item)) {    /* Nullable */    if ((master->offset < sizeof(master->r->symbols)) &&    (intp(master->r->symbols[master->offset]))) {    /* Don't include ourselves */    if (master != current_item) {    master->relation[current_item] = 1;    }    } -  return(nullable[i->r->symbols[i->offset]]); +  return nullable[i->r->symbols[i->offset]]; +  } else +  return 0; /* Not nullable */    } else { -  return (0); /* Not nullable */ -  } -  } else { +     /* At end of rule */    master->relation[current_item] = 1; -  return (1); /* Always nullable */ +  return 1; /* Always nullable */    }    }       static int repair(Kernel state, multiset(int|string) conflicts)    {    multiset(int|string) conflict_set = (<>);       report(NOTICE, "repair",    "Repairing conflict in state:\n%s\n"    "Conflicts on (< %s >)",
pike.git/lib/modules/Parser.pmod/LR.pmod/module.pmod:1276:    conflict_set[symbol] = 1;    }    }       if (sizeof(indices(conflict_set))) {    report(ERROR, "repair",    "Still conflicts remaining in state\n%s\n"    "on symbols (< %s >)",    state_to_string(state),    map(indices(conflict_set), symbol_to_string) * ", "); -  return (ERROR_CONFLICTS); +  return ERROR_CONFLICTS;    } else {    report(WARNING, "repair",    "All conflicts removed!"); -  return (0); +  return 0;    }    }      #ifdef LR_PROFILE   #define LR_GAUGE(X, BLOCK) \    report(NOTICE, "compile", X ": %f\n", gauge BLOCK)   #else /* !LR_PROFILE */   #define LR_GAUGE(X, BLOCK) do BLOCK while(0)   #endif /* LR_PROFILE */   
pike.git/lib/modules/Parser.pmod/LR.pmod/module.pmod:1548:    }    }    }    start_state = s_q->arr[0];    });      #ifdef LR_PROFILE    report(NOTICE, "compile", "DONE\n");   #endif /* LR_PROFILE */    -  return (lr_error); +  return lr_error;    }       //! Parse the input according to the compiled grammar.    //! The last value reduced is returned.    //!    //! @note    //! The parser must have been compiled (with compile())    //! prior to calling this function.    //!    //! @bugs
pike.git/lib/modules/Parser.pmod/LR.pmod/module.pmod:1586:       string input;    mixed value;       lr_error = 0; /* No parse error yet */       if (!functionp(scanner) &&    !(objectp(scanner) && functionp(scanner->`()))) {    report(ERROR, "parse", "parser->parse(): scanner not set!\n");    lr_error = ERROR_NO_SCANNER; -  return(0); +  return 0;    }       while (1) {    mixed a;       /* Read some input */    value = scanner();       if (arrayp(value)) {    input = ([array(string)]value)[0];
pike.git/lib/modules/Parser.pmod/LR.pmod/module.pmod:1670:       state_stack->push(state);    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) */    /* ACCEPT */ -  return(value_stack->pop()); +  return value_stack->pop();    }    /* SHIFT */    report(NOTICE, "parse",    "Shifting \"%s\", value \"%O\"", input, value);    value_stack->push(value);    state_stack->push(state);    state = [object(Kernel)]a;    } else {    /* ERROR */    if (input = "") {
pike.git/lib/modules/Parser.pmod/LR.pmod/module.pmod:1692:    lr_error |= ERROR_EOF;       if (value_stack->ptr != 1) {    if (value_stack->ptr) {    report(ERROR, "parse", "Bad state at EOF -- Throwing \"%O\"",    value_stack->pop());    state = [object(Kernel)]state_stack->pop();    continue;    } else {    report(ERROR, "parse", "Empty stack at EOF!"); -  return (0); +  return 0;    }    } else {    report(ERROR, "parse", "Bad state at EOF"); -  return(value_stack->pop()); +  return value_stack->pop();    }    } else {    lr_error |= ERROR_SYNTAX;       report(ERROR, "parse", "Bad input: %O(%O)", input, value);    }    }    break; /* Break out of the inner while(1) to read more input. */    }    }    }   }