Example #1
0
        /*-----------------------------------------------------------*/
        /*--- General Methods ---------------------------------------*/
        /*-----------------------------------------------------------*/

        /// <summary>Add a transition out of this state to another.
        /// </summary>
        /// <param name="on_sym">the symbol the transition is under.
        /// </param>
        /// <param name="to_st"> the state the transition goes to.
        ///
        /// </param>
        public virtual void  add_transition(symbol on_sym, lalr_state to_st)
        {
            lalr_transition trans;

            /* create a new transition object and put it in our list */
            trans        = new lalr_transition(on_sym, to_st, _transitions);
            _transitions = trans;
        }
Example #2
0
        /*-----------------------------------------------------------*/
        /*--- Constructor(s) ----------------------------------------*/
        /*-----------------------------------------------------------*/
        /// <summary>Simple constructor. 
        /// </summary>
        /// <param name="shft_to">the state that this action shifts to.
        /// 
        /// </param>
        public shift_action(lalr_state shft_to)
        {
            /* sanity check */
            if (shft_to == null)
                throw new internal_error("Attempt to create a shift_action to a null state");

            _shift_to = shft_to;
        }
Example #3
0
        /*-----------------------------------------------------------*/
        /*--- Constructor(s) ----------------------------------------*/
        /*-----------------------------------------------------------*/

        /// <summary>Simple constructor.
        /// </summary>
        /// <param name="shft_to">the state that this action shifts to.
        ///
        /// </param>
        public shift_action(lalr_state shft_to)
        {
            /* sanity check */
            if (shft_to == null)
            {
                throw new internal_error("Attempt to create a shift_action to a null state");
            }

            _shift_to = shft_to;
        }
Example #4
0
        /*-----------------------------------------------------------*/
        /*--- Constructor(s) ----------------------------------------*/
        /*-----------------------------------------------------------*/

        /// <summary>Simple constructor. Note: this should not be used until the number
        /// of terminals in the grammar has been established.
        /// </summary>
        public parse_reduce_row()
        {
            /* make sure the size is set */
            if (_size <= 0)
            {
                _size = non_terminal.number();
            }

            /* allocate the array */
            under_non_term = new lalr_state[size()];
        }
Example #5
0
        /*-----------------------------------------------------------*/
        /*--- Constructor(s) ----------------------------------------*/
        /*-----------------------------------------------------------*/
        /// <summary>Full constructor.
        /// </summary>
        /// <param name="on_sym"> symbol we are transitioning on.
        /// </param>
        /// <param name="to_st">  state we transition to.
        /// </param>
        /// <param name="nxt">    next transition in linked list.
        /// 
        /// </param>
        public lalr_transition(symbol on_sym, lalr_state to_st, lalr_transition nxt)
        {
            /* sanity checks */
            if (on_sym == null)
                throw new internal_error("Attempt to create transition on null symbol");
            if (to_st == null)
                throw new internal_error("Attempt to create transition to null state");

            /* initialize */
            _on_symbol = on_sym;
            _to_state = to_st;
            _next = nxt;
        }
Example #6
0
        /*-----------------------------------------------------------*/
        /*--- Static Methods ----------------------------------------*/
        /*-----------------------------------------------------------*/

        /// <summary>Helper routine for debugging -- produces a dump of the given state
        /// onto System.out.
        /// </summary>
        protected internal static void  dump_state(lalr_state st)
        {
            lalr_item_set   itms;
            lalr_item       itm;
            production_part part;

            if (st == null)
            {
                System.Console.Out.WriteLine("NULL lalr_state");
                return;
            }

            System.Console.Out.WriteLine("lalr_state [" + st.index() + "] {");
            itms = st.items();
            //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
            for (System.Collections.IEnumerator e = itms.all(); e.MoveNext();)
            {
                //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                itm = (lalr_item)e.Current;
                System.Console.Out.Write("  [");
                System.Console.Out.Write(itm.the_production().lhs().the_symbol().name_Renamed_Method());
                System.Console.Out.Write(" ::= ");
                for (int i = 0; i < itm.the_production().rhs_length(); i++)
                {
                    if (i == itm.dot_pos())
                    {
                        System.Console.Out.Write("(*) ");
                    }
                    part = itm.the_production().rhs(i);
                    if (part.is_action())
                    {
                        System.Console.Out.Write("{action} ");
                    }
                    else
                    {
                        System.Console.Out.Write(((symbol_part)part).the_symbol().name_Renamed_Method() + " ");
                    }
                }
                if (itm.dot_at_end())
                {
                    System.Console.Out.Write("(*) ");
                }
                System.Console.Out.WriteLine("]");
            }
            System.Console.Out.WriteLine("}");
        }
