/*************************************************************** * Function: emit * Description: High-level access function to module. **************************************************************/ public void emit ( CSpec spec, System.IO.TextWriter outstream ) { Set(spec, outstream); if (CUtility.DEBUG) { CUtility.ASSERT(null != m_spec); CUtility.ASSERT(null != m_outstream); } if (CUtility.OLD_DEBUG) { print_details(); } emit_header(); emit_construct(); emit_helpers(); emit_driver(); emit_footer(); reset(); }
/*************************************************************** * Function: emit_header * Description: Emits class header. **************************************************************/ private void emit_header ( ) { if (CUtility.DEBUG) { CUtility.ASSERT(null != m_spec); CUtility.ASSERT(null != m_outstream); } m_outstream.WriteLine(); m_outstream.WriteLine(); if (true == m_spec.m_public) { m_outstream.Write("public "); } m_outstream.Write("class "); m_outstream.Write(new string(m_spec.m_class_name, 0, m_spec.m_class_name.Length)); if (m_spec.m_implements_name.Length > 0) { m_outstream.Write(" : "); m_outstream.Write(new string(m_spec.m_implements_name, 0, m_spec.m_implements_name.Length)); } m_outstream.WriteLine(" {"); }
/*************************************************************** * Function: dodash * Description: Recursive descent regular expression parser. **************************************************************/ private void dodash ( CSet Set ) { int first = -1; if (CUtility.DESCENT_DEBUG) { CUtility.enter("dodash", m_spec.m_lexeme, m_spec.m_current_token); } while (CLexGen.EOS != m_spec.m_current_token && CLexGen.CCL_END != m_spec.m_current_token) { // DASH loses its special meaning if it is first in class. if (CLexGen.DASH == m_spec.m_current_token && -1 != first) { m_lexGen.advance(); // DASH loses its special meaning if it is last in class. if (m_spec.m_current_token == CLexGen.CCL_END) { // 'first' already in Set. Set.add('-'); break; } for ( ; first <= m_spec.m_lexeme; ++first) { if (m_spec.m_ignorecase) { Set.addncase((char)first); } else { Set.add(first); } } } else { first = m_spec.m_lexeme; if (m_spec.m_ignorecase) { Set.addncase(m_spec.m_lexeme); } else { Set.add(m_spec.m_lexeme); } } m_lexGen.advance(); } if (CUtility.DESCENT_DEBUG) { CUtility.leave("dodash", m_spec.m_lexeme, m_spec.m_current_token); } }
/** Self-test. */ public static void _Main(string[] args) { const int ITER = 500; const int RANGE = 65536; SparseBitSet a = new SparseBitSet(); CUtility.ASSERT(!a.Get(0) && !a.Get(1)); CUtility.ASSERT(!a.Get(123329)); a.Set(0); CUtility.ASSERT(a.Get(0) && !a.Get(1)); a.Set(1); CUtility.ASSERT(a.Get(0) && a.Get(1)); a.clearAll(); CUtility.ASSERT(!a.Get(0) && !a.Get(1)); Random r = new Random(); Vector v = new Vector(); for (int n = 0; n < ITER; n++) { int rr = ((r.Next() >> 1) % RANGE) << 1; a.Set(rr); v.addElement(rr); // check that all the numbers are there. CUtility.ASSERT(a.Get(rr) && !a.Get(rr + 1) && !a.Get(rr - 1)); for (int i = 0; i < v.size(); i++) { CUtility.ASSERT(a.Get((int)v.elementAt(i))); } } SparseBitSet b = (SparseBitSet)a.Clone(); CUtility.ASSERT(a.Equals(b) && b.Equals(a)); for (int n = 0; n < ITER / 2; n++) { int rr = (r.Next() >> 1) % v.size(); int m = (int)v.elementAt(rr); b.clear(m); v.removeElementAt(rr); // check that numbers are removed properly. CUtility.ASSERT(!b.Get(m)); } CUtility.ASSERT(!a.Equals(b)); SparseBitSet c = (SparseBitSet)a.Clone(); SparseBitSet d = (SparseBitSet)a.Clone(); c.and(a); CUtility.ASSERT(c.Equals(a) && a.Equals(c)); c.xor(a); CUtility.ASSERT(!c.Equals(a) && c.size() == 0); d.or(b); CUtility.ASSERT(d.Equals(a) && !b.Equals(d)); d.and(b); CUtility.ASSERT(!d.Equals(a) && b.Equals(d)); d.xor(a); CUtility.ASSERT(!d.Equals(a) && !b.Equals(d)); c.or(d); c.or(b); CUtility.ASSERT(c.Equals(a) && a.Equals(c)); c = (SparseBitSet)d.Clone(); c.and(b); CUtility.ASSERT(c.size() == 0); System.Console.WriteLine("Success."); }
/*************************************************************** * Function: allocate_BOL_EOF * Description: Expands character class to include special BOL and * EOF characters. Puts numeric index of these characters in * input CSpec. **************************************************************/ public void allocate_BOL_EOF ( CSpec spec ) { CUtility.ASSERT(CSpec.NUM_PSEUDO == 2); spec.BOL = spec.m_dtrans_ncols++; spec.EOF = spec.m_dtrans_ncols++; }
/*************************************************************** * Function: emit_footer * Description: **************************************************************/ private void emit_footer ( ) { if (CUtility.DEBUG) { CUtility.ASSERT(null != m_spec); CUtility.ASSERT(null != m_outstream); } m_outstream.WriteLine("}"); }
private void insert_block(int idx, int bnum) { CUtility.ASSERT(idx <= _size); CUtility.ASSERT(idx == _size || offs[idx] != bnum); // System.arraycopy(bits, idx, bits, idx+1, size-idx); Array.Copy(bits, idx, bits, idx + 1, _size - idx); //System.arraycopy(offs, idx, offs, idx+1, size-idx); Array.Copy(offs, idx, offs, idx + 1, _size - idx); offs[idx] = bnum; bits[idx] = 0; //clear them bits. _size++; }
/*************************************************************** * Function: Set * Description: Sets member variables. **************************************************************/ private void Set ( CSpec spec ) { if (CUtility.DEBUG) { CUtility.ASSERT(null != spec); } m_spec = spec; m_group = null; m_ingroup = null; }
/*************************************************************** * Function: Set * Description: Initializes member variables. **************************************************************/ private void Set ( CSpec spec, TextWriter outstream ) { if (CUtility.DEBUG) { CUtility.ASSERT(null != spec); CUtility.ASSERT(null != outstream); } m_spec = spec; m_outstream = outstream; }
private void new_block(int idx, int bnum) { if (_size == bits.Length) { // resize long[] nbits = new long[_size * 3]; int [] noffs = new int [_size * 3]; // System.arraycopy(bits, 0, nbits, 0, size); Array.Copy(bits, nbits, _size); // System.arraycopy(offs, 0, noffs, 0, size); Array.Copy(offs, noffs, _size); bits = nbits; offs = noffs; } CUtility.ASSERT(_size < bits.Length); insert_block(idx, bnum); }
/*************************************************************** * Function: factor * Description: Recursive descent regular expression parser. **************************************************************/ private void factor ( CNfaPair pair ) { CNfa start = null; CNfa end = null; if (CUtility.DESCENT_DEBUG) { CUtility.enter("factor", m_spec.m_lexeme, m_spec.m_current_token); } term(pair); if (CLexGen.CLOSURE == m_spec.m_current_token || CLexGen.PLUS_CLOSE == m_spec.m_current_token || CLexGen.OPTIONAL == m_spec.m_current_token) { start = CAlloc.newCNfa(m_spec); end = CAlloc.newCNfa(m_spec); start.m_next = pair.m_start; pair.m_end.m_next = end; if (CLexGen.CLOSURE == m_spec.m_current_token || CLexGen.OPTIONAL == m_spec.m_current_token) { start.m_next2 = end; } if (CLexGen.CLOSURE == m_spec.m_current_token || CLexGen.PLUS_CLOSE == m_spec.m_current_token) { pair.m_end.m_next2 = pair.m_start; } pair.m_start = start; pair.m_end = end; m_lexGen.advance(); } if (CUtility.DESCENT_DEBUG) { CUtility.leave("factor", m_spec.m_lexeme, m_spec.m_current_token); } }
/*************************************************************** * Function: emit_actions * Description: **************************************************************/ private void emit_actions ( string tabs ) { int elem; int size; int bogus_index; CAccept accept; if (CUtility.DEBUG) { CUtility.ASSERT(m_spec.m_accept_vector.size() == m_spec.m_anchor_array.Length); } bogus_index = -2; size = m_spec.m_accept_vector.size(); for (elem = 0; elem < size; ++elem) { accept = (CAccept)m_spec.m_accept_vector.elementAt(elem); if (null != accept) { m_outstream.WriteLine(tabs + "case " + elem + ":"); m_outstream.Write(tabs + "\t"); string tmp = new string(accept.m_action, 0, accept.m_action_read); //Added by SI: if (tmp.Equals("{ }")) { tmp = "break;"; } //Added by SI: if (tmp.Equals("")) { tmp = "break;"; } m_outstream.Write(tmp); m_outstream.WriteLine(); m_outstream.WriteLine(tabs + "case " + bogus_index + ":"); m_outstream.WriteLine(tabs + "\tbreak;"); --bogus_index; } } }
/*************************************************************** * Function: Set * Description: Sets CMakeNfa member variables. **************************************************************/ private void Set ( CLexGen lexGen, CSpec spec, CInput input ) { if (CUtility.DEBUG) { CUtility.ASSERT(null != input); CUtility.ASSERT(null != lexGen); CUtility.ASSERT(null != spec); } m_input = input; m_lexGen = lexGen; m_spec = spec; }
/*************************************************************** * Function: expr * Description: Recursive descent regular expression parser. **************************************************************/ private void expr ( CNfaPair pair ) { CNfaPair e2_pair; CNfa p; if (CUtility.DESCENT_DEBUG) { CUtility.enter("expr", m_spec.m_lexeme, m_spec.m_current_token); } if (CUtility.DEBUG) { CUtility.ASSERT(null != pair); } e2_pair = CAlloc.newCNfaPair(); cat_expr(pair); while (CLexGen.OR == m_spec.m_current_token) { m_lexGen.advance(); cat_expr(e2_pair); p = CAlloc.newCNfa(m_spec); p.m_next2 = e2_pair.m_start; p.m_next = pair.m_start; pair.m_start = p; p = CAlloc.newCNfa(m_spec); pair.m_end.m_next = p; e2_pair.m_end.m_next = p; pair.m_end = p; } if (CUtility.DESCENT_DEBUG) { CUtility.leave("expr", m_spec.m_lexeme, m_spec.m_current_token); } }
/*************************************************************** * Function: emit_states * Description: Emits constants that serve as lexical states, * including YYINITIAL. **************************************************************/ private void emit_states ( ) { IEnumerator states; string state; int index; states = m_spec.m_states.Keys.GetEnumerator(); /*index = 0;*/ while (states.MoveNext()) { state = (string)states.Current; if (CUtility.DEBUG) { CUtility.ASSERT(null != state); } m_outstream.WriteLine("\tprivate const int " + state + " = " + (m_spec.m_states[state]).ToString() + ";"); /*++index;*/ } m_outstream.WriteLine("\tprivate static readonly int[] yy_state_dtrans =new int[] {"); for (index = 0; index < m_spec.m_state_dtrans.Length; ++index) { m_outstream.Write("\t\t" + m_spec.m_state_dtrans[index]); if (index < m_spec.m_state_dtrans.Length - 1) { m_outstream.WriteLine(","); } else { m_outstream.WriteLine(); } } m_outstream.WriteLine("\t};"); }
/*************************************************************** * Function: cat_expr * Description: Recursive descent regular expression parser. **************************************************************/ private void cat_expr ( CNfaPair pair ) { CNfaPair e2_pair; if (CUtility.DESCENT_DEBUG) { CUtility.enter("cat_expr", m_spec.m_lexeme, m_spec.m_current_token); } if (CUtility.DEBUG) { CUtility.ASSERT(null != pair); } e2_pair = CAlloc.newCNfaPair(); if (first_in_cat(m_spec.m_current_token)) { factor(pair); } while (first_in_cat(m_spec.m_current_token)) { factor(e2_pair); /* Destroy */ pair.m_end.mimic(e2_pair.m_start); discardCNfa(e2_pair.m_start); pair.m_end = e2_pair.m_end; } if (CUtility.DESCENT_DEBUG) { CUtility.leave("cat_expr", m_spec.m_lexeme, m_spec.m_current_token); } }
/*************************************************************** * function: add_to_dstates * Description: Takes as input a CBunch with details of * a dfa state that needs to be created. * 1) Allocates a new dfa state and saves it in * the appropriate CSpec vector. * 2) Initializes the fields of the dfa state * with the information in the CBunch. * 3) Returns index of new dfa. **************************************************************/ private int add_to_dstates ( CBunch bunch ) { CDfa dfa; if (CUtility.DEBUG) { CUtility.ASSERT(null != bunch.m_nfa_set); CUtility.ASSERT(null != bunch.m_nfa_bit); CUtility.ASSERT(null != bunch.m_accept || CSpec.NONE == bunch.m_anchor); } /* Allocate, passing CSpec so dfa label can be Set. */ dfa = CAlloc.newCDfa(m_spec); /* Initialize fields, including the mark field. */ dfa.m_nfa_set = (Vector)bunch.m_nfa_set.Clone(); dfa.m_nfa_bit = (SparseBitSet)bunch.m_nfa_bit.Clone(); dfa.m_accept = bunch.m_accept; dfa.m_anchor = bunch.m_anchor; dfa.m_mark = false; /* Register dfa state using BitSet in CSpec Hashtable. */ m_spec.m_dfa_sets.Add(dfa.m_nfa_bit, dfa); /*registerCDfa(dfa);*/ if (CUtility.OLD_DUMP_DEBUG) { System.Console.Write("Registering Set : "); m_lexGen.print_set(dfa.m_nfa_set); System.Console.WriteLine(); } return(dfa.m_label); }
/*************************************************************** * Function: CInput * Description: **************************************************************/ public CInput ( TextReader input ) { if (CUtility.DEBUG) { CUtility.ASSERT(null != input); } /* Initialize input stream. */ m_input = input; /* Initialize buffers and index counters. */ m_line = null; m_line_read = 0; m_line_index = 0; /* Initialize state variables. */ m_eof_reached = false; m_line_number = 0; m_pushback_line = false; }
private int bsearch(int bnum) { int l = 0, r = _size; // search interval is [l, r) while (l < r) { int p = (l + r) / 2; if (bnum < offs[p]) { r = p; } else if (bnum > offs[p]) { l = p + 1; } else { return(p); } } CUtility.ASSERT(l == r); return(l); // index at which the bnum *should* be, if it's not. }
/*************************************************************** * Function: emit_helpers * Description: Emits helper functions, particularly * error handling and input buffering. **************************************************************/ private void emit_helpers ( ) { if (CUtility.DEBUG) { CUtility.ASSERT(null != m_spec); CUtility.ASSERT(null != m_outstream); } /* Function: yy_do_eof */ m_outstream.WriteLine("\tprivate bool yy_eof_done = false;"); if (null != m_spec.m_eof_code) { m_outstream.Write("\tprivate void yy_do_eof ()"); //SI:throw code not necessary /* if (null != m_spec.m_eof_throw_code) * { * m_outstream.WriteLine(); * m_outstream.Write("\t\tthrows "); * m_outstream.WriteLine(new string(m_spec.m_eof_throw_code,0, * m_spec.m_eof_throw_read)); * m_outstream.WriteLine("\t\t{"); * } * else * {*/ m_outstream.WriteLine(" {"); m_outstream.WriteLine("\t\tif (false == yy_eof_done) {"); m_outstream.Write(new string(m_spec.m_eof_code, 0, m_spec.m_eof_read)); m_outstream.WriteLine("\t\t}"); m_outstream.WriteLine("\t\tyy_eof_done = true;"); m_outstream.WriteLine("\t}"); } emit_states(); /* Function: yybegin */ m_outstream.WriteLine("\tprivate void yybegin (int state) {"); m_outstream.WriteLine("\t\tyy_lexical_state = state;"); m_outstream.WriteLine("\t}"); /* Function: yy_advance */ m_outstream.WriteLine("\tprivate int yy_advance ()"); //SI: m_outstream.WriteLine("\t\tthrows java.io.IOException {"); /*m_outstream.WriteLine("\t\t{");*/ m_outstream.WriteLine("\t{"); m_outstream.WriteLine("\t\tint next_read;"); m_outstream.WriteLine("\t\tint i;"); m_outstream.WriteLine("\t\tint j;"); m_outstream.WriteLine(); m_outstream.WriteLine("\t\tif (yy_buffer_index < yy_buffer_read) {"); m_outstream.WriteLine("\t\t\treturn yy_buffer[yy_buffer_index++];"); /*m_outstream.WriteLine("\t\t\t++yy_buffer_index;");*/ m_outstream.WriteLine("\t\t}"); m_outstream.WriteLine(); m_outstream.WriteLine("\t\tif (0 != yy_buffer_start) {"); m_outstream.WriteLine("\t\t\ti = yy_buffer_start;"); m_outstream.WriteLine("\t\t\tj = 0;"); m_outstream.WriteLine("\t\t\twhile (i < yy_buffer_read) {"); m_outstream.WriteLine("\t\t\t\tyy_buffer[j] = yy_buffer[i];"); m_outstream.WriteLine("\t\t\t\t++i;"); m_outstream.WriteLine("\t\t\t\t++j;"); m_outstream.WriteLine("\t\t\t}"); m_outstream.WriteLine("\t\t\tyy_buffer_end = yy_buffer_end - yy_buffer_start;"); m_outstream.WriteLine("\t\t\tyy_buffer_start = 0;"); m_outstream.WriteLine("\t\t\tyy_buffer_read = j;"); m_outstream.WriteLine("\t\t\tyy_buffer_index = j;"); m_outstream.WriteLine("\t\t\tnext_read = yy_reader.Read(yy_buffer,"); m_outstream.WriteLine("\t\t\t\t\tyy_buffer_read,"); m_outstream.WriteLine("\t\t\t\t\tyy_buffer.Length - yy_buffer_read);"); m_outstream.WriteLine("\t\t\tif ( next_read<=0) {"); m_outstream.WriteLine("\t\t\t\treturn YY_EOF;"); m_outstream.WriteLine("\t\t\t}"); m_outstream.WriteLine("\t\t\tyy_buffer_read = yy_buffer_read + next_read;"); m_outstream.WriteLine("\t\t}"); m_outstream.WriteLine(); m_outstream.WriteLine("\t\twhile (yy_buffer_index >= yy_buffer_read) {"); m_outstream.WriteLine("\t\t\tif (yy_buffer_index >= yy_buffer.Length) {"); m_outstream.WriteLine("\t\t\t\tyy_buffer = yy_double(yy_buffer);"); m_outstream.WriteLine("\t\t\t}"); m_outstream.WriteLine("\t\t\tnext_read = yy_reader.Read(yy_buffer,"); m_outstream.WriteLine("\t\t\t\t\tyy_buffer_read,"); m_outstream.WriteLine("\t\t\t\t\tyy_buffer.Length - yy_buffer_read);"); m_outstream.WriteLine("\t\t\tif ( next_read<=0) {"); m_outstream.WriteLine("\t\t\t\treturn YY_EOF;"); m_outstream.WriteLine("\t\t\t}"); m_outstream.WriteLine("\t\t\tyy_buffer_read = yy_buffer_read + next_read;"); m_outstream.WriteLine("\t\t}"); m_outstream.WriteLine("\t\treturn yy_buffer[yy_buffer_index++];"); m_outstream.WriteLine("\t}"); /* Function: yy_move_end */ m_outstream.WriteLine("\tprivate void yy_move_end () {"); m_outstream.WriteLine("\t\tif (yy_buffer_end > yy_buffer_start &&"); m_outstream.WriteLine("\t\t '\\n' == yy_buffer[yy_buffer_end-1])"); m_outstream.WriteLine("\t\t\tyy_buffer_end--;"); m_outstream.WriteLine("\t\tif (yy_buffer_end > yy_buffer_start &&"); m_outstream.WriteLine("\t\t '\\r' == yy_buffer[yy_buffer_end-1])"); m_outstream.WriteLine("\t\t\tyy_buffer_end--;"); m_outstream.WriteLine("\t}"); /* Function: yy_mark_start */ m_outstream.WriteLine("\tprivate bool yy_last_was_cr=false;"); m_outstream.WriteLine("\tprivate void yy_mark_start () {"); if (m_spec.m_count_lines || true == m_spec.m_count_chars) { if (m_spec.m_count_lines) { m_outstream.WriteLine("\t\tint i;"); m_outstream.WriteLine("\t\tfor (i = yy_buffer_start; " + "i < yy_buffer_index; ++i) {"); m_outstream.WriteLine("\t\t\tif ('\\n' == yy_buffer[i] && !yy_last_was_cr) {"); m_outstream.WriteLine("\t\t\t\t++yyline;"); m_outstream.WriteLine("\t\t\t}"); m_outstream.WriteLine("\t\t\tif ('\\r' == yy_buffer[i]) {"); m_outstream.WriteLine("\t\t\t\t++yyline;"); m_outstream.WriteLine("\t\t\t\tyy_last_was_cr=true;"); m_outstream.WriteLine("\t\t\t} else yy_last_was_cr=false;"); m_outstream.WriteLine("\t\t}"); } if (m_spec.m_count_chars) { m_outstream.WriteLine("\t\tyychar = yychar"); m_outstream.WriteLine("\t\t\t+ yy_buffer_index - yy_buffer_start;"); } } m_outstream.WriteLine("\t\tyy_buffer_start = yy_buffer_index;"); m_outstream.WriteLine("\t}"); /* Function: yy_mark_end */ m_outstream.WriteLine("\tprivate void yy_mark_end () {"); m_outstream.WriteLine("\t\tyy_buffer_end = yy_buffer_index;"); m_outstream.WriteLine("\t}"); /* Function: yy_to_mark */ m_outstream.WriteLine("\tprivate void yy_to_mark () {"); m_outstream.WriteLine("\t\tyy_buffer_index = yy_buffer_end;"); m_outstream.WriteLine("\t\tyy_at_bol = " + "(yy_buffer_end > yy_buffer_start) &&"); m_outstream.WriteLine("\t\t " + "('\\r' == yy_buffer[yy_buffer_end-1] ||"); m_outstream.WriteLine("\t\t " + " '\\n' == yy_buffer[yy_buffer_end-1] ||"); m_outstream.WriteLine("\t\t " + /* unicode LS */ " 2028/*LS*/ == yy_buffer[yy_buffer_end-1] ||"); m_outstream.WriteLine("\t\t " + /* unicode PS */ " 2029/*PS*/ == yy_buffer[yy_buffer_end-1]);"); m_outstream.WriteLine("\t}"); /* Function: yytext */ m_outstream.WriteLine("\tprivate string yytext () {"); m_outstream.WriteLine("\t\treturn (new string(yy_buffer,"); m_outstream.WriteLine("\t\t\tyy_buffer_start,"); m_outstream.WriteLine("\t\t\tyy_buffer_end - yy_buffer_start));"); m_outstream.WriteLine("\t}"); /* Function: yylength */ m_outstream.WriteLine("\tprivate int yylength () {"); m_outstream.WriteLine("\t\treturn yy_buffer_end - yy_buffer_start;"); m_outstream.WriteLine("\t}"); /* Function: yy_double */ m_outstream.WriteLine("\tprivate char[] yy_double (char[] buf) {"); m_outstream.WriteLine("\t\tint i;"); m_outstream.WriteLine("\t\tchar[] newbuf;"); m_outstream.WriteLine("\t\tnewbuf = new char[2*buf.Length];"); m_outstream.WriteLine("\t\tfor (i = 0; i < buf.Length; ++i) {"); m_outstream.WriteLine("\t\t\tnewbuf[i] = buf[i];"); m_outstream.WriteLine("\t\t}"); m_outstream.WriteLine("\t\treturn newbuf;"); m_outstream.WriteLine("\t}"); /* Function: yy_error */ m_outstream.WriteLine("\tprivate const int YY_E_INTERNAL = 0;"); m_outstream.WriteLine("\tprivate const int YY_E_MATCH = 1;"); m_outstream.WriteLine("\tprivate string[] yy_error_string = {"); m_outstream.WriteLine("\t\t\"Error: Internal error.\\n\","); m_outstream.WriteLine("\t\t\"Error: Unmatched input.\\n\""); m_outstream.WriteLine("\t};"); m_outstream.WriteLine("\tprivate void yy_error (int code,bool fatal) {"); m_outstream.WriteLine("\t\t System.Console.Write(yy_error_string[code]);"); m_outstream.WriteLine("\t\t System.Console.Out.Flush();"); m_outstream.WriteLine("\t\tif (fatal) {"); m_outstream.WriteLine("\t\t\tthrow new System.Exception(\"Fatal Error.\\n\");"); m_outstream.WriteLine("\t\t}"); m_outstream.WriteLine("\t}"); // Function: private int [][] unpackFromString(int size1, int size2, string st) // Added 6/24/98 Raimondas Lencevicius // May be made more efficient by replacing string operations // Assumes correctly formed input string. Performs no error checking m_outstream.WriteLine("\tprivate static int[][] unpackFromString" + "(int size1, int size2, string st) {"); m_outstream.WriteLine("\t\tint colonIndex = -1;"); m_outstream.WriteLine("\t\tstring lengthString;"); m_outstream.WriteLine("\t\tint sequenceLength = 0;"); m_outstream.WriteLine("\t\tint sequenceInteger = 0;"); m_outstream.WriteLine(); m_outstream.WriteLine("\t\tint commaIndex;"); m_outstream.WriteLine("\t\tstring workString;"); m_outstream.WriteLine(); m_outstream.WriteLine("\t\tint[][] res = new int[size1][];"); m_outstream.WriteLine("\t\tfor(int i=0;i<size1;i++) res[i]=new int[size2];"); m_outstream.WriteLine("\t\tfor (int i= 0; i < size1; i++) {"); m_outstream.WriteLine("\t\t\tfor (int j= 0; j < size2; j++) {"); m_outstream.WriteLine("\t\t\t\tif (sequenceLength != 0) {"); m_outstream.WriteLine("\t\t\t\t\tres[i][j] = sequenceInteger;"); m_outstream.WriteLine("\t\t\t\t\tsequenceLength--;"); m_outstream.WriteLine("\t\t\t\t\tcontinue;"); m_outstream.WriteLine("\t\t\t\t}"); m_outstream.WriteLine("\t\t\t\tcommaIndex = st.IndexOf(',');"); m_outstream.WriteLine("\t\t\t\tworkString = (commaIndex==-1) ? st :"); m_outstream.WriteLine("\t\t\t\t\tst.Substring(0, commaIndex);"); m_outstream.WriteLine("\t\t\t\tst = st.Substring(commaIndex+1);"); m_outstream.WriteLine("\t\t\t\tcolonIndex = workString.IndexOf(':');"); m_outstream.WriteLine("\t\t\t\tif (colonIndex == -1) {"); m_outstream.WriteLine("\t\t\t\t\tres[i][j]=System.Int32.Parse(workString);"); m_outstream.WriteLine("\t\t\t\t\tcontinue;"); m_outstream.WriteLine("\t\t\t\t}"); m_outstream.WriteLine("\t\t\t\tlengthString ="); m_outstream.WriteLine("\t\t\t\t\tworkString.Substring(colonIndex+1);"); m_outstream.WriteLine("\t\t\t\tsequenceLength=" + "System.Int32.Parse(lengthString);"); m_outstream.WriteLine("\t\t\t\tworkString=" + "workString.Substring(0,colonIndex);"); m_outstream.WriteLine("\t\t\t\tsequenceInteger=" + "System.Int32.Parse(workString);"); m_outstream.WriteLine("\t\t\t\tres[i][j] = sequenceInteger;"); m_outstream.WriteLine("\t\t\t\tsequenceLength--;"); m_outstream.WriteLine("\t\t\t}"); m_outstream.WriteLine("\t\t}"); m_outstream.WriteLine("\t\treturn res;"); m_outstream.WriteLine("\t}"); }
/*************************************************************** * Function: emit_table * Description: Emits transition table. **************************************************************/ private void emit_table ( ) { int i; int elem; int size; CDTrans dtrans; bool is_start; bool is_end; CAccept accept; if (CUtility.DEBUG) { CUtility.ASSERT(null != m_spec); CUtility.ASSERT(null != m_outstream); } m_outstream.WriteLine("\tprivate int[] yy_acpt = {"); size = m_spec.m_accept_vector.Count; for (elem = 0; elem < size; ++elem) { accept = (CAccept)m_spec.m_accept_vector.elementAt(elem); m_outstream.Write("\t\t/* " + elem + " */ "); if (null != accept) { is_start = (0 != (m_spec.m_anchor_array[elem] & CSpec.START)); is_end = (0 != (m_spec.m_anchor_array[elem] & CSpec.END)); if (is_start && true == is_end) { m_outstream.Write("YY_START | YY_END"); } else if (is_start) { m_outstream.Write("YY_START"); } else if (is_end) { m_outstream.Write("YY_END"); } else { m_outstream.Write("YY_NO_ANCHOR"); } } else { m_outstream.Write("YY_NOT_ACCEPT"); } if (elem < size - 1) { m_outstream.Write(","); } m_outstream.WriteLine(); } m_outstream.WriteLine("\t};"); // CSA: modified yy_cmap to use string packing 9-Aug-1999 int[] yy_cmap = new int[m_spec.m_ccls_map.Length]; for (i = 0; i < m_spec.m_ccls_map.Length; ++i) { yy_cmap[i] = m_spec.m_col_map[m_spec.m_ccls_map[i]]; } m_outstream.Write("\tprivate int[] yy_cmap = unpackFromString("); emit_table_as_string(new int[][] { yy_cmap }); m_outstream.WriteLine(")[0];"); m_outstream.WriteLine(); // CSA: modified yy_rmap to use string packing 9-Aug-1999 m_outstream.Write("\tprivate int[] yy_rmap = unpackFromString("); emit_table_as_string(new int[][] { m_spec.m_row_map }); m_outstream.WriteLine(")[0];"); m_outstream.WriteLine(); // 6/24/98 Raimondas Lencevicius // modified to use // int[][] unpackFromString(int size1, int size2, string st) size = m_spec.m_dtrans_vector.size(); int[][] yy_nxt = new int[size][]; for (elem = 0; elem < size; elem++) { dtrans = (CDTrans)m_spec.m_dtrans_vector.elementAt(elem); CUtility.ASSERT(dtrans.m_dtrans.Length == m_spec.m_dtrans_ncols); yy_nxt[elem] = dtrans.m_dtrans; } m_outstream.Write ("\tprivate int[][] yy_nxt = unpackFromString("); emit_table_as_string(yy_nxt); m_outstream.WriteLine(");"); m_outstream.WriteLine(); }
/*************************************************************** * Function: emit_construct * Description: Emits constructor, member variables, * and constants. **************************************************************/ private void emit_construct ( ) { if (CUtility.DEBUG) { CUtility.ASSERT(null != m_spec); CUtility.ASSERT(null != m_outstream); } /* Constants */ m_outstream.WriteLine("\tprivate const int YY_BUFFER_SIZE = 512;"); m_outstream.WriteLine("\tprivate const int YY_F = -1;"); m_outstream.WriteLine("\tprivate const int YY_NO_STATE = -1;"); m_outstream.WriteLine("\tprivate const int YY_NOT_ACCEPT = 0;"); m_outstream.WriteLine("\tprivate const int YY_START = 1;"); m_outstream.WriteLine("\tprivate const int YY_END = 2;"); m_outstream.WriteLine("\tprivate const int YY_NO_ANCHOR = 4;"); // internal m_outstream.WriteLine("\tprivate const int YY_BOL = " + m_spec.BOL + ";"); m_outstream.WriteLine("\tprivate const int YY_EOF = " + m_spec.EOF + ";"); // external if (m_spec.m_integer_type || true == m_spec.m_yyeof) { m_outstream.WriteLine("\tpublic const int YYEOF = -1;"); } /* User specified class code. */ if (null != m_spec.m_class_code) { m_outstream.Write(new string(m_spec.m_class_code, 0, m_spec.m_class_read)); } /* Member Variables */ m_outstream.WriteLine("\tprivate System.IO.TextReader yy_reader;"); m_outstream.WriteLine("\tprivate int yy_buffer_index;"); m_outstream.WriteLine("\tprivate int yy_buffer_read;"); m_outstream.WriteLine("\tprivate int yy_buffer_start;"); m_outstream.WriteLine("\tprivate int yy_buffer_end;"); m_outstream.WriteLine("\tprivate char[] yy_buffer;"); if (m_spec.m_count_chars) { m_outstream.WriteLine("\tprivate int yychar;"); } if (m_spec.m_count_lines) { m_outstream.WriteLine("\tprivate int yyline;"); } m_outstream.WriteLine("\tprivate bool yy_at_bol;"); m_outstream.WriteLine("\tprivate int yy_lexical_state;"); /*if (m_spec.m_count_lines || true == m_spec.m_count_chars) * { * m_outstream.WriteLine("\tprivate int yy_buffer_prev_start;"); * }*/ m_outstream.WriteLine(); /* Function: first constructor (Reader) */ m_outstream.Write("\t"); m_outstream.Write("public "); m_outstream.Write(new string(m_spec.m_class_name)); m_outstream.Write(" (System.IO.TextReader yy_reader1) : this()"); //SI: /*if (null != m_spec.m_init_throw_code) * { * m_outstream.WriteLine(); * m_outstream.Write("\t\tthrows "); * m_outstream.Write(new string(m_spec.m_init_throw_code,0, * m_spec.m_init_throw_read)); * m_outstream.WriteLine(); * m_outstream.WriteLine("\t\t{"); * } * else * {*/ m_outstream.WriteLine(" {"); //SI: m_outstream.WriteLine("\t\tthis ();"); m_outstream.WriteLine("\t\tif (null == yy_reader1) {"); m_outstream.WriteLine("\t\t\tthrow (new System.Exception(\"Error: Bad input " + "stream initializer.\"));"); m_outstream.WriteLine("\t\t}"); m_outstream.WriteLine("\t\tyy_reader = yy_reader1;"); m_outstream.WriteLine("\t}"); m_outstream.WriteLine(); /* * m_outstream.Write("\t"); * if (true == m_spec.m_public) * { * m_outstream.Write("public "); * } * m_outstream.Write(new string(m_spec.m_class_name)); * m_outstream.Write(" (java.io.InputStream instream)"); * * if (null != m_spec.m_init_throw_code) * { * m_outstream.WriteLine(); * m_outstream.Write("\t\tthrows "); * m_outstream.WriteLine(new string(m_spec.m_init_throw_code,0, * m_spec.m_init_throw_read)); * m_outstream.WriteLine("\t\t{"); * } * else * { * m_outstream.WriteLine(" {"); * } * * m_outstream.WriteLine("\t\tthis ();"); * m_outstream.WriteLine("\t\tif (null == instream) {"); * m_outstream.WriteLine("\t\t\tthrow (new Error(\"Error: Bad input " + "stream initializer.\"));"); + m_outstream.WriteLine("\t\t}"); + m_outstream.WriteLine("\t\tyy_reader = new java.io.BufferedReader(new java.io.InputStreamReader(instream));"); + m_outstream.WriteLine("\t}"); + m_outstream.WriteLine(); */ /* Function: third, private constructor - only for internal use */ m_outstream.Write("\tprivate "); m_outstream.Write(new string(m_spec.m_class_name)); m_outstream.Write(" ()"); //SI:throw code not necceasary /*if (null != m_spec.m_init_throw_code) * { * m_outstream.WriteLine(); * m_outstream.Write("\t\tthrows "); * m_outstream.WriteLine(new string(m_spec.m_init_throw_code,0, * m_spec.m_init_throw_read)); * m_outstream.WriteLine("\t\t{"); * } * else * {*/ m_outstream.WriteLine(" {"); m_outstream.WriteLine("\t\tyy_buffer = new char[YY_BUFFER_SIZE];"); m_outstream.WriteLine("\t\tyy_buffer_read = 0;"); m_outstream.WriteLine("\t\tyy_buffer_index = 0;"); m_outstream.WriteLine("\t\tyy_buffer_start = 0;"); m_outstream.WriteLine("\t\tyy_buffer_end = 0;"); if (m_spec.m_count_chars) { m_outstream.WriteLine("\t\tyychar = 0;"); } if (m_spec.m_count_lines) { m_outstream.WriteLine("\t\tyyline = 0;"); } m_outstream.WriteLine("\t\tyy_at_bol = true;"); m_outstream.WriteLine("\t\tyy_lexical_state = YYINITIAL;"); /*if (m_spec.m_count_lines || true == m_spec.m_count_chars) * { * m_outstream.WriteLine("\t\tyy_buffer_prev_start = 0;"); * }*/ /* User specified constructor code. */ if (null != m_spec.m_init_code) { m_outstream.Write(new string(m_spec.m_init_code, 0, m_spec.m_init_read)); } m_outstream.WriteLine("\t}"); m_outstream.WriteLine(); }
/*************************************************************** * Function: term * Description: Recursive descent regular expression parser. **************************************************************/ private void term ( CNfaPair pair ) { CNfa start; bool isAlphaL; // int c; if (CUtility.DESCENT_DEBUG) { CUtility.enter("term", m_spec.m_lexeme, m_spec.m_current_token); } if (CLexGen.OPEN_PAREN == m_spec.m_current_token) { m_lexGen.advance(); expr(pair); if (CLexGen.CLOSE_PAREN == m_spec.m_current_token) { m_lexGen.advance(); } else { CError.parse_error(CError.E_SYNTAX, m_input.m_line_number); } } else { start = CAlloc.newCNfa(m_spec); pair.m_start = start; start.m_next = CAlloc.newCNfa(m_spec); pair.m_end = start.m_next; if (CLexGen.L == m_spec.m_current_token && Char.IsLetter(m_spec.m_lexeme)) { isAlphaL = true; } else { isAlphaL = false; } if (false == (CLexGen.ANY == m_spec.m_current_token || CLexGen.CCL_START == m_spec.m_current_token || (m_spec.m_ignorecase && isAlphaL))) { start.m_edge = m_spec.m_lexeme; m_lexGen.advance(); } else { start.m_edge = CNfa.CCL; start.m_set = new CSet(); /* Match case-insensitive letters using character class. */ if (m_spec.m_ignorecase && isAlphaL) { start.m_set.addncase(m_spec.m_lexeme); } /* Match dot (.) using character class. */ else if (CLexGen.ANY == m_spec.m_current_token) { start.m_set.add('\n'); start.m_set.add('\r'); // CSA: exclude BOL and EOF from character classes start.m_set.add(m_spec.BOL); start.m_set.add(m_spec.EOF); start.m_set.complement(); } else { m_lexGen.advance(); if (CLexGen.AT_BOL == m_spec.m_current_token) { m_lexGen.advance(); // CSA: exclude BOL and EOF from character classes start.m_set.add(m_spec.BOL); start.m_set.add(m_spec.EOF); start.m_set.complement(); } if (false == (CLexGen.CCL_END == m_spec.m_current_token)) { dodash(start.m_set); } /*else * { * for (c = 0; c <= ' '; ++c) * { * start.m_set.add((byte) c); * } * }*/ } m_lexGen.advance(); } } if (CUtility.DESCENT_DEBUG) { CUtility.leave("term", m_spec.m_lexeme, m_spec.m_current_token); } }
/*************************************************************** * Function: getLine * Description: Returns true on EOF, false otherwise. * Guarantees not to return a blank line, or a line * of zero length. **************************************************************/ public bool getLine ( ) { string lineStr; int elem; /* Has EOF already been reached? */ if (m_eof_reached) { return(EOF); } /* Pushback current line? */ if (m_pushback_line) { m_pushback_line = false; /* Check for empty line. */ for (elem = 0; elem < m_line_read; ++elem) { if (false == CUtility.isspace(m_line[elem])) { break; } } /* Nonempty? */ if (elem < m_line_read) { m_line_index = 0; return(NOT_EOF); } } while (true) { if (null == (lineStr = m_input.ReadLine())) { m_eof_reached = true; m_line_index = 0; return(EOF); } m_line = (lineStr + "\n").ToCharArray(); m_line_read = m_line.Length; ++m_line_number; /* Check for empty lines and discard them. */ elem = 0; while (CUtility.isspace(m_line[elem])) { ++elem; if (elem == m_line_read) { break; } } if (elem < m_line_read) { break; } } m_line_index = 0; return(NOT_EOF); }
/*************************************************************** * Function: emit_driver * Description: **************************************************************/ private void emit_driver ( ) { if (CUtility.DEBUG) { CUtility.ASSERT(null != m_spec); CUtility.ASSERT(null != m_outstream); } emit_table(); if (m_spec.m_integer_type) { m_outstream.Write("\tpublic int "); m_outstream.Write(new string(m_spec.m_function_name)); m_outstream.WriteLine(" ()"); } else if (m_spec.m_intwrap_type) { m_outstream.Write("\tpublic int "); m_outstream.Write(new string(m_spec.m_function_name)); m_outstream.WriteLine(" ()"); } else { m_outstream.Write("\tpublic "); m_outstream.Write(new string(m_spec.m_type_name)); m_outstream.Write(" "); m_outstream.Write(new string(m_spec.m_function_name)); m_outstream.WriteLine(" ()"); } /*m_outstream.WriteLine("\t\tthrows java.io.IOException {");*/ // m_outstream.Write("\t\tthrows java.io.IOException"); if (null != m_spec.m_yylex_throw_code) { m_outstream.Write(", "); m_outstream.Write(new string(m_spec.m_yylex_throw_code, 0, m_spec.m_yylex_throw_read)); m_outstream.WriteLine(); m_outstream.WriteLine("\t\t{"); } else { m_outstream.WriteLine(" {"); } m_outstream.WriteLine("\t\tint yy_lookahead;"); m_outstream.WriteLine("\t\tint yy_anchor = YY_NO_ANCHOR;"); /*m_outstream.WriteLine("\t\tint yy_state " + "= yy_initial_dtrans(yy_lexical_state);");*/ m_outstream.WriteLine("\t\tint yy_state " + "= yy_state_dtrans[yy_lexical_state];"); m_outstream.WriteLine("\t\tint yy_next_state = YY_NO_STATE;"); /*m_outstream.WriteLine("\t\tint yy_prev_stave = YY_NO_STATE;");*/ m_outstream.WriteLine("\t\tint yy_last_accept_state = YY_NO_STATE;"); m_outstream.WriteLine("\t\tbool yy_initial = true;"); m_outstream.WriteLine("\t\tint yy_this_accept;"); m_outstream.WriteLine(); m_outstream.WriteLine("\t\tyy_mark_start();"); /*m_outstream.WriteLine("\t\tyy_this_accept = yy_accept(yy_state);");*/ m_outstream.WriteLine("\t\tyy_this_accept = yy_acpt[yy_state];"); m_outstream.WriteLine("\t\tif (YY_NOT_ACCEPT != yy_this_accept) {"); m_outstream.WriteLine("\t\t\tyy_last_accept_state = yy_state;"); m_outstream.WriteLine("\t\t\tyy_mark_end();"); m_outstream.WriteLine("\t\t}"); if (NOT_EDBG) { m_outstream.WriteLine("\t\tSystem.Console.WriteLine(\"Begin\");"); } m_outstream.WriteLine("\t\twhile (true) {"); m_outstream.WriteLine("\t\t\tif (yy_initial && yy_at_bol) " + "yy_lookahead = YY_BOL;"); m_outstream.WriteLine("\t\t\telse yy_lookahead = yy_advance();"); m_outstream.WriteLine("\t\t\tyy_next_state = YY_F;"); /*m_outstream.WriteLine("\t\t\t\tyy_next_state = " + "yy_next(yy_state,yy_lookahead);");*/ m_outstream.WriteLine("\t\t\tyy_next_state = " + "yy_nxt[yy_rmap[yy_state]][yy_cmap[yy_lookahead]];"); if (NOT_EDBG) { m_outstream.WriteLine("System.Console.WriteLine(\"Current state: \"" + " + yy_state"); m_outstream.WriteLine("+ \"\tCurrent input: \""); m_outstream.WriteLine(" + ((char) yy_lookahead));"); } if (NOT_EDBG) { m_outstream.WriteLine("\t\t\tSystem.Console.WriteLine(\"State = \"" + "+ yy_state);"); m_outstream.WriteLine("\t\t\tSystem.Console.WriteLine(\"Accepting status = \"" + "+ yy_this_accept);"); m_outstream.WriteLine("\t\t\tSystem.Console.WriteLine(\"Last accepting state = \"" + "+ yy_last_accept_state);"); m_outstream.WriteLine("\t\t\tSystem.Console.WriteLine(\"Next state = \"" + "+ yy_next_state);"); m_outstream.WriteLine("\t\t\tSystem.Console.WriteLine(\"Lookahead input = \"" + "+ ((char) yy_lookahead));"); } // handle bare EOF. m_outstream.WriteLine("\t\t\tif (YY_EOF == yy_lookahead " + "&& true == yy_initial) {"); if (null != m_spec.m_eof_code) { m_outstream.WriteLine("\t\t\t\tyy_do_eof();"); } if (true == m_spec.m_integer_type) { m_outstream.WriteLine("\t\t\t\treturn YYEOF;"); } else if (null != m_spec.m_eof_value_code) { m_outstream.Write(new string(m_spec.m_eof_value_code, 0, m_spec.m_eof_value_read)); } else { m_outstream.WriteLine("\t\t\t\treturn null;"); } m_outstream.WriteLine("\t\t\t}"); m_outstream.WriteLine("\t\t\tif (YY_F != yy_next_state) {"); m_outstream.WriteLine("\t\t\t\tyy_state = yy_next_state;"); m_outstream.WriteLine("\t\t\t\tyy_initial = false;"); /*m_outstream.WriteLine("\t\t\t\tyy_this_accept = yy_accept(yy_state);");*/ m_outstream.WriteLine("\t\t\t\tyy_this_accept = yy_acpt[yy_state];"); m_outstream.WriteLine("\t\t\t\tif (YY_NOT_ACCEPT != yy_this_accept) {"); m_outstream.WriteLine("\t\t\t\t\tyy_last_accept_state = yy_state;"); m_outstream.WriteLine("\t\t\t\t\tyy_mark_end();"); m_outstream.WriteLine("\t\t\t\t}"); /*m_outstream.WriteLine("\t\t\t\tyy_prev_state = yy_state;");*/ /*m_outstream.WriteLine("\t\t\t\tyy_state = yy_next_state;");*/ m_outstream.WriteLine("\t\t\t}"); m_outstream.WriteLine("\t\t\telse {"); m_outstream.WriteLine("\t\t\t\tif (YY_NO_STATE == yy_last_accept_state) {"); m_outstream.WriteLine("\t\t\t\t\tthrow (new System.Exception(\"Lexical Error: Unmatched Input.\"));"); m_outstream.WriteLine("\t\t\t\t}"); m_outstream.WriteLine("\t\t\t\telse {"); m_outstream.WriteLine("\t\t\t\t\tyy_anchor = yy_acpt[yy_last_accept_state];"); m_outstream.WriteLine("\t\t\t\t\tif (0 != (YY_END & yy_anchor)) {"); m_outstream.WriteLine("\t\t\t\t\t\tyy_move_end();"); m_outstream.WriteLine("\t\t\t\t\t}"); m_outstream.WriteLine("\t\t\t\t\tyy_to_mark();"); m_outstream.WriteLine("\t\t\t\t\tswitch (yy_last_accept_state) {"); emit_actions("\t\t\t\t\t"); m_outstream.WriteLine("\t\t\t\t\tdefault:"); //SI:break added m_outstream.WriteLine("\t\t\t\t\t\tyy_error(YY_E_INTERNAL,false);break;"); /*m_outstream.WriteLine("\t\t\t\t\t\treturn null;");*/ //SI:removed // m_outstream.WriteLine("\t\t\t\t\tcase -1:"); m_outstream.WriteLine("\t\t\t\t\t}"); m_outstream.WriteLine("\t\t\t\t\tyy_initial = true;"); m_outstream.WriteLine("\t\t\t\t\tyy_state " + "= yy_state_dtrans[yy_lexical_state];"); m_outstream.WriteLine("\t\t\t\t\tyy_next_state = YY_NO_STATE;"); /*m_outstream.WriteLine("\t\t\t\t\tyy_prev_state = YY_NO_STATE;");*/ m_outstream.WriteLine("\t\t\t\t\tyy_last_accept_state = YY_NO_STATE;"); m_outstream.WriteLine("\t\t\t\t\tyy_mark_start();"); /*m_outstream.WriteLine("\t\t\t\t\tyy_this_accept = yy_accept(yy_state);");*/ m_outstream.WriteLine("\t\t\t\t\tyy_this_accept = yy_acpt[yy_state];"); m_outstream.WriteLine("\t\t\t\t\tif (YY_NOT_ACCEPT != yy_this_accept) {"); m_outstream.WriteLine("\t\t\t\t\t\tyy_last_accept_state = yy_state;"); m_outstream.WriteLine("\t\t\t\t\t\tyy_mark_end();"); m_outstream.WriteLine("\t\t\t\t\t}"); m_outstream.WriteLine("\t\t\t\t}"); m_outstream.WriteLine("\t\t\t}"); m_outstream.WriteLine("\t\t}"); m_outstream.WriteLine("\t}"); }
/*************************************************************** * Function: rule * Description: Recursive descent regular expression parser. **************************************************************/ private CNfa rule ( ) { CNfaPair pair; //CNfa p; CNfa start = null; CNfa end = null; int anchor = CSpec.NONE; if (CUtility.DESCENT_DEBUG) { CUtility.enter("rule", m_spec.m_lexeme, m_spec.m_current_token); } pair = CAlloc.newCNfaPair(); if (CLexGen.AT_BOL == m_spec.m_current_token) { anchor = anchor | CSpec.START; m_lexGen.advance(); expr(pair); // CSA: fixed beginning-of-line operator. 8-aug-1999 start = CAlloc.newCNfa(m_spec); start.m_edge = m_spec.BOL; start.m_next = pair.m_start; end = pair.m_end; } else { expr(pair); start = pair.m_start; end = pair.m_end; } if (CLexGen.AT_EOL == m_spec.m_current_token) { m_lexGen.advance(); // CSA: fixed end-of-line operator. 8-aug-1999 CNfaPair nlpair = CAlloc.newNLPair(m_spec); end.m_next = CAlloc.newCNfa(m_spec); end.m_next.m_next = nlpair.m_start; end.m_next.m_next2 = CAlloc.newCNfa(m_spec); end.m_next.m_next2.m_edge = m_spec.EOF; end.m_next.m_next2.m_next = nlpair.m_end; end = nlpair.m_end; anchor = anchor | CSpec.END; } /* Check for null rules. Charles Fischer found this bug. [CSA] */ if (end == null) { CError.parse_error(CError.E_ZERO, m_input.m_line_number); } /* Handle end of regular expression. See page 103. */ end.m_accept = m_lexGen.packAccept(); end.m_anchor = anchor; /* Begin: Removed for states. */ /*m_lexGen.advance();*/ /* End: Removed for states. */ if (CUtility.DESCENT_DEBUG) { CUtility.leave("rule", m_spec.m_lexeme, m_spec.m_current_token); } return(start); }
/*************************************************************** * Function: machine * Description: Recursive descent regular expression parser. **************************************************************/ private CNfa machine ( ) { CNfa start; CNfa p; SparseBitSet states; if (CUtility.DESCENT_DEBUG) { CUtility.enter("machine", m_spec.m_lexeme, m_spec.m_current_token); } start = CAlloc.newCNfa(m_spec); p = start; states = m_lexGen.getStates(); /* Begin: Added for states. */ m_spec.m_current_token = CLexGen.EOS; m_lexGen.advance(); /* End: Added for states. */ if (CLexGen.END_OF_INPUT != m_spec.m_current_token) // CSA fix. { p.m_next = rule(); processStates(states, p.m_next); } while (CLexGen.END_OF_INPUT != m_spec.m_current_token) { /* Make state changes HERE. */ states = m_lexGen.getStates(); /* Begin: Added for states. */ m_lexGen.advance(); if (CLexGen.END_OF_INPUT == m_spec.m_current_token) { break; } /* End: Added for states. */ p.m_next2 = CAlloc.newCNfa(m_spec); p = p.m_next2; p.m_next = rule(); processStates(states, p.m_next); } // CSA: add pseudo-rules for BOL and EOF SparseBitSet all_states = new SparseBitSet(); for (int i = 0; i < m_spec.m_states.Count; ++i) { all_states.Set(i); } p.m_next2 = CAlloc.newCNfa(m_spec); p = p.m_next2; p.m_next = CAlloc.newCNfa(m_spec); p.m_next.m_edge = CNfa.CCL; p.m_next.m_next = CAlloc.newCNfa(m_spec); p.m_next.m_set = new CSet(); p.m_next.m_set.add(m_spec.BOL); p.m_next.m_set.add(m_spec.EOF); p.m_next.m_next.m_accept = // do-nothing accept rule new CAccept(new char[0], 0, m_input.m_line_number + 1); processStates(all_states, p.m_next); // CSA: done. if (CUtility.DESCENT_DEBUG) { CUtility.leave("machine", m_spec.m_lexeme, m_spec.m_current_token); } return(start); }
/*************************************************************** * Function: make_dtrans * Description: Creates uncompressed CDTrans transition table. **************************************************************/ private void make_dtrans ( ) /* throws java.lang.CloneNotSupportedException*/ { //CDfa next; CDfa dfa; CBunch bunch; int i; int nextstate; int size; CDTrans dtrans; CNfa nfa; int istate; int nstates; System.Console.Write("Working on DFA states."); /* Reference passing type and initializations. */ bunch = new CBunch(); m_unmarked_dfa = 0; /* Allocate mapping array. */ nstates = m_spec.m_state_rules.Length; m_spec.m_state_dtrans = new int[nstates]; for (istate = 0; nstates > istate; ++istate) { /* CSA bugfix: if we skip all zero size rules, then * an specification with no rules produces an illegal * lexer (0 states) instead of a lexer that rejects * everything (1 nonaccepting state). [27-Jul-1999] * if (0 == m_spec.m_state_rules[istate].size()) * { * m_spec.m_state_dtrans[istate] = CDTrans.F; * continue; * } */ /* Create start state and initialize fields. */ //SI:testing //bunch.m_nfa_set = (Vector) m_spec.m_state_rules[istate].Clone(); bunch.m_nfa_set = (Vector)((m_spec.m_state_rules[istate]).Clone()); sortStates(bunch.m_nfa_set); bunch.m_nfa_bit = new SparseBitSet(); /* Initialize bit Set. */ size = bunch.m_nfa_set.size(); for (i = 0; size > i; ++i) { nfa = (CNfa)bunch.m_nfa_set.elementAt(i); bunch.m_nfa_bit.Set(nfa.m_label); } bunch.m_accept = null; bunch.m_anchor = CSpec.NONE; bunch.m_accept_index = CUtility.INT_MAX; e_closure(bunch); add_to_dstates(bunch); m_spec.m_state_dtrans[istate] = m_spec.m_dtrans_vector.size(); /* Main loop of CDTrans creation. */ while (null != (dfa = get_unmarked())) { System.Console.Write("."); System.Console.Out.Flush(); if (CUtility.DEBUG) { CUtility.ASSERT(false == dfa.m_mark); } /* Get first unmarked node, then mark it. */ dfa.m_mark = true; /* Allocate new CDTrans, then initialize fields. */ dtrans = new CDTrans(m_spec.m_dtrans_vector.size(), m_spec); dtrans.m_accept = dfa.m_accept; dtrans.m_anchor = dfa.m_anchor; /* Set CDTrans array for each character transition. */ for (i = 0; i < m_spec.m_dtrans_ncols; ++i) { if (CUtility.DEBUG) { CUtility.ASSERT(0 <= i); CUtility.ASSERT(m_spec.m_dtrans_ncols > i); } /* Create new dfa Set by attempting character transition. */ move(dfa.m_nfa_set, dfa.m_nfa_bit, i, bunch); if (null != bunch.m_nfa_set) { e_closure(bunch); } if (CUtility.DEBUG) { CUtility.ASSERT((null == bunch.m_nfa_set && null == bunch.m_nfa_bit) || (null != bunch.m_nfa_set && null != bunch.m_nfa_bit)); } /* Create new state or Set state to empty. */ if (null == bunch.m_nfa_set) { nextstate = CDTrans.F; } else { nextstate = in_dstates(bunch); if (NOT_IN_DSTATES == nextstate) { nextstate = add_to_dstates(bunch); } } if (CUtility.DEBUG) { CUtility.ASSERT(nextstate < m_spec.m_dfa_states.Count); } dtrans.m_dtrans[i] = nextstate; } if (CUtility.DEBUG) { CUtility.ASSERT(m_spec.m_dtrans_vector.size() == dfa.m_label); } m_spec.m_dtrans_vector.addElement(dtrans); } } System.Console.WriteLine(); }
/*************************************************************** * Function: move * Description: Returns null if resulting NFA Set is empty. **************************************************************/ void move ( Vector nfa_set, SparseBitSet nfa_bit, int b, CBunch bunch ) { int size; int index; CNfa state; bunch.m_nfa_set = null; bunch.m_nfa_bit = null; size = nfa_set.Count; // System.Console.WriteLine(size); for (index = 0; index < size; ++index) { state = (CNfa)nfa_set.elementAt(index); // System.Console.WriteLine(index+" "+state.m_set); if (b == state.m_edge || (CNfa.CCL == state.m_edge && true == state.m_set.contains(b))) { // System.Console.WriteLine(state.m_edge+" "+b); if (null == bunch.m_nfa_set) { if (CUtility.DEBUG) { CUtility.ASSERT(null == bunch.m_nfa_bit); } bunch.m_nfa_set = new Vector(); /*bunch.m_nfa_bit * = new SparseBitSet(m_spec.m_nfa_states.size());*/ bunch.m_nfa_bit = new SparseBitSet(); } bunch.m_nfa_set.addElement(state.m_next); /*System.Console.WriteLine("Size of bitset: " + bunch.m_nfa_bit.size()); * System.Console.WriteLine("Reference index: " + state.m_next.m_label); * System.out.flush();*/ bunch.m_nfa_bit.Set(state.m_next.m_label); } } if (null != bunch.m_nfa_set) { if (CUtility.DEBUG) { CUtility.ASSERT(null != bunch.m_nfa_bit); } sortStates(bunch.m_nfa_set); } return; }
/*************************************************************** * Function: e_closure * Description: Alters and returns input Set. **************************************************************/ private void e_closure ( CBunch bunch ) { Stack nfa_stack; int size; int i; CNfa state; /* Debug checks. */ if (CUtility.DEBUG) { CUtility.ASSERT(null != bunch); CUtility.ASSERT(null != bunch.m_nfa_set); CUtility.ASSERT(null != bunch.m_nfa_bit); } bunch.m_accept = null; bunch.m_anchor = CSpec.NONE; bunch.m_accept_index = CUtility.INT_MAX; /* Create initial stack. */ nfa_stack = new Stack(); size = bunch.m_nfa_set.size(); for (i = 0; i < size; ++i) { state = (CNfa)bunch.m_nfa_set.elementAt(i); if (CUtility.DEBUG) { CUtility.ASSERT(bunch.m_nfa_bit.Get(state.m_label)); } nfa_stack.Push(state); } /* Main loop. */ //while (false == nfa_stack.empty()) while (nfa_stack.Count > 0) { state = (CNfa)nfa_stack.Pop(); if (CUtility.OLD_DUMP_DEBUG) { if (null != state.m_accept) { System.Console.WriteLine("Looking at accepting state " + state.m_label + " with <" + (new string(state.m_accept.m_action, 0, state.m_accept.m_action_read)) + ">"); } } if (null != state.m_accept && state.m_label < bunch.m_accept_index) { bunch.m_accept_index = state.m_label; bunch.m_accept = state.m_accept; bunch.m_anchor = state.m_anchor; if (CUtility.OLD_DUMP_DEBUG) { System.Console.WriteLine("Found accepting state " + state.m_label + " with <" + (new string(state.m_accept.m_action, 0, state.m_accept.m_action_read)) + ">"); } if (CUtility.DEBUG) { CUtility.ASSERT(null != bunch.m_accept); CUtility.ASSERT(CSpec.NONE == bunch.m_anchor || 0 != (bunch.m_anchor & CSpec.END) || 0 != (bunch.m_anchor & CSpec.START)); } } if (CNfa.EPSILON == state.m_edge) { if (null != state.m_next) { if (false == bunch.m_nfa_set.contains(state.m_next)) { if (CUtility.DEBUG) { CUtility.ASSERT(false == bunch.m_nfa_bit.Get(state.m_next.m_label)); } bunch.m_nfa_bit.Set(state.m_next.m_label); bunch.m_nfa_set.addElement(state.m_next); nfa_stack.Push(state.m_next); } } if (null != state.m_next2) { if (false == bunch.m_nfa_set.contains(state.m_next2)) { if (CUtility.DEBUG) { CUtility.ASSERT(false == bunch.m_nfa_bit.Get(state.m_next2.m_label)); } bunch.m_nfa_bit.Set(state.m_next2.m_label); bunch.m_nfa_set.addElement(state.m_next2); nfa_stack.Push(state.m_next2); } } } } if (null != bunch.m_nfa_set) { sortStates(bunch.m_nfa_set); } return; }