Logo Search packages:      
Sourcecode: jasmin-sable version File versions  Download package

Symbol java_cup::runtime::lr_parser::debug_parse (  )  throws java.lang.Exception [inline, inherited]

Perform a parse with debugging output. This does exactly the same things as parse(), except that it calls debug_shift() and debug_reduce() when shift and reduce moves are taken by the parser and produces various other debugging messages.

Definition at line 695 of file lr_parser.java.

References java_cup::runtime::lr_parser::_done_parsing, java_cup::runtime::lr_parser::action_tab, java_cup::runtime::lr_parser::action_table(), java_cup::runtime::lr_parser::cur_token, java_cup::runtime::lr_parser::debug_message(), java_cup::runtime::lr_parser::debug_reduce(), java_cup::runtime::lr_parser::debug_shift(), java_cup::runtime::lr_parser::do_action(), java_cup::runtime::lr_parser::done_parsing(), java_cup::runtime::lr_parser::error_recovery(), java_cup::runtime::lr_parser::get_action(), java_cup::runtime::lr_parser::get_reduce(), java_cup::runtime::lr_parser::init_actions(), java_cup::runtime::Symbol::parse_state, java_cup::runtime::lr_parser::production_tab, java_cup::runtime::lr_parser::production_table(), java_cup::runtime::lr_parser::reduce_tab, java_cup::runtime::lr_parser::reduce_table(), java_cup::runtime::lr_parser::scan(), java_cup::runtime::lr_parser::stack, java_cup::runtime::lr_parser::start_state(), java_cup::runtime::Symbol::sym, java_cup::runtime::lr_parser::syntax_error(), java_cup::runtime::lr_parser::tos, java_cup::runtime::lr_parser::unrecovered_syntax_error(), java_cup::runtime::Symbol::used_by_parser, and java_cup::runtime::lr_parser::user_init().

Referenced by java_cup::Main::parse_grammar_spec().

    {
      /* the current action code */
      int act;

      /* the Symbol/stack element returned by a reduce */
      Symbol lhs_sym = null;

      /* information about production being reduced with */
      short handle_size, lhs_sym_num;

      /* set up direct reference to tables to drive the parser */
      production_tab = production_table();
      action_tab     = action_table();
      reduce_tab     = reduce_table();

      debug_message("# Initializing parser");

      /* initialize the action encapsulation object */
      init_actions();

      /* do user initialization */
      user_init();

      /* the current Symbol */
      cur_token = scan(); 

      debug_message("# Current Symbol is #" + cur_token.sym);

      /* push dummy Symbol with start state to get us underway */
      stack.removeAllElements();
      stack.push(new Symbol(0, start_state()));
      tos = 0;

      /* continue until we are told to stop */
      for (_done_parsing = false; !_done_parsing; )
      {
        /* Check current token for freshness. */
        if (cur_token.used_by_parser)
          throw new Error("Symbol recycling detected (fix your scanner).");

        /* current state is always on the top of the stack */
        //debug_stack();

        /* look up action out of the current state with the current input */
        act = get_action(((Symbol)stack.peek()).parse_state, cur_token.sym);

        /* decode the action -- > 0 encodes shift */
        if (act > 0)
          {
            /* shift to the encoded state by pushing it on the stack */
            cur_token.parse_state = act-1;
            cur_token.used_by_parser = true;
            debug_shift(cur_token);
            stack.push(cur_token);
            tos++;

            /* advance to the next Symbol */
            cur_token = scan();
              debug_message("# Current token is " + cur_token);
          }
        /* if its less than zero, then it encodes a reduce action */
        else if (act < 0)
          {
            /* perform the action for the reduce */
            lhs_sym = do_action((-act)-1, this, stack, tos);

            /* look up information about the production */
            lhs_sym_num = production_tab[(-act)-1][0];
            handle_size = production_tab[(-act)-1][1];

            debug_reduce((-act)-1, lhs_sym_num, handle_size);

            /* pop the handle off the stack */
            for (int i = 0; i < handle_size; i++)
            {
              stack.pop();
              tos--;
            }
            
            /* look up the state to go to from the one popped back to */
            act = get_reduce(((Symbol)stack.peek()).parse_state, lhs_sym_num);
            debug_message("# Reduce rule: top state " +
                       ((Symbol)stack.peek()).parse_state +
                       ", lhs sym " + lhs_sym_num + " -> state " + act); 

            /* shift to that state */
            lhs_sym.parse_state = act;
            lhs_sym.used_by_parser = true;
            stack.push(lhs_sym);
            tos++;

            debug_message("# Goto state #" + act);
          }
        /* finally if the entry is zero, we have an error */
        else if (act == 0)
          {
            /* call user syntax error reporting routine */
            syntax_error(cur_token);

            /* try to error recover */
            if (!error_recovery(true))
            {
              /* if that fails give up with a fatal syntax error */
              unrecovered_syntax_error(cur_token);

              /* just in case that wasn't fatal enough, end parse */
              done_parsing();
            } else {
              lhs_sym = (Symbol)stack.peek();
            }
          }
      }
      return lhs_sym;
    }


Generated by  Doxygen 1.6.0   Back to index