Example #7
0
        /*-----------------------------------------------------------*/
        /*--- Constructor(s) ----------------------------------------*/
        /*-----------------------------------------------------------*/

        /// <summary>Full constructor.
        /// </summary>
        /// <param name="on_sym"> symbol we are transitioning on.
        /// </param>
        /// <param name="to_st">  state we transition to.
        /// </param>
        /// <param name="nxt">    next transition in linked list.
        ///
        /// </param>
        public lalr_transition(symbol on_sym, lalr_state to_st, lalr_transition nxt)
        {
            /* sanity checks */
            if (on_sym == null)
            {
                throw new internal_error("Attempt to create transition on null symbol");
            }
            if (to_st == null)
            {
                throw new internal_error("Attempt to create transition to null state");
            }

            /* initialize */
            _on_symbol = on_sym;
            _to_state  = to_st;
            _next      = nxt;
        }
Example #8
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Produce a (semi-) human readable dump of the complete viable prefix
        /// recognition state machine.
        /// </summary>
        public static void  dump_machine()
        {
            lalr_state[] ordered = new lalr_state[lalr_state.number()];

            /* put the states in sorted order for a nicer display */
            //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
            for (System.Collections.IEnumerator s = lalr_state.all(); s.MoveNext();)
            {
                //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                lalr_state st = (lalr_state)s.Current;
                ordered[st.index()] = st;
            }

            System.Console.Error.WriteLine("===== Viable Prefix Recognizer =====");
            for (int i = 0; i < lalr_state.number(); i++)
            {
                if (ordered[i] == start_state)
                {
                    System.Console.Error.Write("START ");
                }
                System.Console.Error.WriteLine(ordered[i]);
                System.Console.Error.WriteLine("-------------------");
            }
        }
Example #9
0
        /*-----------------------------------------------------------*/
        /*--- Static Methods ----------------------------------------*/
        /*-----------------------------------------------------------*/
        /// <summary>Helper routine for debugging -- produces a dump of the given state
        /// onto System.out.
        /// </summary>
        protected internal static void dump_state(lalr_state st)
        {
            lalr_item_set itms;
            lalr_item itm;
            production_part part;

            if (st == null)
            {
                System.Console.Out.WriteLine("NULL lalr_state");
                return ;
            }

            System.Console.Out.WriteLine("lalr_state [" + st.index() + "] {");
            itms = st.items();
            //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
             for (System.Collections.IEnumerator e = itms.all(); e.MoveNext(); )
            {
                //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                itm = (lalr_item) e.Current;
                System.Console.Out.Write("  [");
                System.Console.Out.Write(itm.the_production().lhs().the_symbol().name_Renamed_Method());
                System.Console.Out.Write(" ::= ");
                 for (int i = 0; i < itm.the_production().rhs_length(); i++)
                {
                    if (i == itm.dot_pos())
                        System.Console.Out.Write("(*) ");
                    part = itm.the_production().rhs(i);
                    if (part.is_action())
                        System.Console.Out.Write("{action} ");
                    else
                        System.Console.Out.Write(((symbol_part) part).the_symbol().name_Renamed_Method() + " ");
                }
                if (itm.dot_at_end())
                    System.Console.Out.Write("(*) ");
                System.Console.Out.WriteLine("]");
            }
            System.Console.Out.WriteLine("}");
        }
