Example #1
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Equality comparison -- here we only require the cores to be equal since
        /// we need to do sets of items based only on core equality (ignoring
        /// lookahead sets).
        /// </summary>
        public virtual bool equals(lalr_item other)
        {
            if (other == null)
            {
                return(false);
            }
            return(base.Equals(other));
        }
Example #2
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Add a new item to the set of items we propagate to.
        /// </summary>
        public virtual void  add_propagate(lalr_item prop_to)
        {
            System.Object temp_object;
            temp_object = prop_to;
            System.Object generatedAux = temp_object;
            _propagate_items.Push(temp_object);
            needs_propagation = true;
        }
Example #3
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Remove a single item if it is in the set.
        /// </summary>
        /// <param name="itm">the item to remove.
        ///
        /// </param>
        public virtual void  remove(lalr_item itm)
        {
            not_null(itm);

            /* invalidate cached hashcode */
            hashcode_cache = int.MinValue;

            /* remove it from hash table implementing set */
            _all.Remove(itm);
        }
Example #4
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Produce the new lalr_item that results from shifting the dot one position
        /// to the right.
        /// </summary>
        public virtual lalr_item shift()
        {
            lalr_item result;

            /* can't shift if we have dot already at the end */
            if (dot_at_end())
            {
                throw new internal_error("Attempt to shift past end of an lalr_item");
            }

            /* create the new item w/ the dot shifted by one */
            result = new lalr_item(the_production(), dot_pos() + 1, new terminal_set(lookahead()));

            /* change in our lookahead needs to be propagated to this item */
            add_propagate(result);

            return(result);
        }
Example #5
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Produce a warning message for one reduce/reduce conflict.
        /// *
        /// </summary>
        /// <param name="itm1">first item in conflict.
        /// </param>
        /// <param name="itm2">second item in conflict.
        ///
        /// </param>
        protected internal virtual void  report_reduce_reduce(lalr_item itm1, lalr_item itm2)
        {
            bool comma_flag = false;

            System.Console.Error.WriteLine("*** Reduce/Reduce conflict found in state #" + index());
            System.Console.Error.Write("  between ");
            System.Console.Error.WriteLine(itm1.to_simple_string());
            System.Console.Error.Write("  and     ");
            System.Console.Error.WriteLine(itm2.to_simple_string());
            System.Console.Error.Write("  under symbols: {");
            for (int t = 0; t < terminal.number(); t++)
            {
                if (itm1.lookahead().contains(t) && itm2.lookahead().contains(t))
                {
                    if (comma_flag)
                    {
                        System.Console.Error.Write(", ");
                    }
                    else
                    {
                        comma_flag = true;
                    }
                    System.Console.Error.Write(terminal.find(t).name_Renamed_Method());
                }
            }
            System.Console.Error.WriteLine("}");
            System.Console.Error.Write("  Resolved in favor of ");
            if (itm1.the_production().index() < itm2.the_production().index())
            {
                System.Console.Error.WriteLine("the first production.\n");
            }
            else
            {
                System.Console.Error.WriteLine("the second production.\n");
            }

            /* count the conflict */
            emit.num_conflicts++;
            lexer.warning_count++;
        }
Example #6
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Produce a warning message for one shift/reduce conflict.
        /// *
        /// </summary>
        /// <param name="red_itm">     the item with the reduce.
        /// </param>
        /// <param name="conflict_sym">the index of the symbol conflict occurs under.
        ///
        /// </param>
        protected internal virtual void  report_shift_reduce(lalr_item red_itm, int conflict_sym)
        {
            lalr_item itm;
            symbol    shift_sym;

            /* emit top part of message including the reduce item */
            System.Console.Error.WriteLine("*** Shift/Reduce conflict found in state #" + index());
            System.Console.Error.Write("  between ");
            System.Console.Error.WriteLine(red_itm.to_simple_string());

            /* find and report on all items that shift under our conflict 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 (System.Collections.IEnumerator itms = items().all(); itms.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)itms.Current;

                /* only look if its not the same item and not a reduce */
                if (itm != red_itm && !itm.dot_at_end())
                {
                    /* is it a shift on our conflicting terminal */
                    shift_sym = itm.symbol_after_dot();
                    if (!shift_sym.is_non_term() && shift_sym.index() == conflict_sym)
                    {
                        /* yes, report on it */
                        System.Console.Error.WriteLine("  and     " + itm.to_simple_string());
                    }
                }
            }
            System.Console.Error.WriteLine("  under symbol " + terminal.find(conflict_sym).name_Renamed_Method());
            System.Console.Error.WriteLine("  Resolved in favor of shifting.\n");

            /* count the conflict */
            emit.num_conflicts++;
            lexer.warning_count++;
        }
