public void AddStartItems(ParseState pstate, SymbolSet follows) { for (int index = 0; index < this.m_prods.Count; ++index) { Production prod = (Production)this.m_prods[index]; pstate.MaybeAdd(new ProdItem(prod, 0)); } }
public void AddEntries() { for (ProdItemList prodItemList = this.m_items; prodItemList.m_pi != null; prodItemList = prodItemList.m_next) { ProdItem pi1 = prodItemList.m_pi; if (!pi1.m_done) { CSymbol csymbol = pi1.Next(); if (csymbol != null && !pi1.IsReducingAction()) { ParseState parseState = new ParseState(this.m_sgen, csymbol); parseState.MaybeAdd(new ProdItem(pi1.m_prod, pi1.m_pos + 1)); for (ProdItemList next = prodItemList.m_next; next != null && next.m_pi != null; next = next.m_next) { ProdItem pi2 = next.m_pi; if (csymbol == pi2.Next()) { parseState.MaybeAdd(new ProdItem(pi2.m_prod, pi2.m_pos + 1)); pi2.m_done = true; } } if (!this.m_items.AtEnd) { if (csymbol.IsAction()) { ParseState next = parseState.CheckExists(); foreach (CSymbol key in (IEnumerable)csymbol.m_follow.Keys) { if (key != this.m_sgen.m_symbols.EOFSymbol) { this.GetTransition(key).m_next = new ParserShift((ParserAction)csymbol, next); } } } else { this.GetTransition(csymbol).m_next = new ParserShift((ParserAction)null, parseState.CheckExists()); } } } } } }
public void AddEntries() { ProdItemList pil; for (pil=m_items; pil.m_pi!=null; pil=pil.m_next) { ProdItem item = pil.m_pi; if (item.m_done) continue; CSymbol s = item.Next(); if (s==null || item.IsReducingAction()) continue; // shift/goto action // Build a new parse state as target: we will check later to see if we need it ParseState p = new ParseState(m_sgen,s); // the new state should have at least the successor of this item p.MaybeAdd(new ProdItem(item.m_prod, item.m_pos+1)); // check the rest of the items in this ParseState (leads to m_done for them) // looking for other items that allow this CSymbol to pass for (ProdItemList pil1=pil.m_next; pil1!=null && pil1.m_pi!=null; pil1=pil1.m_next) { ProdItem another = pil1.m_pi; if (s==another.Next()) { p.MaybeAdd(new ProdItem(another.m_prod, another.m_pos+1)); another.m_done = true; } } if (!m_items.AtEnd) { if (s.IsAction()) { p = p.CheckExists(); foreach (CSymbol f in s.m_follow.Keys) if (f!=m_sgen.m_symbols.EOFSymbol) { Transition t = GetTransition(f); // if (t.m_next!=null) // m_sgen.Error(15,s.pos,String.Format("Action/Action or Action/Shift conflict on {0}",f.yytext)); t.m_next = new ParserShift((ParserAction)s,p); } } else { // we guarantee to make a nonzero entry in the parsetable GetTransition(s).m_next = new ParserShift(null, p.CheckExists()); } } } }
public ObjectList m_prods = new ObjectList(); // Production: productions with this symbol as left side public void AddStartItems(ParseState pstate,SymbolSet follows) { for (int pos=0;pos<m_prods.Count;pos++) { Production p = (Production)m_prods[pos]; pstate.MaybeAdd(new ProdItem(p, 0)); } }
void _Create() { ptokens tks = new ptokens(erh); m_outname = "syntax"; tks.m_sgen = this; m_lexer = tks; m_symbols.erh = erh; m_symbols.ClassInit(this); m_outname = "syntax"; m_outFile.WriteLine("using System;using Tools;"); Production special = new Production(this,m_symbols.Special); m_lexer.yytext = "error"; CSymbol e = (new CSymbol(this)).Resolve(); e.m_symtype = CSymbol.SymType.nonterminal; e.m_defined=true; // 1: INPUT // top-down parsing of script m_lexer.Start(m_inFile); m_tok = (TOKEN)m_lexer.Next(); //Console.WriteLine("Token <{0}> {1}",m_tok.yytext,m_tok.GetType().Name); while (m_tok!=null) ParseProduction(); // that's the end of the script if (!m_parserseen) Error(30,0,"no parser directive detected - possibly incorrect text encoding?"); m_outFile.WriteLine(m_actions); // output the action function m_outFile.WriteLine("} return null; }"); special.AddToRhs(m_symbols.m_startSymbol); special.AddToRhs(m_symbols.EOFSymbol); // 2: PROCESSING Console.WriteLine("First"); DoFirst(); Console.WriteLine("Follow"); DoFollow(); Console.WriteLine("Parse Table"); ParseState start = new ParseState(this,null); m_symbols.m_states[0] = start; start.MaybeAdd(new ProdItem(special,0)); start.Closure(); start.AddEntries(); Transition tfinal = (Transition)start.m_transitions[m_symbols.m_startSymbol.yytext]; ParserShift pe = tfinal.m_next; m_symbols.m_accept = pe.m_next; if (m_symbols.m_accept==null) m_symbols.erh.Error(new CSToolsFatalException(43,0,0,"","No accept state. ParserGenerator cannot continue.")); // 2A: Reduce States for the LR(0) parser foreach (ParseState ps in m_symbols.m_states.Values) ps.ReduceStates(); /* if (m_showParser) { foreach (ParseState ps in m_symbols.m_states.Values) { ps.Print0(); if (ps==m_symbols.m_accept) Console.WriteLine(" EOF accept"); } } */ // 3: Compute Look-ahead sets if (m_lalrParser) { m_symbols.Transitions(new Builder(Transition.BuildDR)); /* if (m_showParser) m_symbols.PrintTransitions(new Func(Transition.DR),"DR"); */ m_symbols.Transitions(new Builder(Transition.BuildReads)); new Digraph(this, new Relation(Transition.reads), new Func(Transition.DR), new Func(Transition.Read), new AddToFunc(Transition.AddToRead)).Compute(); // detect cycles in Read TBD /* if (m_showParser) m_symbols.PrintTransitions(new Func(Transition.Read),"Read"); */ m_symbols.Transitions(new Builder(Transition.BuildIncludes)); m_symbols.Transitions(new Builder(Transition.BuildLookback)); new Digraph(this, new Relation(Transition.includes), new Func(Transition.Read), new Func(Transition.Follow), new AddToFunc(Transition.AddToFollow)).Compute(); // detect cycles for which Read is non empty TBD /* if (m_showParser) m_symbols.PrintTransitions(new Func(Transition.Follow),"Follow"); */ m_symbols.Transitions(new Builder(Transition.BuildLA)); } // 5: OUTPUT // output the run-time ParsingInfo table Console.WriteLine("Building parse table"); m_symbols.Transitions(new Builder(Transition.BuildParseTable)); foreach (CSymbol v in m_symbols.symbols.Values) { if (v.m_symtype!=CSymbol.SymType.nodesymbol) continue; ParsingInfo pi = new ParsingInfo(v.yytext); CSymbol r = v; while (r.m_symtype==CSymbol.SymType.nodesymbol) r = r.m_refSymbol; if (m_symbols.symbolInfo[v.yytext]!=null) m_symbols.erh.Error(new CSToolsException(45,"Bad %node/%symbol hierarchy")); pi.m_parsetable = m_symbols.GetSymbolInfo(r.yytext).m_parsetable; m_symbols.symbolInfo[v.yytext] = pi; } Console.WriteLine("Writing the output file"); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public yy"+m_outname+"():base() { arr = new int[] { "); m_symbols.Emit(m_outFile); // output the class factories CSymbol s; Console.WriteLine("Class factories"); IDictionaryEnumerator de = m_symbols.symbols.GetEnumerator(); for (int pos = 0; pos<m_symbols.symbols.Count; pos++) { de.MoveNext(); string str = (string)de.Key; s = (CSymbol)de.Value; if ((s==null) // might happen because of error recovery || (s.m_symtype!=CSymbol.SymType.nonterminal && s.m_symtype!=CSymbol.SymType.nodesymbol)) continue; //Console.WriteLine("{0} {1}",s.yytext,s.m_symtype); m_outFile.WriteLine("new Sfactory(this,\"{0}\",new SCreator({0}_factory));",str); } m_outFile.WriteLine("}"); de.Reset(); for (int pos = 0; pos<m_symbols.symbols.Count; pos++) { de.MoveNext(); string str = (string)de.Key; s = (CSymbol)de.Value; if ((s==null) // might happen because of error recovery || (s.m_symtype!=CSymbol.SymType.nonterminal && s.m_symtype!=CSymbol.SymType.nodesymbol)) continue; //Console.WriteLine("{0} {1}",s.yytext,s.m_symtype); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public static object "+str+"_factory(Parser yyp) { return new "+str+"(yyp); }"); } m_outFile.WriteLine("}"); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public class "+m_outname+": Parser {"); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public "+m_outname+"():base(new yy"+m_outname+"(),new "+m_lexerClass+"()) {}"); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public "+m_outname+"(Symbols syms):base(syms,new "+m_lexerClass+"()) {}"); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public "+m_outname+"(Symbols syms,ErrorHandler erh):base(syms,new "+m_lexerClass+"(erh)) {}"); m_outFile.WriteLine(m_actvars); m_outFile.WriteLine(" }"); if (m_namespace) m_outFile.WriteLine("}"); Console.WriteLine("Done"); if (m_showParser) { foreach (ParseState ps in m_symbols.m_states.Values) { ps.Print(); if (ps==m_symbols.m_accept) Console.WriteLine(" EOF accept"); } } }