Example #10
0
 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 /// <summary>Equality comparison. 
 /// </summary>
 public virtual bool equals(lalr_state other)
 {
     /* we are equal if our item sets are equal */
     return other != null && items().equals(other.items());
 }
Example #11
0
        /*-----------------------------------------------------------*/
        /*--- General Methods ---------------------------------------*/
        /*-----------------------------------------------------------*/
        /// <summary>Add a transition out of this state to another.
        /// </summary>
        /// <param name="on_sym">the symbol the transition is under.
        /// </param>
        /// <param name="to_st"> the state the transition goes to.
        /// 
        /// </param>
        public virtual void add_transition(symbol on_sym, lalr_state to_st)
        {
            lalr_transition trans;

            /* create a new transition object and put it in our list */
            trans = new lalr_transition(on_sym, to_st, _transitions);
            _transitions = trans;
        }
Example #12
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
        /// <summary>Build an LALR viable prefix recognition machine given a start 
        /// production.  This method operates by first building a start state
        /// from the start production (based on a single item with the dot at
        /// the beginning and EOF as expected lookahead).  Then for each state
        /// it attempts to extend the machine by creating transitions out of
        /// the state to new or existing states.  When considering extension
        /// from a state we make a transition on each symbol that appears before
        /// the dot in some item.  For example, if we have the items: <pre>
        /// [A ::= a b * X c, {d,e}]
        /// [B ::= a b * X d, {a,b}]
        /// </pre>
        /// in some state, then we would be making a transition under X to a new
        /// state.  This new state would be formed by a "kernel" of items 
        /// corresponding to moving the dot past the X.  In this case: <pre>
        /// [A ::= a b X * c, {d,e}]
        /// [B ::= a b X * Y, {a,b}]
        /// </pre>
        /// The full state would then be formed by "closing" this kernel set of 
        /// items so that it included items that represented productions of things
        /// the parser was now looking for.  In this case we would items 
        /// corresponding to productions of Y, since various forms of Y are expected
        /// next when in this state (see lalr_item_set.compute_closure() for details 
        /// on closure). <p>
        /// *
        /// The process of building the viable prefix recognizer terminates when no
        /// new states can be added.  However, in order to build a smaller number of
        /// states (i.e., corresponding to LALR rather than canonical LR) the state 
        /// building process does not maintain full loookaheads in all items.  
        /// Consequently, after the machine is built, we go back and propagate 
        /// lookaheads through the constructed machine using a call to 
        /// propagate_all_lookaheads().  This makes use of propagation links 
        /// constructed during the closure and transition process.
        /// *
        /// </summary>
        /// <param name="start_prod">the start production of the grammar
        /// </param>
        /// <seealso cref="   CUP.lalr_item_set#compute_closure
        /// "/>
        /// <seealso cref="   CUP.lalr_state#propagate_all_lookaheads
        /// 
        /// "/>
        public static lalr_state build_machine(production start_prod)
        {
            lalr_state start_state;
            lalr_item_set start_items;
            lalr_item_set new_items;
            lalr_item_set linked_items;
            lalr_item_set kernel;
            CUP.runtime.SymbolStack work_stack = new CUP.runtime.SymbolStack();
            lalr_state st, new_st;
            symbol_set outgoing;
            lalr_item itm, new_itm, existing, fix_itm;
            symbol sym, sym2;
            System.Collections.IEnumerator i, s, fix;

            /* sanity check */
            if (start_prod == null)
                throw new internal_error("Attempt to build viable prefix recognizer using a null production");

            /* build item with dot at front of start production and EOF lookahead */
            start_items = new lalr_item_set();

            itm = new lalr_item(start_prod);
            itm.lookahead().add(terminal.EOF);

            start_items.add(itm);

            /* create copy the item set to form the kernel */
            kernel = new lalr_item_set(start_items);

            /* create the closure from that item set */
            start_items.compute_closure();

            /* build a state out of that item set and put it in our work set */
            start_state = new lalr_state(start_items);
            System.Object temp_object;
            temp_object = start_state;
            System.Object generatedAux = temp_object;
            work_stack.Push(temp_object);

            /* enter the state using the kernel as the key */
            SupportClass.PutElement(_all_kernels, kernel, start_state);

            /* continue looking at new states until we have no more work to do */
            while (!(work_stack.Count == 0))
            {
                /* remove a state from the work set */
                st = (lalr_state) work_stack.Pop();

                /* gather up all the symbols that appear before dots */
                outgoing = new symbol_set();
                //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
                 for (i = st.items().all(); i.MoveNext(); )
                {
                    //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                    itm = (lalr_item) i.Current;

                    /* add the symbol before the dot (if any) to our collection */
                    sym = itm.symbol_after_dot();
                    if (sym != null)
                        outgoing.add(sym);
                }

                /* now create a transition out for each individual symbol */
                //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
                 for (s = outgoing.all(); s.MoveNext(); )
                {
                    //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                    sym = (symbol) s.Current;

                    /* will be keeping the set of items with propagate links */
                    linked_items = new lalr_item_set();

                    /* gather up shifted versions of all the items that have this
                    symbol before the dot */
                    new_items = new lalr_item_set();
                    //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
                     for (i = st.items().all(); i.MoveNext(); )
                    {
                        //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                        itm = (lalr_item) i.Current;

                        /* if this is the symbol we are working on now, add to set */
                        sym2 = itm.symbol_after_dot();
                        if (sym.Equals(sym2))
                        {
                            /* add to the kernel of the new state */
                            new_items.add(itm.shift());

                            /* remember that itm has propagate link to it */
                            linked_items.add(itm);
                        }
                    }

                    /* use new items as state kernel */
                    kernel = new lalr_item_set(new_items);

                    /* have we seen this one already? */
                    new_st = (lalr_state) _all_kernels[kernel];

                    /* if we haven't, build a new state out of the item set */
                    if (new_st == null)
                    {
                        /* compute closure of the kernel for the full item set */
                        new_items.compute_closure();

                        /* build the new state */
                        new_st = new lalr_state(new_items);

                        /* add the new state to our work set */
                        System.Object temp_object2;
                        temp_object2 = new_st;
                        System.Object generatedAux2 = temp_object2;
                        work_stack.Push(temp_object2);

                        /* put it in our kernel table */
                        SupportClass.PutElement(_all_kernels, kernel, new_st);
                    }
                    else
                    {
                        /* walk through the items that have links to the new state */
                        //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
                         for (fix = linked_items.all(); fix.MoveNext(); )
                        {
                            //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                            fix_itm = (lalr_item) fix.Current;

                            /* look at each propagate link out of that item */
                             for (int l = 0; l < fix_itm.propagate_items().Count; l++)
                            {
                                /* pull out item linked to in the new state */
                                new_itm = (lalr_item) (fix_itm.propagate_items().Peek(fix_itm.propagate_items().Count - (l + 1)));

                                /* find corresponding item in the existing state */
                                existing = new_st.items().find(new_itm);

                                /* fix up the item so it points to the existing set */
                                if (existing != null)
                                    fix_itm.propagate_items()[l] = existing;
                            }
                        }
                    }

                    /* add a transition from current state to that state */
                    st.add_transition(sym, new_st);
                }
            }

            /* all done building states */

            /* propagate complete lookahead sets throughout the states */
            propagate_all_lookaheads();

            return start_state;
        }