Example #7
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
        /// <summary>Add a singleton item, merging lookahead sets if the item is already 
        /// part of the set.  returns the element of the set that was added or 
        /// merged into.
        /// </summary>
        /// <param name="itm">the item being added.
        /// 
        /// </param>
        public virtual lalr_item add(lalr_item itm)
        {
            lalr_item other;

            not_null(itm);

            /* see if an item with a matching core is already there */
            other = (lalr_item) _all[itm];

            /* if so, merge this lookahead into the original and leave it */
            if (other != null)
            {
                other.lookahead().add(itm.lookahead());
                return other;
            }
            else
            {
                /* invalidate cached hashcode */
                hashcode_cache = int.MinValue;

                SupportClass.PutElement(_all, itm, itm);
                return itm;
            }
        }
Example #8
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Add a singleton item, merging lookahead sets if the item is already
        /// part of the set.  returns the element of the set that was added or
        /// merged into.
        /// </summary>
        /// <param name="itm">the item being added.
        ///
        /// </param>
        public virtual lalr_item add(lalr_item itm)
        {
            lalr_item other;

            not_null(itm);

            /* see if an item with a matching core is already there */
            other = (lalr_item)_all[itm];

            /* if so, merge this lookahead into the original and leave it */
            if (other != null)
            {
                other.lookahead().add(itm.lookahead());
                return(other);
            }
            else
            {
                /* invalidate cached hashcode */
                hashcode_cache = int.MinValue;

                SupportClass.PutElement(_all, itm, itm);
                return(itm);
            }
        }
Example #9
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 #10
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
        /// <summary>Remove a single item if it is in the set. 
        /// </summary>
        /// <param name="itm">the item to remove.
        /// 
        /// </param>
        public virtual void remove(lalr_item itm)
        {
            not_null(itm);

            /* invalidate cached hashcode */
            hashcode_cache = int.MinValue;

            /* remove it from hash table implementing set */
            _all.Remove(itm);
        }
Example #11
0
 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 /// <summary>Return the item in the set matching a particular item (or null if not 
 /// found) 
 /// </summary>
 /// <param name="itm">the item we are looking for.
 /// 
 /// </param>
 public virtual lalr_item find(lalr_item itm)
 {
     return (lalr_item) _all[itm];
 }
Example #12
0
 /*-----------------------------------------------------------*/
 /*--- Set Operation Methods ---------------------------------*/
 /*-----------------------------------------------------------*/
 /// <summary>Does the set contain a particular item? 
 /// </summary>
 /// <param name="itm">the item in question.
 /// 
 /// </param>
 public virtual bool contains(lalr_item itm)
 {
     return _all.ContainsKey(itm);
 }
