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

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

This method provides the main parsing routine. It returns only when done_parsing() has been called (typically because the parser has accepted, or a fatal error has been reported). See the header documentation for the class regarding how shift/reduce parsers operate and how the various tables are used.

Definition at line 509 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::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(), and jasmin::ClassFile::readJasmin().

    {
      /* 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();

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

      /* do user initialization */
      user_init();

      /* get the first token */
      cur_token = scan(); 

      /* 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 */

        /* 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;
            stack.push(cur_token);
            tos++;

            /* advance to the next Symbol */
            cur_token = scan();
          }
        /* 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];

            /* 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);

            /* shift to that state */
            lhs_sym.parse_state = act;
            lhs_sym.used_by_parser = true;
            stack.push(lhs_sym);
            tos++;
          }
        /* 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(false))
            {
              /* 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