Example #13
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
        /// <summary>Build the (internal) parser from the previously parsed specification.
        /// This includes:<ul>
        /// <li> Computing nullability of non-terminals.
        /// <li> Computing first sets of non-terminals and productions.
        /// <li> Building the viable prefix recognizer machine.
        /// <li> Filling in the (internal) parse tables.
        /// <li> Checking for unreduced productions.
        /// </ul>
        /// </summary>
        protected internal static void build_parser()
        {
            /* compute nullability of all non terminals */
            if (opt_do_debug || print_progress)
                System.Console.Error.WriteLine("  Computing non-terminal nullability...");
            non_terminal.compute_nullability();

            nullability_end = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* compute first sets of all non terminals */
            if (opt_do_debug || print_progress)
                System.Console.Error.WriteLine("  Computing first sets...");
            non_terminal.compute_first_sets();

            first_end = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* build the LR viable prefix recognition machine */
            if (opt_do_debug || print_progress)
                System.Console.Error.WriteLine("  Building state machine...");
            start_state = lalr_state.build_machine(emit.start_production);

            machine_end = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* build the LR parser action and reduce-goto tables */
            if (opt_do_debug || print_progress)
                System.Console.Error.WriteLine("  Filling in tables...");
            action_table = new parse_action_table();
            reduce_table = new parse_reduce_table();
             for (System.Collections.IEnumerator st = lalr_state.all(); st.MoveNext(); )
            {
                lalr_state lst = (lalr_state) st.Current;
                lst.build_table_entries(action_table, reduce_table);
            }

            table_end = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* check and warn for non-reduced productions */
            if (opt_do_debug || print_progress)
                System.Console.Error.WriteLine("  Checking for non-reduced productions...");
            action_table.check_reductions();

            reduce_check_end = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* if we have more conflicts than we expected issue a message and die */
            if (emit.num_conflicts > expect_conflicts)
            {
                System.Console.Error.WriteLine("*** More conflicts encountered than expected " + "-- parser generation aborted");
                lexer.error_count++; // indicate the problem.
                // we'll die on return, after clean up.
            }
        }