Example #13
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
        /// <summary>Compute the closure of the set using the LALR closure rules.  Basically
        /// for every item of the form: <pre>
        /// [L ::= a *N alpha, l] 
        /// </pre>
        /// (where N is a a non terminal and alpha is a string of symbols) make 
        /// sure there are also items of the form:  <pre>
        /// [N ::= *beta, first(alpha l)] 
        /// </pre>
        /// corresponding to each production of N.  Items with identical cores but 
        /// differing lookahead sets are merged by creating a new item with the same 
        /// core and the union of the lookahead sets (the LA in LALR stands for 
        /// "lookahead merged" and this is where the merger is).  This routine 
        /// assumes that nullability and first sets have been computed for all 
        /// productions before it is called.
        /// </summary>
        public virtual void compute_closure()
        {
            lalr_item_set consider;
            lalr_item itm, new_itm, add_itm;
            non_terminal nt;
            terminal_set new_lookaheads;
            System.Collections.IEnumerator p;
            production prod;
            bool need_prop;

            /* invalidate cached hashcode */
            hashcode_cache = int.MinValue;

            /* each current element needs to be considered */
            consider = new lalr_item_set(this);

            /* repeat this until there is nothing else to consider */
            while (consider.size() > 0)
            {
                /* get one item to consider */
                itm = consider.get_one();

                /* do we have a dot before a non terminal */
                nt = itm.dot_before_nt();
                if (nt != null)
                {
                    /* create the lookahead set based on first after dot */
                    new_lookaheads = itm.calc_lookahead(itm.lookahead());

                    /* are we going to need to propagate our lookahead to new item */
                    need_prop = itm.lookahead_visible();

                    /* create items for each production of that non term */
                    //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 (p = nt.productions(); p.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"'
                        prod = (production) p.Current;

                        /* create new item with dot at start and that lookahead */
                        new_itm = new lalr_item(prod, new terminal_set(new_lookaheads));

                        /* add/merge item into the set */
                        add_itm = add(new_itm);
                        /* if propagation is needed link to that item */
                        if (need_prop)
                            itm.add_propagate(add_itm);

                        /* was this was a new item*/
                        if (add_itm == new_itm)
                        {
                            /* that may need further closure, consider it also */
                            consider.add(new_itm);
                        }
                    }
                }
            }
        }
Example #14
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
        /// <summary>Produce the new lalr_item that results from shifting the dot one position
        /// to the right. 
        /// </summary>
        public virtual lalr_item shift()
        {
            lalr_item result;

            /* can't shift if we have dot already at the end */
            if (dot_at_end())
                throw new internal_error("Attempt to shift past end of an lalr_item");

            /* create the new item w/ the dot shifted by one */
            result = new lalr_item(the_production(), dot_pos() + 1, new terminal_set(lookahead()));

            /* change in our lookahead needs to be propagated to this item */
            add_propagate(result);

            return result;
        }
Example #15
0
 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 /// <summary>Equality comparison -- here we only require the cores to be equal since
 /// we need to do sets of items based only on core equality (ignoring 
 /// lookahead sets). 
 /// </summary>
 public virtual bool equals(lalr_item other)
 {
     if (other == null)
         return false;
     return base.Equals(other);
 }
Example #16
0
 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 /// <summary>Add a new item to the set of items we propagate to. 
 /// </summary>
 public virtual void add_propagate(lalr_item prop_to)
 {
     System.Object temp_object;
     temp_object = prop_to;
     System.Object generatedAux = temp_object;
     _propagate_items.Push(temp_object);
     needs_propagation = true;
 }
Example #17
0
        /*-----------------------------------------------------------*/
        /*--- Set Operation Methods ---------------------------------*/
        /*-----------------------------------------------------------*/

        /// <summary>Does the set contain a particular item?
        /// </summary>
        /// <param name="itm">the item in question.
        ///
        /// </param>
        public virtual bool contains(lalr_item itm)
        {
            return(_all.ContainsKey(itm));
        }
Example #18
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Compute the closure of the set using the LALR closure rules.  Basically
        /// for every item of the form: <pre>
        /// [L ::= a *N alpha, l]
        /// </pre>
        /// (where N is a a non terminal and alpha is a string of symbols) make
        /// sure there are also items of the form:  <pre>
        /// [N ::= *beta, first(alpha l)]
        /// </pre>
        /// corresponding to each production of N.  Items with identical cores but
        /// differing lookahead sets are merged by creating a new item with the same
        /// core and the union of the lookahead sets (the LA in LALR stands for
        /// "lookahead merged" and this is where the merger is).  This routine
        /// assumes that nullability and first sets have been computed for all
        /// productions before it is called.
        /// </summary>
        public virtual void  compute_closure()
        {
            lalr_item_set consider;
            lalr_item     itm, new_itm, add_itm;
            non_terminal  nt;
            terminal_set  new_lookaheads;

            System.Collections.IEnumerator p;
            production prod;
            bool       need_prop;



            /* invalidate cached hashcode */
            hashcode_cache = int.MinValue;

            /* each current element needs to be considered */
            consider = new lalr_item_set(this);

            /* repeat this until there is nothing else to consider */
            while (consider.size() > 0)
            {
                /* get one item to consider */
                itm = consider.get_one();

                /* do we have a dot before a non terminal */
                nt = itm.dot_before_nt();
                if (nt != null)
                {
                    /* create the lookahead set based on first after dot */
                    new_lookaheads = itm.calc_lookahead(itm.lookahead());

                    /* are we going to need to propagate our lookahead to new item */
                    need_prop = itm.lookahead_visible();

                    /* create items for each production of that non term */
                    //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 (p = nt.productions(); p.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"'
                        prod = (production)p.Current;

                        /* create new item with dot at start and that lookahead */
                        new_itm = new lalr_item(prod, new terminal_set(new_lookaheads));

                        /* add/merge item into the set */
                        add_itm = add(new_itm);
                        /* if propagation is needed link to that item */
                        if (need_prop)
                        {
                            itm.add_propagate(add_itm);
                        }

                        /* was this was a new item*/
                        if (add_itm == new_itm)
                        {
                            /* that may need further closure, consider it also */
                            consider.add(new_itm);
                        }
                    }
                }
            }
        }
