/* * Function: machine * Description: Recursive descent regular expression parser. */ private static Nfa machine() { Nfa start; Nfa p; BitSet states; #if DESCENT_DEBUG Utility.enter("machine", spec.lexeme, spec.current_token); #endif start = Alloc.NewNfa(spec); p = start; states = gen.GetStates(); /* Begin: Added for states. */ spec.current_token = Gen.EOS; gen.Advance(); /* End: Added for states. */ if (Gen.END_OF_INPUT != spec.current_token) { p.SetNext(rule()); ProcessStates(states, p.GetNext()); } while (Gen.END_OF_INPUT != spec.current_token) { /* Make state changes HERE. */ states = gen.GetStates(); /* Begin: Added for states. */ gen.Advance(); if (Gen.END_OF_INPUT == spec.current_token) { break; } /* End: Added for states. */ p.SetSib(Alloc.NewNfa(spec)); p = p.GetSib(); p.SetNext(rule()); ProcessStates(states, p.GetNext()); } /* * add pseudo-rules for BOL and EOF */ p.SetSib(Alloc.NewNfa(spec)); p = p.GetSib(); p.SetNext(Alloc.NewNfa(spec)); Nfa pnext = p.GetNext(); pnext.SetEdge(Nfa.CCL); pnext.SetNext(Alloc.NewNfa(spec)); pnext.SetCharSet(new CharSet()); pnext.GetCharSet().add(spec.BOL); pnext.GetCharSet().add(spec.EOF); // do-nothing accept rule pnext.GetNext().SetAccept(new Accept(null, input.line_number + 1)); /* add the pseudo rules */ for (int i = 0; i < spec.states.Count; i++) { ArrayList srule = spec.state_rules[i]; srule.Add(pnext); } #if DESCENT_DEBUG Utility.leave("machine", spec.lexeme, spec.current_token); #endif return(start); }
/* * Function: rule * Description: Recursive descent regular expression parser. */ private static Nfa rule() { NfaPair pair; Nfa start = null; Nfa end = null; int anchor = Spec.NONE; #if DESCENT_DEBUG Utility.enter("rule", spec.lexeme, spec.current_token); #endif pair = Alloc.NewNfaPair(); if (Gen.AT_BOL == spec.current_token) { anchor = anchor | Spec.START; gen.Advance(); expr(pair); start = Alloc.NewNfa(spec); start.SetEdge(spec.BOL); start.SetNext(pair.start); end = pair.end; } else { expr(pair); start = pair.start; end = pair.end; } if (Gen.AT_EOL == spec.current_token) { gen.Advance(); NfaPair nlpair = Alloc.NewNLPair(spec); end.SetNext(Alloc.NewNfa(spec)); Nfa enext = end.GetNext(); enext.SetNext(nlpair.start); enext.SetSib(Alloc.NewNfa(spec)); enext.GetSib().SetEdge(spec.EOF); enext.GetSib().SetNext(nlpair.end); end = nlpair.end; anchor = anchor | Spec.END; } /* check for null rules */ if (end == null) { Error.parse_error(Error.E_ZERO, input.line_number); } /* Handle end of regular expression */ end.SetAccept(gen.packAccept()); end.SetAnchor(anchor); #if DESCENT_DEBUG Utility.leave("rule", spec.lexeme, spec.current_token); #endif return(start); }
/* * Function: e_closure * Description: Alters input set. */ public void e_closure() { Nfa state = null; /* * Debug checks */ #if DEBUG Utility.assert(null != nfa_set); Utility.assert(null != nfa_bit); #endif accept = null; anchor = Spec.NONE; accept_index = Utility.INT_MAX; /* * Create initial stack. */ Stack nfa_stack = new Stack(); int size = nfa_set.Count; for (int i = 0; i < size; i++) { state = (Nfa)nfa_set[i]; #if DEBUG Utility.assert(nfa_bit.Get(state.GetLabel())); #endif nfa_stack.Push(state); } /* * Main loop. */ while (nfa_stack.Count > 0) { Object o = nfa_stack.Pop(); if (o == null) { break; } state = (Nfa)o; #if OLD_DUMP_DEBUG if (null != state.GetAccept()) { Console.WriteLine("Looking at accepting state " + Int32.ToString(state.GetLabel()) + " with <" + state.GetAccept().action + ">"); } #endif if (null != state.GetAccept() && state.GetLabel() < accept_index) { accept_index = state.GetLabel(); accept = state.GetAccept(); anchor = state.GetAnchor(); #if OLD_DUMP_DEBUG Console.WriteLine("Found accepting state " + Int32.ToString(state.GetLabel()) + " with <" + state.GetAccept().action + ">"); #endif #if DEBUG Utility.assert(null != accept); Utility.assert(Spec.NONE == anchor || 0 != (anchor & Spec.END) || 0 != (anchor & Spec.START)); #endif } if (Nfa.EPSILON == state.GetEdge()) { if (state.GetNext() != null) { if (false == nfa_set.Contains(state.GetNext())) { #if DEBUG Utility.assert(false == nfa_bit.Get(state.GetNext().GetLabel())); #endif nfa_bit.Set(state.GetNext().GetLabel(), true); nfa_set.Add(state.GetNext()); nfa_stack.Push(state.GetNext()); } } if (null != state.GetSib()) { if (false == nfa_set.Contains(state.GetSib())) { #if DEBUG Utility.assert(false == nfa_bit.Get(state.GetSib().GetLabel())); #endif nfa_bit.Set(state.GetSib().GetLabel(), true); nfa_set.Add(state.GetSib()); nfa_stack.Push(state.GetSib()); } } } } if (null != nfa_set) { sort_states(); } }