Example #14
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
        /// <summary>Produce a (semi-) human readable dump of the complete viable prefix 
        /// recognition state machine. 
        /// </summary>
        public static void dump_machine()
        {
            lalr_state[] ordered = new lalr_state[lalr_state.number()];

            /* put the states in sorted order for a nicer display */
            //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
             for (System.Collections.IEnumerator s = lalr_state.all(); s.MoveNext(); )
            {
                //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                lalr_state st = (lalr_state) s.Current;
                ordered[st.index()] = st;
            }

            System.Console.Error.WriteLine("===== Viable Prefix Recognizer =====");
             for (int i = 0; i < lalr_state.number(); i++)
            {
                if (ordered[i] == start_state)
                    System.Console.Error.Write("START ");
                System.Console.Error.WriteLine(ordered[i]);
                System.Console.Error.WriteLine("-------------------");
            }
        }
Example #15
0
 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 /// <summary>Constructor with null next. 
 /// </summary>
 /// <param name="on_sym"> symbol we are transitioning on.
 /// </param>
 /// <param name="to_st">  state we transition to.
 /// 
 /// </param>
 public lalr_transition(symbol on_sym, lalr_state to_st)
     : this(on_sym, to_st, null)
 {
 }
Example #16
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Build the (internal) parser from the previously parsed specification.
        /// This includes:<ul>
        /// <li> Computing nullability of non-terminals.
        /// <li> Computing first sets of non-terminals and productions.
        /// <li> Building the viable prefix recognizer machine.
        /// <li> Filling in the (internal) parse tables.
        /// <li> Checking for unreduced productions.
        /// </ul>
        /// </summary>
        protected internal static void  build_parser()
        {
            /* compute nullability of all non terminals */
            if (opt_do_debug || print_progress)
            {
                System.Console.Error.WriteLine("  Computing non-terminal nullability...");
            }
            non_terminal.compute_nullability();

            nullability_end = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* compute first sets of all non terminals */
            if (opt_do_debug || print_progress)
            {
                System.Console.Error.WriteLine("  Computing first sets...");
            }
            non_terminal.compute_first_sets();

            first_end = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* build the LR viable prefix recognition machine */
            if (opt_do_debug || print_progress)
            {
                System.Console.Error.WriteLine("  Building state machine...");
            }
            start_state = lalr_state.build_machine(emit.start_production);

            machine_end = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* build the LR parser action and reduce-goto tables */
            if (opt_do_debug || print_progress)
            {
                System.Console.Error.WriteLine("  Filling in tables...");
            }
            action_table = new parse_action_table();
            reduce_table = new parse_reduce_table();
            for (System.Collections.IEnumerator st = lalr_state.all(); st.MoveNext();)
            {
                lalr_state lst = (lalr_state)st.Current;
                lst.build_table_entries(action_table, reduce_table);
            }

            table_end = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* check and warn for non-reduced productions */
            if (opt_do_debug || print_progress)
            {
                System.Console.Error.WriteLine("  Checking for non-reduced productions...");
            }
            action_table.check_reductions();

            reduce_check_end = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* if we have more conflicts than we expected issue a message and die */
            if (emit.num_conflicts > expect_conflicts)
            {
                System.Console.Error.WriteLine("*** More conflicts encountered than expected " + "-- parser generation aborted");
                lexer.error_count++;                 // indicate the problem.
                // we'll die on return, after clean up.
            }
        }
Example #17
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Equality comparison.
        /// </summary>
        public virtual bool equals(lalr_state other)
        {
            /* we are equal if our item sets are equal */
            return(other != null && items().equals(other.items()));
        }
Example #18
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Build an LALR viable prefix recognition machine given a start
        /// production.  This method operates by first building a start state
        /// from the start production (based on a single item with the dot at
        /// the beginning and EOF as expected lookahead).  Then for each state
        /// it attempts to extend the machine by creating transitions out of
        /// the state to new or existing states.  When considering extension
        /// from a state we make a transition on each symbol that appears before
        /// the dot in some item.  For example, if we have the items: <pre>
        /// [A ::= a b * X c, {d,e}]
        /// [B ::= a b * X d, {a,b}]
        /// </pre>
        /// in some state, then we would be making a transition under X to a new
        /// state.  This new state would be formed by a "kernel" of items
        /// corresponding to moving the dot past the X.  In this case: <pre>
        /// [A ::= a b X * c, {d,e}]
        /// [B ::= a b X * Y, {a,b}]
        /// </pre>
        /// The full state would then be formed by "closing" this kernel set of
        /// items so that it included items that represented productions of things
        /// the parser was now looking for.  In this case we would items
        /// corresponding to productions of Y, since various forms of Y are expected
        /// next when in this state (see lalr_item_set.compute_closure() for details
        /// on closure). <p>
        /// *
        /// The process of building the viable prefix recognizer terminates when no
        /// new states can be added.  However, in order to build a smaller number of
        /// states (i.e., corresponding to LALR rather than canonical LR) the state
        /// building process does not maintain full loookaheads in all items.
        /// Consequently, after the machine is built, we go back and propagate
        /// lookaheads through the constructed machine using a call to
        /// propagate_all_lookaheads().  This makes use of propagation links
        /// constructed during the closure and transition process.
        /// *
        /// </summary>
        /// <param name="start_prod">the start production of the grammar
        /// </param>
        /// <seealso cref="   CUP.lalr_item_set#compute_closure
        /// "/>
        /// <seealso cref="   CUP.lalr_state#propagate_all_lookaheads
        ///
        /// "/>

        public static lalr_state build_machine(production start_prod)
        {
            lalr_state    start_state;
            lalr_item_set start_items;
            lalr_item_set new_items;
            lalr_item_set linked_items;
            lalr_item_set kernel;

            CUP.runtime.SymbolStack work_stack = new CUP.runtime.SymbolStack();
            lalr_state st, new_st;
            symbol_set outgoing;
            lalr_item  itm, new_itm, existing, fix_itm;
            symbol     sym, sym2;

            System.Collections.IEnumerator i, s, fix;

            /* sanity check */
            if (start_prod == null)
            {
                throw new internal_error("Attempt to build viable prefix recognizer using a null production");
            }

            /* build item with dot at front of start production and EOF lookahead */
            start_items = new lalr_item_set();

            itm = new lalr_item(start_prod);
            itm.lookahead().add(terminal.EOF);

            start_items.add(itm);

            /* create copy the item set to form the kernel */
            kernel = new lalr_item_set(start_items);

            /* create the closure from that item set */
            start_items.compute_closure();

            /* build a state out of that item set and put it in our work set */
            start_state = new lalr_state(start_items);
            System.Object temp_object;
            temp_object = start_state;
            System.Object generatedAux = temp_object;
            work_stack.Push(temp_object);

            /* enter the state using the kernel as the key */
            SupportClass.PutElement(_all_kernels, kernel, start_state);

            /* continue looking at new states until we have no more work to do */
            while (!(work_stack.Count == 0))
            {
                /* remove a state from the work set */
                st = (lalr_state)work_stack.Pop();

                /* gather up all the symbols that appear before dots */
                outgoing = new symbol_set();
                //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
                for (i = st.items().all(); i.MoveNext();)
                {
                    //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                    itm = (lalr_item)i.Current;

                    /* add the symbol before the dot (if any) to our collection */
                    sym = itm.symbol_after_dot();
                    if (sym != null)
                    {
                        outgoing.add(sym);
                    }
                }

                /* now create a transition out for each individual symbol */
                //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
                for (s = outgoing.all(); s.MoveNext();)
                {
                    //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                    sym = (symbol)s.Current;

                    /* will be keeping the set of items with propagate links */
                    linked_items = new lalr_item_set();

                    /* gather up shifted versions of all the items that have this
                     * symbol before the dot */
                    new_items = new lalr_item_set();
                    //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
                    for (i = st.items().all(); i.MoveNext();)
                    {
                        //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                        itm = (lalr_item)i.Current;

                        /* if this is the symbol we are working on now, add to set */
                        sym2 = itm.symbol_after_dot();
                        if (sym.Equals(sym2))
                        {
                            /* add to the kernel of the new state */
                            new_items.add(itm.shift());

                            /* remember that itm has propagate link to it */
                            linked_items.add(itm);
                        }
                    }

                    /* use new items as state kernel */
                    kernel = new lalr_item_set(new_items);

                    /* have we seen this one already? */
                    new_st = (lalr_state)_all_kernels[kernel];

                    /* if we haven't, build a new state out of the item set */
                    if (new_st == null)
                    {
                        /* compute closure of the kernel for the full item set */
                        new_items.compute_closure();

                        /* build the new state */
                        new_st = new lalr_state(new_items);

                        /* add the new state to our work set */
                        System.Object temp_object2;
                        temp_object2 = new_st;
                        System.Object generatedAux2 = temp_object2;
                        work_stack.Push(temp_object2);

                        /* put it in our kernel table */
                        SupportClass.PutElement(_all_kernels, kernel, new_st);
                    }
                    else
                    {
                        /* walk through the items that have links to the new state */
                        //UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
                        for (fix = linked_items.all(); fix.MoveNext();)
                        {
                            //UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
                            fix_itm = (lalr_item)fix.Current;

                            /* look at each propagate link out of that item */
                            for (int l = 0; l < fix_itm.propagate_items().Count; l++)
                            {
                                /* pull out item linked to in the new state */
                                new_itm = (lalr_item)(fix_itm.propagate_items().Peek(fix_itm.propagate_items().Count - (l + 1)));

                                /* find corresponding item in the existing state */
                                existing = new_st.items().find(new_itm);

                                /* fix up the item so it points to the existing set */
                                if (existing != null)
                                {
                                    fix_itm.propagate_items()[l] = existing;
                                }
                            }
                        }
                    }

                    /* add a transition from current state to that state */
                    st.add_transition(sym, new_st);
                }
            }

            /* all done building states */

            /* propagate complete lookahead sets throughout the states */
            propagate_all_lookaheads();

            return(start_state);
        }
Example #19
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Constructor with null next.
        /// </summary>
        /// <param name="on_sym"> symbol we are transitioning on.
        /// </param>
        /// <param name="to_st">  state we transition to.
        ///
        /// </param>
        public lalr_transition(symbol on_sym, lalr_state to_st) : this(on_sym, to_st, null)
        {
        }