Example #19
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 #20
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <summary>Return the item in the set matching a particular item (or null if not
        /// found)
        /// </summary>
        /// <param name="itm">the item we are looking for.
        ///
        /// </param>
        public virtual lalr_item find(lalr_item itm)
        {
            return((lalr_item)_all[itm]);
        }
Example #21
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
        /// <summary>Produce a warning message for one shift/reduce conflict.
        /// *
        /// </summary>
        /// <param name="red_itm">     the item with the reduce.
        /// </param>
        /// <param name="conflict_sym">the index of the symbol conflict occurs under.
        /// 
        /// </param>
        protected internal virtual void report_shift_reduce(lalr_item red_itm, int conflict_sym)
        {
            lalr_item itm;
            symbol shift_sym;

            /* emit top part of message including the reduce item */
            System.Console.Error.WriteLine("*** Shift/Reduce conflict found in state #" + index());
            System.Console.Error.Write("  between ");
            System.Console.Error.WriteLine(red_itm.to_simple_string());

            /* find and report on all items that shift under our conflict 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 (System.Collections.IEnumerator itms = items().all(); itms.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) itms.Current;

                /* only look if its not the same item and not a reduce */
                if (itm != red_itm && !itm.dot_at_end())
                {
                    /* is it a shift on our conflicting terminal */
                    shift_sym = itm.symbol_after_dot();
                    if (!shift_sym.is_non_term() && shift_sym.index() == conflict_sym)
                    {
                        /* yes, report on it */
                        System.Console.Error.WriteLine("  and     " + itm.to_simple_string());
                    }
                }
            }
            System.Console.Error.WriteLine("  under symbol " + terminal.find(conflict_sym).name_Renamed_Method());
            System.Console.Error.WriteLine("  Resolved in favor of shifting.\n");

            /* count the conflict */
            emit.num_conflicts++;
            lexer.warning_count++;
        }
Example #22
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
        /// <summary>Produce a warning message for one reduce/reduce conflict. 
        /// *
        /// </summary>
        /// <param name="itm1">first item in conflict.
        /// </param>
        /// <param name="itm2">second item in conflict.
        /// 
        /// </param>
        protected internal virtual void report_reduce_reduce(lalr_item itm1, lalr_item itm2)
        {
            bool comma_flag = false;

            System.Console.Error.WriteLine("*** Reduce/Reduce conflict found in state #" + index());
            System.Console.Error.Write("  between ");
            System.Console.Error.WriteLine(itm1.to_simple_string());
            System.Console.Error.Write("  and     ");
            System.Console.Error.WriteLine(itm2.to_simple_string());
            System.Console.Error.Write("  under symbols: {");
             for (int t = 0; t < terminal.number(); t++)
            {
                if (itm1.lookahead().contains(t) && itm2.lookahead().contains(t))
                {
                    if (comma_flag)
                        System.Console.Error.Write(", ");
                    else
                        comma_flag = true;
                    System.Console.Error.Write(terminal.find(t).name_Renamed_Method());
                }
            }
            System.Console.Error.WriteLine("}");
            System.Console.Error.Write("  Resolved in favor of ");
            if (itm1.the_production().index() < itm2.the_production().index())
                System.Console.Error.WriteLine("the first production.\n");
            else
                System.Console.Error.WriteLine("the second production.\n");

            /* count the conflict */
            emit.num_conflicts++;
            lexer.warning_count++;
        }