private NfaNode GetNfaInternal(Node <char> node) { NfaNode left = node.Left != null?GetNfaInternal(node.Left) : null; NfaNode right = node.Right != null?GetNfaInternal(node.Right) : null; if (node.Value == AND) { return(GetNfaNodesWithAnd(left, right)); } else if (node.Value == OR) { return(GetNfaNodesWithOr(left, right)); } else if (node.Value == STAR) { return(GetNfaNodeWithStar(left)); } else { var first = new NfaNode(); first.Links.Add(new NfaNodeLink(new NfaNode(), node.Value)); return(first); } }
public void Emit(Hashtable actions, string actvars, bool nameSpace, bool showDfa) { if (showDfa) { for (int j = 0; j < states.Count; j++) { ((Dfa)states[j]).Print(); } foreach (DictionaryEntry d in m_tokens.starts) { Console.WriteLine((string)d.Key + ": " + ((Dfa)d.Value).m_state); } } m_outFile.WriteLine("//%|" + m_outname); m_outFile.WriteLine("public class yy" + m_outname + " : YyLexer {"); m_outFile.WriteLine(" public yy" + m_outname + "(ErrorHandler eh):base(eh) { arr = new int[] { "); m_tokens.EmitDfa(m_outFile); foreach (string x in m_tokens.tokens.Keys) { m_outFile.WriteLine(" new Tfactory(this,\"" + x + "\",new TCreator(" + x + "_factory));"); } m_outFile.WriteLine("}"); foreach (string y in m_tokens.tokens.Keys) { m_outFile.WriteLine("public static object " + y + "_factory(Lexer yyl) { return new " + y + "(yyl);}"); } Console.WriteLine("Actions function"); m_outFile.WriteLine("public override TOKEN OldAction(Lexer yym,ref string yytext,int action, ref bool reject) {"); m_outFile.WriteLine(" switch(action) {"); m_outFile.WriteLine(" case -1: break;"); IDictionaryEnumerator pos = actions.GetEnumerator(); for (int m = 0; m < actions.Count; m++) { pos.MoveNext(); int act = (int)pos.Key; NfaNode e = (NfaNode)pos.Value; if (e.m_sTerminal.Length != 0 && e.m_sTerminal[0] == '%') // auto token action { continue; } m_outFile.WriteLine(" case {0}: {1}", act, ActionTransform(e.m_sTerminal)); m_outFile.WriteLine(" break;"); // in case m_sTerminal ends with a // comment (quite likely) } m_outFile.WriteLine(" }"); m_outFile.WriteLine(" return null;"); m_outFile.WriteLine("}}"); m_outFile.WriteLine("public class " + m_outname + ":Lexer {"); m_outFile.WriteLine("public " + m_outname + "():base(new yy" + m_outname + "(new ErrorHandler(false))) {}"); m_outFile.WriteLine("public " + m_outname + "(ErrorHandler eh):base(new yy" + m_outname + "(eh)) {}"); m_outFile.WriteLine("public " + m_outname + "(YyLexer tks):base(tks){}"); m_outFile.WriteLine(actvars); m_outFile.WriteLine(" }"); if (nameSpace) { m_outFile.WriteLine("}"); } }
private NfaNode GetNfaNodeWithStar(NfaNode left) { NfaNode leftLastNfaNode = GetLastNfaNode(left); left.Links.Add(new NfaNodeLink(leftLastNfaNode, EPS)); leftLastNfaNode.Links.Add(new NfaNodeLink(left, EPS)); return(left); }
private DfaNode GetFirstDfaNode(NfaNode nfaNode) { var dfaNode = new DfaNode(); dfaNode.NfaNodes = GetValuesFromNfaNode(nfaNode, c => c == EPS, nfaL => nfaL.NfaNode, values: new List <NfaNode> { nfaNode }).Distinct().ToList(); return(dfaNode); }
public DfaNode GetDfa(NfaNode nfaNode) { DfaNode firstDfaNode = GetFirstDfaNode(nfaNode); BuildDfaInternal(firstDfaNode, new List <DfaNode> { firstDfaNode }); MarkIsFinal(firstDfaNode, nfaNode); return(firstDfaNode); }
private void MarkIsFinal(DfaNode firstDfaNode, NfaNode nfaNode) { NfaNode lastNfaNode = GetLastNfaNode(nfaNode); if (firstDfaNode.NfaNodes.Contains(lastNfaNode)) { firstDfaNode.IsFinal = true; } MarkIsFinalInternal(firstDfaNode, lastNfaNode); }
internal Nfa.Nfa BuildNfa() { var nfa = new Nfa.Nfa(); NfaNode end_node = new NfaNode().SetAccepting(true); foreach (Tuple <char, char> elem in bracketElements) { nfa.StartNode.ConnectTo(end_node, NfaEdge.Create(elem.Item1, elem.Item2)); } return(nfa); }
private NfaNode GetNfaNodesWithOr(NfaNode left, NfaNode right) { var firstNfaNode = new NfaNode(); firstNfaNode.Links.Add(new NfaNodeLink(left, EPS)); firstNfaNode.Links.Add(new NfaNodeLink(right, EPS)); var lastNfaNode = new NfaNode(); GetLastNfaNode(left).Links.Add(new NfaNodeLink(lastNfaNode, EPS)); GetLastNfaNode(right).Links.Add(new NfaNodeLink(lastNfaNode, EPS)); return(firstNfaNode); }
private void mergeNodes(NfaNode[] endNodes) { if (endNodes.Length <= 1) { return; } NfaNode master = endNodes[0]; master.TakeConnections(endNodes.Skip(1)); mergeNodes(master.Connections.From.Select(it => it.Item2) // we are connected to the master node, // so this outgoing connection does not stop us from the merging .Where(it => it.ConnectedTo.Count() == 1).ToArray()); }
private List <T> GetValuesFromNfaNode <T>(NfaNode node, Func <char, bool> matchСondition, Func <NfaNodeLink, T> getValue, bool addEpsCondition = false, List <T> values = null, List <NfaNode> previous = null) { if (previous == null) { previous = new List <NfaNode>(); } if (values == null) { values = new List <T>(); } previous.Add(node); Func <char, bool> matchСonditionClone = matchСondition; foreach (NfaNodeLink childNode in node.Links) { if (matchСondition(childNode.LinkValue)) { values.Add(getValue(childNode)); if (addEpsCondition) { matchСonditionClone += c => c == EPS; } } else { continue; } if (previous.Contains(childNode.NfaNode)) { continue; } GetValuesFromNfaNode <T>(childNode.NfaNode, matchСonditionClone, getValue, addEpsCondition, values, previous); } return(values); }
private NfaNode GetLastNfaNode(NfaNode node, List <NfaNode> previous = null) { if (previous == null) { previous = new List <NfaNode>(); } previous.Add(node); foreach (NfaNodeLink childNode in node.Links) { if (previous.Contains(childNode.NfaNode)) { continue; } return(GetLastNfaNode(childNode.NfaNode, previous)); } return(node); }
public bool Add(NfaNode n) { if (this.m_node == null) { this.m_next = new NList(); this.m_node = n; } else if (this.m_node.m_state < n.m_state) { this.m_next = new NList(this.m_node, this.m_next); this.m_node = n; } else { if (this.m_node.m_state == n.m_state) { return(false); } return(this.m_next.Add(n)); } return(true); }
private void MarkIsFinalInternal(DfaNode node, NfaNode nfaNode, List <DfaNode> previous = null) { if (previous == null) { previous = new List <DfaNode>(); } previous.Add(node); foreach (DfaNodeLink childNode in node.Links) { if (childNode.DfaNode.NfaNodes.Contains(nfaNode)) { childNode.DfaNode.IsFinal = true; } if (previous.Contains(childNode.DfaNode)) { continue; } MarkIsFinalInternal(childNode.DfaNode, nfaNode, previous); } }
public void AddEps(NfaNode next) { this.m_eps.Add((object)next); }
public void AddArcEx(Regex re, NfaNode next) { this.m_arcs.Add((object)new ArcEx(re, next)); }
public void AddUArc(char ch, NfaNode next) { this.m_arcs.Add((object)new UArc(ch, next)); }
private NList(NfaNode nd, NList nx) { this.m_node = nd; this.m_next = nx; }
public NList() { this.m_node = (NfaNode)null; this.m_next = (NList)null; }
private NfaNode GetNfaNodesWithAnd(NfaNode left, NfaNode right) { GetLastNfaNode(left).Links.Add(new NfaNodeLink(right, EPS)); return(left); }
public Nfa(TokensGen tks) : base(tks) { this.m_end = new NfaNode(this.m_tks); }
public Nfa(TokensGen tks, Regex re) : base(tks) { this.m_end = new NfaNode(tks); re.Build(this); }