public static void BuildIncludes( Transition t) // code improved by Wayne Kelly { t.m_Follow = new SymbolSet (t.m_ps.m_sgen); foreach (Production p in t.m_A.m_prods) { for ( int i = p.m_rhs.Count - 1; i >= 0; i--) { CSymbol s = ( CSymbol)p.m_rhs[i]; if (s.m_symtype == CSymbol. SymType .nonterminal) { ParseState ps; if (i > 0) ps = new Path(t.m_ps, p.Prefix(i)).Top; else ps = t.m_ps; Transition b = ( Transition) ps.m_transitions[s.yytext]; b.m_includes[t] = true; } if (!s.IsNullable()) break; } } }
public static void BuildLookback(Transition t) { foreach (ParserReduce pr in t.m_reduce.Values) pr.BuildLookback(t); }
public static void Final(Transition t) { t.m_DR.AddIn(t.m_ps.m_sgen.m_symbols.EOFSymbol); }
public static void BuildReads(Transition t) { t.m_Read = new SymbolSet(t.m_ps.m_sgen); ParseState ps = t.m_A.Next(t.m_ps); if (ps==null) return; foreach (Transition b in ps.m_transitions.Values) if (b.m_A.IsNullable()) t.m_reads[b] = true; }
public static void AddToFollow(Transition a, SymbolSet s) { a.m_Follow.Add(s); }
SymbolSet m_Follow; // ditto public static void BuildDR(Transition t) { SymbolsGen sg = t.m_ps.m_sgen; t.m_DR = new SymbolSet(sg); if (t.m_next==null) return; foreach (Transition u in t.m_next.m_next.m_transitions.Values) if (u.m_next!=null) if (u.m_A.m_symtype==CSymbol.SymType.terminal || u.m_A.m_symtype==CSymbol.SymType.eofsymbol) t.m_DR.AddIn(u.m_A); }
public static SymbolSet Follow(Transition a) { return a.m_Follow; }
public static void BuildParseTable(Transition t) { Symbols syms = t.m_ps.m_sgen.m_symbols; ParsingInfo pi = t.ParsingInfo; ParserReduce red = null; foreach (ParserReduce pr in t.m_reduce.Values) { if (t.m_ps.m_sgen.m_lalrParser? pr.m_lookAhead.Contains(t.m_A): pr.m_prod.m_lhs.m_follow.Contains(t.m_A)) { if (red!=null) syms.erh.Error(new CSToolsException(12,string.Format("reduce/reduce conflict {0} vs {1}",red.m_prod.m_pno,pr.m_prod.m_pno)+ string.Format(" state {0} on {1}",t.m_ps.m_state,t.m_A.yytext))); red = pr; } // else // t.Print(pr.m_lookAhead,"discarding reduce ("+pr.m_prod.m_pno+") LA "); } if (t.m_next!=null && t.m_A!=syms.EOFSymbol) { if (red==null) pi.m_parsetable[t.m_ps.m_state] = t.m_next; else if (t.m_A.ShiftPrecedence(red.m_prod,t.m_ps)) pi.m_parsetable[t.m_ps.m_state] = t.m_next; else pi.m_parsetable[t.m_ps.m_state] = red; } else if (red!=null) pi.m_parsetable[t.m_ps.m_state] = red; }
public static SymbolSet DR(Transition a) { return a.m_DR; }
public static SymbolSet Read(Transition a) { return a.m_Read; }
public static Hashtable includes(Transition a) { return a.m_includes; }
internal Hashtable m_lookbackOf = new Hashtable(); // ParserReduce->bool public static Hashtable reads(Transition a) { return a.m_reads; }
public void BuildLookback(Transition a) { SymbolsGen sg = a.m_ps.m_sgen; if (m_lookAhead!=null) return; m_lookAhead = new SymbolSet(sg); foreach (ParseState p in sg.m_symbols.m_states.Values) { Transition b = (Transition)p.m_transitions[m_prod.m_lhs.yytext]; if (b==null) continue; Path pa = new Path(p,m_prod.Prefix(m_prod.m_rhs.Count)); if (pa.valid && pa.Top == a.m_ps) b.m_lookbackOf[this] = true; } }
public static void BuildLA(Transition t) { foreach (ParserReduce pr in t.m_lookbackOf.Keys) pr.m_lookAhead.Add(t.m_Follow); }
public static void AddToRead(Transition a,SymbolSet s) { a.m_Read.Add(s); }
public static void BuildParseTable(Transition t) { YyParser syms = t.m_ps.m_sgen.m_symbols; ParsingInfo pi = t.ParsingInfo; ParserReduce red = null; foreach (ParserReduce pr in t.m_reduce.Values) { if (t.m_ps.m_sgen.m_lalrParser? pr.m_lookAhead.Contains(t.m_A): pr.m_prod.m_lhs.m_follow.Contains(t.m_A)) { if (red!=null) syms.erh.Error(new CSToolsException(12,string.Format("reduce/reduce conflict {0} vs {1}",red.m_prod.m_pno,pr.m_prod.m_pno)+ string.Format(" state {0} on {1}",t.m_ps.m_state,t.m_A.yytext))); red = pr; } // else // t.Print(pr.m_lookAhead,"discarding reduce ("+pr.m_prod.m_pno+") LA "); } if (t.m_next!=null && t.m_A!=syms.EOFSymbol) { if (red == null) pi.m_parsetable[t.m_ps.m_state] = t.m_next; else { int p = Precedence.Check(t.m_A, red.m_prod, 0); // 4.7m if (p > 0) { pi.m_parsetable[t.m_ps.m_state] = t.m_next; t.m_reduce.Remove(red.m_prod); } else if (p < 0) { pi.m_parsetable[t.m_ps.m_state] = red; t.m_next = null; } else switch (t.m_A.ShiftPrecedence(red.m_prod, t.m_ps)) // 4.5h { case Precedence.PrecType.left: pi.m_parsetable[t.m_ps.m_state] = t.m_next; t.m_reduce.Remove(red.m_prod); break; case Precedence.PrecType.right: pi.m_parsetable[t.m_ps.m_state] = red; t.m_next = null; break; } } } else if (red != null) pi.m_parsetable[t.m_ps.m_state] = red; }
public static void BuildIncludes(Transition t) { t.m_Follow = new SymbolSet(t.m_ps.m_sgen); foreach (ParseState ps in t.m_ps.m_sgen.m_symbols.m_states.Values) foreach (Production p in t.m_ps.m_sgen.prods) { Transition b = (Transition)ps.m_transitions[p.m_lhs.yytext]; if (b==null) continue; for (int i=p.m_rhs.Count-1;i>=0;i--) { CSymbol s = (CSymbol)p.m_rhs[i]; if (s==t.m_A) { if (i==0 && b.m_ps==t.m_ps) { t.m_includes[b] = true; break; } if (new Path(b.m_ps,p.Prefix(i)).Top == t.m_ps) { t.m_includes[b] = true; break; } } if (!s.CouldBeEmpty()) break; } } }