//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ // PLEASE DO NOT EDIT WITHOUT THE EXPLICIT CONSENT OF THE AUTHOR! \\ //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ using System.Text; using System.Collections; using Ilog.Language.Util; namespace Ilog.Language.Syntax { /** * A state is really a set of rule items, also represented as a hash * table for efficient computation of the next state. The fields are: *
* CLOSURE(s) = s U { C --> .W | A --> P.BS in s & B L* C }
*
*/
internal void Closure ()
{
foreach (Item item in items)
{
GrammarSymbol marker = item.Marker;
if (marker is NonTerminal)
foreach (NonTerminal n in ((NonTerminal)marker).LSet)
foreach (Rule rule in n.Rules)
Add(grammar.GetItem(rule,1));
}
}
/**
* Compute the next state for all the entries in the transition table.
*/
internal void ComputeNextStates ()
{
foreach (StateTransition transition in transitions.Values)
transition.ComputeNextState();
}
/**
* Extracts the kernel items from the set of items.
*/
internal void ExtractKernels ()
{
kernels = new SetOf(grammar.Items);
foreach (Item item in items)
if (item.IsKernel)
kernels.Add(item.Index);
}
/**
* Compute the PRED relation for this state, and returns
* true iff this made an actual change in
* PRED(this,item) for some item.
*/
internal bool ComputePreds ()
{
bool change = false;
foreach (StateTransition transition in transitions.Values)
change |= transition.ComputePreds();
return change;
}
/**
* Returns the next state for the given symbol, or null
* if so such state exists.
*/
internal State Next (GrammarSymbol symbol)
{
return GetTransition(symbol).Next;
}
/**
* A table associating nonterminals to their follow sets in this
* state.
*/
private Hashtable followTable;
internal Hashtable FollowTable
{
get { return followTable; }
}
/**
* Returns the follow set for the given nonterminal in this state
* if one has been computed; otherwise, returns the empty set.
*/
internal SetOf Follow (NonTerminal n)
{
Follow f;
if (followTable == null || (f=(Follow)followTable[n]) == null)
return new SetOf(grammar.Terminals);
return f.Follows;
}
/**
* For the given symbol, this checks whether Follow(this,symbol)
* already exists. If so, it returns it. If not, this creates a new
* Follow object, records it as appropriate, and returns it.
*/
internal Follow GetFollow (NonTerminal n)
{
if (followTable == null)
followTable = new Hashtable();
Follow f = (Follow)followTable[n];
if (f == null)
{
f = new Follow(this,n);
followTable[n] = f;
f.Add();
grammar.FCount++;
}
return f;
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
/**
* This records description of conflicts encountered in this state.
*/
private ArrayList conflicts;
internal ArrayList Conflicts
{
get { return conflicts; }
}
/**
* Adds the specified conflict description.
*/
internal void AddConflict (string conflict)
{
if (conflicts == null)
conflicts = new ArrayList(5);
conflicts.Add(conflict);
}
/**
* Displays conflicts encountered in this state, if any.
*/
internal void ShowConflicts ()
{
if (conflicts == null)
return;
Grammar.Say("This state has conflicts:\n");
foreach (string conflict in conflicts)
Grammar.Say(conflict);
Grammar.Say("-----------------------------");
}
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
/**
* A list of sets of actions for dynamic operators.
*/
private ArrayList dynamicActions = new ArrayList();
internal ArrayList DynamicActions
{
get { return dynamicActions; }
}
/**
* A table recording the parsing automaton actions for this state.
*/
private Map actionTable = new Map();
internal Map ActionTable
{
get { return actionTable; }
set { actionTable = value; }
}
/**
* This is the index identifying this state's action table.
*/
private int ac_index;
internal int ActionTableIndex
{
get { return ac_index; }
set { ac_index = value; }
}
/*
* Returns the parsing action to perform upon reading the
* specified token symbol when in this state.
*/
internal Action GetAction (Terminal symbol)
{
return (Action)actionTable[symbol];
}
/**
* Sets the parsing action to perform in this state for the
* specified token symbol.
*/
internal void SetAction (Terminal symbol, Action action)
{
actionTable[symbol] = action;
}
/**
* A table recording the parsing automaton "goto" transitions from
* this state.
*/
private Map gotoTable = new Map();
internal Map GotoTable
{
get { return gotoTable; }
set { gotoTable = value; }
}
/**
* This is the index identifying this state's goto table.
*/
private int gt_index;
internal int GotoTableIndex
{
get { return gt_index; }
set { gt_index = value; }
}
/*
* Returns the state to go to upon seeing the specified noterminal
* symbol on the top of the stack when in this state.
*/
internal State GetGoto (NonTerminal symbol)
{
return (State)gotoTable[symbol];
}
/*
* Sets the state to go to upon seeing the specified noterminal
* symbol on the top of the stack when in this state to the
* specified state.
*/
internal void SetGoto (NonTerminal symbol, State next)
{
gotoTable[symbol] = next;
}
/**
* A help method returning a string representation of this state's
* set of dynamic actions.
*/
private string DynamicActionSet ()
{
StringBuilder b = new StringBuilder("{");
int n = dynamicActions.Count;
for (int i=0; i