All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Parser.LRActionTable Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Parser;

// C# Translation of GoldParser, by Marcus Klimstra .
// Based on GOLDParser by Devin Cook .

/**
 * This class contains the actions (reduce/shift) and goto information
 * for a STATE in a LR parser. Essentially, this is just a row of actions in
 * the LR state transition table. The only data structure is a list of
 * LR Actions.
 */
public class LRActionTable {
    private java.util.ArrayList m_members;

    /* constructor */

    public LRActionTable() {
        m_members = new java.util.ArrayList();
    }

    /* properties */

    public final int getCount() {
        return m_members.size();
    }

    public final java.util.ArrayList getMembers() {
        return m_members;
    }

    /* public methods */

    public final LRAction GetActionForSymbol(int p_symbolIndex) {
        // kan met hashtable bv.
        //Huma:
        LRAction action;
        for (Object actionObj : m_members) {
            action = (LRAction) actionObj;
            if (action.getSymbol().getTableIndex() == p_symbolIndex) {
                return action;
            }
        }

        return null;
    }

    public final LRAction GetItem(int p_index) {
        if (p_index >= 0 && p_index < m_members.size()) {
            return (LRAction) m_members.get(p_index);
        } else {
            return null;
        }
    }

    /**
     * Adds an new LRAction to this table.
     *
     * @param p_symbol The Symbol.
     * @param p_action The Action.
     * @param p_value  The value.
     */
    public final void AddItem(Symbol p_symbol, Action p_action, int p_value) {
        LRAction item = new LRAction();
        item.setSymbol(p_symbol);
        item.setAction(p_action);
        item.setValue(p_value);
        m_members.add(item);
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append("LALR table:\n");
        //Huma:
        LRAction action;
        for (Object actionObj : m_members) {
            action = (LRAction) actionObj;
            result.append("- ").append(action.toString() + "\n");
        }
        return result.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy