public ail.net.parser.FsaStateSet CalculateEClosureFromMove(ail.net.parser.FsaStateSet xi_state_set) { ail.net.framework.Assert.NonNullReference(xi_state_set, "xi_state_set"); ail.net.parser.FsaStateSet result = new ail.net.parser.FsaStateSet(); foreach (ail.net.parser.FsaState state in xi_state_set.States.Values) { if (!result.States.Contains(state.Id)) { result.States.Add(state.Id, state); ail.net.parser.FsaStateSet eclosure = CalculateStateEclosure(state); foreach (ail.net.parser.FsaState e_state in eclosure.States.Values) { if (!result.States.Contains(e_state.Id)) { result.States.Add(e_state.Id, e_state); } } } } return(result); }
public ail.net.parser.Fsa Minimize(ail.net.parser.Fsa.EMinimizationMode xi_mode) { ail.net.parser.Fsa result = new ail.net.parser.Fsa(); // phase I (clean) RemoveUselessStates(); // phase II (divide fsa into equivalent groups, each group will be new fsa state) Hashtable partition = new Hashtable(); BuildPartition(ref partition, xi_mode); // phase III (compose fsa) foreach (ail.net.parser.FsaStateSet group in partition.Values) { ail.net.parser.FsaState new_state = result.AddState(group.Id); ail.net.parser.FsaState old_state = (ail.net.parser.FsaState)ail.net.framework.Functor.FirstElementOfCollection(group.States.Values); // check if group has start state foreach (ail.net.parser.FsaState state in group.States.Values) { if (IsStartState(state)) { result.StartState = new_state; break; } } // check if group final state foreach (ail.net.parser.FsaState state in group.States.Values) { if (IsFinalState(state)) { result.AddFinalState(new_state, state.Token); break; } } // add transitions foreach (ail.net.parser.FsaTransition transition in old_state.Transitions.Values) { ail.net.parser.FsaStateSet transition_group = GetGroupFromState(transition.End, partition); ail.net.framework.Assert.NonNullReference(transition_group, "transition_group"); result.AddTransition(new_state.Id, transition_group.Id, transition.Predicate.Text, transition.Predicate.SwitchChar, transition.Predicate.Context, transition.Predicate.Rank); } } // phase IV (clean) result.RemoveUselessStates(); return(result); }
public ail.net.parser.FsaStateSet CalculateStateEclosure(ail.net.parser.FsaState xi_state) { // Calculate e-closure: // Push all states in T onto stack; // Initialize e-closure(T) to T; // While stack <> empty do // Pop t; // For each transition (t, u) in e-transition do // If u is not in e-closure(T) then // Add u to e-closure(T); // Push u; // End; // End; ail.net.framework.Assert.NonNullReference(xi_state, "xi_state"); ail.net.parser.FsaStateSet result = new ail.net.parser.FsaStateSet(); ail.net.parser.FsaState state = xi_state; result.States.Add(state.Id, state); Stack stack = new Stack(); for (;;) { int count = stack.Count; foreach (ail.net.parser.FsaTransition transition in state.Transitions.Values) { if (transition.IsEpsilon()) { if (!result.States.Contains(transition.End)) { ail.net.parser.FsaState end_state = (ail.net.parser.FsaState)States[transition.End]; ail.net.framework.Assert.NonNullReference(end_state, "end_state"); result.States.Add(end_state.Id, end_state); stack.Push(end_state); } } } if (stack.Count != count && !result.States.Contains(state.Id)) { result.States.Add(state.Id, state); } if (stack.Count == 0) { break; } state = (ail.net.parser.FsaState)stack.Pop(); } return(result); }
private Hashtable SplitGroup(ail.net.parser.FsaStateSet xi_group, Hashtable xi_partition) { ail.net.framework.Assert.NonNullReference(xi_group, "xi_group"); ail.net.framework.Assert.Condition(xi_group.States.Count > 0, "xi_group.States.Count > 0"); ail.net.framework.Assert.NonNullReference(xi_partition, "xi_partition"); Hashtable result = new Hashtable(); // first state introduces new subgroup ail.net.parser.FsaStateSet group = new ail.net.parser.FsaStateSet(GroupCounter.Next()); result.Add(group.Id, group); group.States.Add(((ail.net.parser.FsaState)ail.net.framework.Functor.FirstElementOfCollection(xi_group.States.Values)).Id, (ail.net.parser.FsaState)ail.net.framework.Functor.FirstElementOfCollection(xi_group.States.Values)); // go over states in the group foreach (ail.net.parser.FsaState state in xi_group.States.Values) { bool added = false; // check if state is not ortogonal to any subgroup foreach (ail.net.parser.FsaStateSet subgroup in result.Values) { if (subgroup.States.Contains(state.Id)) // state == state { added = true; break; } ail.net.parser.FsaState group_state = (ail.net.parser.FsaState)ail.net.framework.Functor.FirstElementOfCollection(subgroup.States.Values); if (AreEqual(state, group_state, xi_partition)) { subgroup.States.Add(state.Id, state); added = true; break; } } // if state is ortogonal introduce a new group and add state to this new group if (!added) { group = new ail.net.parser.FsaStateSet(GroupCounter.Next()); result.Add(group.Id, group); group.States.Add(state.Id, state); } } return(result); }
private ail.net.parser.FsaStateSet GetGroupFromState(int xi_state_id, Hashtable xi_partition) { ail.net.framework.Assert.NonNullReference(xi_partition, "xi_partition"); ail.net.parser.FsaStateSet result = null; foreach (ail.net.parser.FsaStateSet group in xi_partition.Values) { if (group.States.Contains(xi_state_id)) { result = group; break; } } return(result); }
private ail.net.parser.FsaStateSet HasDfaCompoundState(ArrayList xi_dfa_states, ail.net.parser.FsaStateSet xi_dfa_state) { ail.net.framework.Assert.NonNullReference(xi_dfa_states, "xi_dfa_states"); ail.net.framework.Assert.NonNullReference(xi_dfa_state, "xi_dfa_state"); ail.net.parser.FsaStateSet result = null; foreach (ail.net.parser.FsaStateSet dfa_state in xi_dfa_states) { if (dfa_state == xi_dfa_state) { result = dfa_state; break; } } return(result); }
private void BuildPartition(ref Hashtable xio_partition) { ail.net.framework.Assert.NonNullReference(xio_partition, "xio_partition"); BuildPrimaryPartition(ref xio_partition); for (;;) { bool process = false; ArrayList groups = new ArrayList(xio_partition.Values); for (int i = 0; i < groups.Count; i++) { ail.net.parser.FsaStateSet group = (ail.net.parser.FsaStateSet)groups[i]; if (group.States.Count > 1) { Hashtable partition = SplitGroup(group, xio_partition); if (partition.Count > 1) { // substitute group with new set of subgroups process = true; xio_partition.Remove(group.Id); foreach (ail.net.parser.FsaStateSet new_group in partition.Values) { xio_partition.Add(new_group.Id, new_group); // id is unique } } } } if (!process) { break; } } }
public ail.net.parser.FsaStateSet CalculateMove(ail.net.parser.FsaStateSet xi_state_set, string xi_predicate) { // Algorithm to compute T=Move(S,c) // T = 0; // for each state s in S // { // for each edge e from s to some s’ // { // if(e is labelled with c) // { // T = T U closure({s’}); // } // } // } ail.net.framework.Assert.NonNullReference(xi_state_set, "xi_state_set"); ail.net.framework.Assert.NonEmptyString(xi_predicate, "xi_predicate"); ail.net.parser.FsaStateSet result = new ail.net.parser.FsaStateSet(); foreach (ail.net.parser.FsaState state in xi_state_set.States.Values) { foreach (ail.net.parser.FsaTransition transition in state.Transitions.Values) { if (transition.Predicate.Text == xi_predicate) { if (!result.States.Contains(transition.End)) { ail.net.parser.FsaState end_state = (ail.net.parser.FsaState)States[transition.End]; ail.net.framework.Assert.NonNullReference(end_state, "end_state"); result.States.Add(end_state.Id, end_state); } } } } return(result); }
private void BuildTable(ref Hashtable xio_partition) { //!! not implemented due to huge tables being built, postponed :( // Mark the distinguishable pairs of states. // To achieve this task, we first mark all pairs (p,q), where p belongs to F and // q does not belong to F as distinguishable. Then, we proceed as follows: // repeat // for all non-marked pairs {p,q} do // for each letter 'a' do // if the pair {(s)igma(p,a), (s)igma(q,a)} is marked // then mark {p,q} // until no new pairs are marked ail.net.framework.Assert.NonNullReference(xio_partition, "xio_partition"); // initialization, this algorithm is sensitive to states organization ResetMarkedStates(); xio_partition.Clear(); // table for 'A B C D E' states, cells in use marked with '*' // 0 A // 1 B * // 2 C * * // 3 D * * * // 4 E * * * * // A B C D E // 0 1 2 3 4 // access by (p, q) = table[ArithmeticProgressionSum(Math.Max(0, p_id-1)+q_id] // map[index][state] ArrayList map = new ArrayList(States.Values); Hashtable table = new Hashtable(CalculateTableSize()); // create table of pairs, also mark all pairs of accept and non-accept states as non-equivalent // keep only one of pairs (p, q) or (q, p) ArrayList states = new ArrayList(States.Values); states.Sort(); // states must be numbered in sequence for (int i = 0; i < states.Count - 1; i++) { for (int j = i + 1; j < states.Count; j++) { ail.net.parser.FsaState p_state = (ail.net.parser.FsaState)states[i]; ail.net.parser.FsaState q_state = (ail.net.parser.FsaState)states[j]; ail.net.parser.FsaPair pair = new ail.net.parser.FsaPair(p_state, q_state); table.Add(new ail.net.parser.FsaPairKey(p_state, q_state), pair); bool p_final = IsFinalState(p_state); bool q_final = IsFinalState(q_state); bool mark = ((p_final && !q_final) || (!p_final && q_final)); if (!mark && p_final && q_final) { mark = p_state.Token.Type != q_state.Token.Type; } if (mark) { pair.Marked = true; } } } // populate table for (;;) { bool process = false; foreach (ail.net.parser.FsaPair pair in table.Values) { if (!pair.Marked) { Hashtable predicates = CombinePredicates(pair.PState, pair.QState); foreach (ail.net.parser.FsaTransitionPredicate predicate in predicates.Values) { ail.net.parser.FsaTransition p_state_t = pair.PState.GetTransitionByPredicate(predicate); ail.net.parser.FsaTransition q_state_t = pair.QState.GetTransitionByPredicate(predicate); if ((p_state_t == (object)null || q_state_t == (object)null)) { continue; } ail.net.parser.FsaState p_state = (ail.net.parser.FsaState)States[p_state_t.End]; ail.net.parser.FsaState q_state = (ail.net.parser.FsaState)States[q_state_t.End]; if ((object)p_state == (object)q_state) // pairs with same states are assumped unmarked { continue; } ail.net.parser.FsaPair sigma_pair = GetPairFromStates(p_state, q_state, table); if (sigma_pair.Marked) { pair.Marked = true; process = true; } } } } if (!process) { break; } } ResetMarkedStates(); // build partition foreach (ail.net.parser.FsaState state in states) { if (!state.Marked) { // add equivalent groups, optimistic assumption ail.net.parser.FsaStateSet group = new ail.net.parser.FsaStateSet(0); for (int i = 0; i < state.Id; i++) { ail.net.parser.FsaPair pair = GetPairFromStates((ail.net.parser.FsaState)States[i], state, table); if (!pair.Marked) { if ((object)pair.QState != (object)state) { if (!pair.QState.Marked) { group.States.Add(pair.QState.Id, pair.QState); } } else { if (!pair.PState.Marked) { group.States.Add(pair.PState.Id, pair.PState); } } } } if (group.States.Count > 0) { group.States.Add(state.Id, state); group.Id = GroupCounter.Next(); // correct id xio_partition.Add(group.Id, group); foreach (ail.net.parser.FsaState group_state in group.States.Values) { group_state.Marked = true; } } } } foreach (ail.net.parser.FsaState state in states) { if (!state.Marked) { // add non-equivalent groups ail.net.parser.FsaStateSet group = new ail.net.parser.FsaStateSet(GroupCounter.Next()); group.States.Add(state.Id, state); xio_partition.Add(group.Id, group); } } ResetMarkedStates(); }
public ail.net.parser.Fsa Nfa2Dfa() { // Conversion of an NFA into a DFA (subset construction) // ..................................................... // Input: An NFA N. // Output: A DFA D accepting the same language. // Operations: // e-closure(s) is the set of NFA states reachable from s on e-transitions alone. // e-closure(T) is the union of e-closure(r) for all r in T. // move(T, a) is the set of NFA states to which there is a transition on input a from some NFA state in T. // // set the start state to e-closure(s0) and unmark it. // While there is an unmarked state T in Dstates do // Mark T // For each input symbol a do // If U := e-closure(move(T, a)); // If U is not in Dstates then // Add U as an unmarked state to Dstates; // Dtran(T, a) := U; // End; // End; ail.net.parser.Fsa result = new ail.net.parser.Fsa(); ArrayList dfa_states = new ArrayList(); // build pseudo dfa ail.net.parser.FsaStateSet start_dfa_state = CalculateStateEclosure(StartState); start_dfa_state.Id = dfa_states.Count; start_dfa_state.Marked = false; dfa_states.Add(start_dfa_state); bool proceed = false; for (;;) { IEnumerator dfa_state_enum = dfa_states.GetEnumerator(); while (dfa_state_enum.MoveNext()) { ail.net.parser.FsaStateSet dfa_state = (ail.net.parser.FsaStateSet)dfa_state_enum.Current; if (!dfa_state.Marked) { dfa_state.Marked = true; foreach (ail.net.parser.FsaTransitionPredicate predicate in Predicates.Values) { if (predicate.Text != ail.net.parser.FsaTransition.kEpsilonPredicate) { ail.net.parser.FsaStateSet move_set = CalculateMove(dfa_state, predicate.Text); if (move_set != (object)null) { ail.net.parser.FsaStateSet pseudo_dfa_state = CalculateEClosureFromMove(move_set); if (pseudo_dfa_state != (object)null && pseudo_dfa_state.States.Count > 0) { ail.net.parser.FsaStateSet new_dfa_state = HasDfaCompoundState(dfa_states, pseudo_dfa_state); if (new_dfa_state == (object)null) { new_dfa_state = pseudo_dfa_state; new_dfa_state.Id = dfa_states.Count; new_dfa_state.Marked = false; dfa_states.Add(new_dfa_state); dfa_state_enum = dfa_states.GetEnumerator(); // reset iterator } if (!dfa_state.Transitions.Contains(dfa_state.Transitions.Count)) { ail.net.parser.FsaTransition transition = new ail.net.parser.FsaTransition(dfa_state.Transitions.Count, dfa_state.Id, new_dfa_state.Id, predicate.Text, predicate.SwitchChar, predicate.Context, predicate.Rank); dfa_state.Transitions.Add(transition.Id, transition); } } } } } proceed = true; } } if (!proceed) { break; } proceed = false; } // populate states and final states foreach (ail.net.parser.FsaStateSet dfa_state in dfa_states) { ail.net.parser.FsaState state = result.AddState(dfa_state.Id); ail.net.framework.Assert.NonNullReference(state, "state"); foreach (ail.net.parser.FsaTransition transition in dfa_state.Transitions.Values) { result.AddTransition(transition.Start, transition.End, transition.Predicate.Text, transition.Predicate.SwitchChar, transition.Predicate.Context, transition.Predicate.Rank); } ail.net.parser.FsaState final_state = null; foreach (ail.net.parser.FsaState tmp_state in dfa_state.States.Values) { if (FinalStates.Contains(tmp_state.Id)) { ail.net.parser.FsaState org_state = (ail.net.parser.FsaState)States[tmp_state.Id]; ail.net.framework.Assert.NonNullReference(org_state, "org_state"); if (final_state == (object)null || org_state.Token.Priority > final_state.Token.Priority) { final_state = org_state; } } } if (final_state != (object)null) { result.AddFinalState(state, final_state.Token); } } result.StateCounter.Reset(States.Count); return(result); }
private void BuildPrimaryPartition(ref Hashtable xio_partition) { // builds primary partition, which consist of two groups: {F} anf {Q-F} ail.net.framework.Assert.NonNullReference(xio_partition, "xio_partition"); xio_partition.Clear(); ail.net.parser.FsaStateSet nf_states = new ail.net.parser.FsaStateSet(GroupCounter.Next()); // non-final states ail.net.parser.FsaStateSet fn_states = new ail.net.parser.FsaStateSet(GroupCounter.Next()); // final states xio_partition.Add(nf_states.Id, nf_states); // final states will be split and add later foreach (ail.net.parser.FsaState state in States.Values) { if (IsFinalState(state)) { fn_states.States.Add(state.Id, state); } else { nf_states.States.Add(state.Id, state); } } // also, for lexical analyzer we should separate final states by tokens into different groups Hashtable subgroups = new Hashtable(); ail.net.parser.FsaStateSet group = new ail.net.parser.FsaStateSet(GroupCounter.Next()); ail.net.parser.FsaState pin_state = (ail.net.parser.FsaState)ail.net.framework.Functor.FirstElementOfCollection(fn_states.States.Values); group.States.Add(pin_state.Id, pin_state); subgroups.Add(group.Id, group); _start: foreach (ail.net.parser.FsaState state in fn_states.States.Values) { if (state.Marked) { continue; } ail.net.parser.FsaStateSet subgroup_to_add = null; // find subgroup it may belong to foreach (ail.net.parser.FsaStateSet subgroup in subgroups.Values) { foreach (ail.net.parser.FsaState subgroup_state in subgroup.States.Values) { if (state.Token.Type == subgroup_state.Token.Type) { subgroup_to_add = subgroup; break; } } if (subgroup_to_add != (object)null) { break; } } if (subgroup_to_add == (object)null) // not found - split { ail.net.parser.FsaStateSet new_group = new ail.net.parser.FsaStateSet(GroupCounter.Next()); new_group.States.Add(state.Id, state); subgroups.Add(new_group.Id, new_group); state.Marked = true; goto _start; // fortunately not so many final states :(( } else { if (!subgroup_to_add.States.Contains(state.Id)) { subgroup_to_add.States.Add(state.Id, state); } } } // clean up _reset: foreach (ail.net.parser.FsaState state in fn_states.States.Values) { if (state.Marked) { state.Marked = false; fn_states.States.Remove(state.Id); goto _reset; } } // insert subgroups foreach (ail.net.parser.FsaStateSet subgroup in subgroups.Values) { xio_partition.Add(subgroup.Id, subgroup); } }