/// <exclude/> public Dfa(Nfa nfa) : base(nfa.m_tks) { m_tokens = m_tks.m_tokens; AddNfaNode(nfa); // the starting node is Closure(start) Closure(); AddActions(); // recursively build the Dfa }
/// <exclude/> public virtual void Build(Nfa nfa) { if (m_sub!=null) { Nfa sub = new Nfa(nfa.m_tks,m_sub); nfa.AddEps(sub); sub.m_end.AddEps(nfa.m_end); } else nfa.AddEps(nfa.m_end); }
public override void Build(Nfa nfa) { Nfa sub = new Nfa(nfa.m_tks,m_sub); nfa.AddEps(sub); nfa.AddEps(nfa.m_end); sub.m_end.AddEps(nfa); }
public override void Build(Nfa nfa) { int j,n = m_str.Length; NfaNode p, pp = nfa; for (j=0;j<n;pp = p,j++) { p = new NfaNode(nfa.m_tks); pp.AddUArc(m_str[j],p); } pp.AddEps(nfa.m_end); }
public override void Build(Nfa nfa) { nfa.AddArcEx(this,nfa.m_end); }
public override void Build(Nfa nfa) { base.Build(nfa); nfa.m_end.AddEps(nfa); }
/// <exclude/> public override void Build(Nfa nfa) { if (m_next!=null) { if (m_sub!=null) { Nfa first = new Nfa(nfa.m_tks,m_sub); Nfa second = new Nfa(nfa.m_tks,m_next); nfa.AddEps(first); first.m_end.AddEps(second); second.m_end.AddEps(nfa.m_end); } else m_next.Build(nfa); } else base.Build(nfa); }
/// <exclude/> public override void Build(Nfa nfa) { if (m_alt!=null) { Nfa alt = new Nfa(nfa.m_tks,m_alt); nfa.AddEps(alt); alt.m_end.AddEps(nfa.m_end); } base.Build(nfa); }
void _Create() { m_outFile.WriteLine("using System;using Tools;"); m_tokens = new YyLexer(erh); string buf = ""; string str = ""; string name=""; string startsym; Nfa nfa; int p,q,max; Console.WriteLine("Reading Input File"); while (!m_inFile.Eof()) { buf = m_inFile.ReadLine(); startsym = "YYINITIAL"; max = buf.Length; p = 0; if (!White(buf,ref p,max)) continue; if (buf[p]=='%') { // directive // %lexer if(buf.Length>=p+6 && "%lexer".Equals(buf.Substring(p,6))) { m_lexerseen = true; p+=6; if (!White(buf,ref p, max)) continue; q = p; NonWhite(buf,ref p, max); if (q!=p) m_outname = buf.Substring(q,p-q); continue; } // %encoding if (buf.Length>=p+9 && "%encoding".Equals(buf.Substring(p,9))) { p+=9; White(buf,ref p, max); q = p; NonWhite(buf,ref p, max); m_tokens.InputEncoding = buf.Substring(q,p-q); continue; } // %namespace if (buf.Length>=p+10 && "%namespace".Equals(buf.Substring(p,10))) { p+=10; White(buf,ref p,max); q = p; NonWhite(buf,ref p,max); m_outFile.WriteLine("namespace "+buf.Substring(q,p-q)+" {"); m_namespace = true; continue; } // %define if(buf.Length>=p+7 && "%define".Equals(buf.Substring(p,7))) { p+=7; White(buf,ref p,max); q = p; if (!NonWhite(buf,ref p,max)) { erh.Error(new CSToolsException(44,"Bad define")); continue; } name=buf.Substring(q,p-q); p++; if (White(buf,ref p,max)) defines[name]=buf.Substring(p,max-p); } else // % token/node if (buf.Length>=p+6 && "%token".Equals(buf.Substring(p,6))) EmitClassDefin(buf,ref p,max,m_inFile,"TOKEN", out str,out name,true); else if (buf.Length>=p+5 && "%node".Equals(buf.Substring(p,5))) EmitClassDefin(buf,ref p,max,m_inFile,"NODE",out str,out name,true); else if (buf.Length>=p+2 && "%{".Equals(buf.Substring(p,2))) CopyCode(); else if (buf.Length>=p+9 && "%declare{".Equals(buf.Substring(p,9))) { p += 8; m_actvars = ToBraceIfFound(ref buf,ref p,ref max,m_inFile); m_actvars = m_actvars.Substring(1,m_actvars.Length-2); } else m_tokens.erh.Error(new CSToolsException(8,"Unknown directive "+buf.Substring(p,max-p))); continue; } else if (buf[p]=='<') { // startstate q = p++; while (p<max && buf[p]!='>') p++; if (p++ ==max) { m_tokens.erh.Error(new CSToolsException(25,"Bad startsymbol")); continue; } startsym = buf.Substring(q+1,p-q-2); White(buf, ref p, max); } q=p; // can't simply look for nonwhite space here because embedded spaces GetRegex(buf,ref p,max); string trgx = buf.Substring(q,p-q); if (m_tokens.toupper) trgx = trgx.ToUpper(); Regex rgx = new Regex(this,q,trgx); Nfa nfa1= new Nfa(this,rgx); if (!m_startstates.Contains(startsym)) m_startstates[startsym] = new Nfa(this); nfa = (Nfa)m_startstates[startsym]; nfa.AddEps(nfa1); White(buf,ref p,max); m_actions[nfa1.m_end.m_state] = nfa1.m_end; // handle multiline actions enclosed in {} nfa1.m_end.m_sTerminal = ToBraceIfFound(ref buf,ref p, ref max,m_inFile); // examine action string if (nfa1.m_end.m_sTerminal.Length>0 && nfa1.m_end.m_sTerminal[0] == '%') { string tokClass,b = nfa1.m_end.m_sTerminal; q = 1; max = b.Length; int n; for (n=0;q<max&&b[q]!=' '&&b[q]!='\t'&&b[q]!='\n'&&b[q]!='{'&&b[q]!=':';q++,n++) // extract the class name ; tokClass = b.Substring(1,n); // new-style auto token construction object ob = m_tokens.tokens[tokClass]; TokClassDef t = (TokClassDef)ob; bool isNew = (t==null); // check for initialisation action following %name string init = b.Substring(n+1,b.Length-n-1); string bas1 = "TOKEN"; bool haveInit = false; for (int j=0;j<init.Length;j++) if (init[j]=='{') { haveInit = true; break; } else if (init[j]==':') { bas1 = ""; for (;init[j]==' '||init[j]=='\r';j++) ; for (;init[j]!=' '&&init[j]!='\t'&&init[j]!='{'&&init[j]!='\n';j++) bas1 += init[j]; break; } if (isNew && tokClass!="TOKEN") { // this token class has not been declared. Do so now bool isNode = (m_tokens.tokens[bas1]!=null); t = new TokClassDef(this,tokClass,bas1); // updates TOKEN.tokens m_outFile.WriteLine("//%{0}+{1}",tokClass,t.m_yynum); m_outFile.Write("public class {0} : {1}",tokClass,bas1); m_outFile.WriteLine("{ public override string yyname { get { return \""+tokClass+"\";}}"); m_outFile.WriteLine("public override int yynum { get { return "+t.m_yynum+"; }}"); m_outFile.WriteLine(" public "+tokClass+"(Lexer yyl):base(yyl) {}}"); } if (haveInit && init.IndexOf("%except")<0) { init = init.Replace("yylval","yymval"); // yuk: avoid the next line munging yylval init = FixActions(init); init = init.Replace("yymval","yylval"); nfa1.m_end.m_sTerminal = "%"+NewConstructor(t,init); } } } if (!m_lexerseen) m_tokens.erh.Error(new CSToolsException(26,"No %lexer directive detected: possibly incorrect text encoding?")); Console.WriteLine("Constructing DFAs"); foreach (string s in m_startstates.Keys) { Dfa d = new Dfa((Nfa)m_startstates[s]); m_tokens.starts[s] = d; if (d.m_actions!=null) Console.WriteLine("Warning: This lexer script generates an infinite token stream on bad input"); } Console.WriteLine("Output phase"); Emit(m_actions,m_actvars,m_namespace,m_showDfa); Console.WriteLine("End of Create"); object o = m_tokens.starts["YYINITIAL"]; if (o == null) Console.WriteLine("Warning: No lexer"); else if (((Dfa)o).m_actions!=null) // repeat the above warning Console.WriteLine("Warning: This lexer script generates an infinite token stream on bad input"); }