public override HashSet <int> GetSet() { HashSet <int> set = new HashSet <int>(FirstSet.GetSet()); set.UnionWith(SecondSet.GetSet()); return(set); }
public override ParseNode_Base Parse(SyntaxTreeBuilder pSyntaxTreeBuilder) { if (FirstSet.Contains(pSyntaxTreeBuilder.TokenScanner.Current.tokenId)) { return(node.Parse(pSyntaxTreeBuilder)); } return(parent.NextAfterChild(this, pSyntaxTreeBuilder)); }
public override TokenSet Init_PreCheckSet(ParseNode_Root parser) { if (FirstSet == null) { base.Init_PreCheckSet(parser); FirstSet.Add(new TokenSet(parser.TokenToId("IDENTIFIER"))); } return(FirstSet); }
public override ParseNode_Base Parse(SyntaxTreeBuilder pSyntaxTreeBuilder) { if (FirstSet.Contains(pSyntaxTreeBuilder.TokenScanner.Current.tokenId) && CheckPredicate(pSyntaxTreeBuilder)) { return(node.Parse(pSyntaxTreeBuilder)); } else { return(parent.NextAfterChild(this, pSyntaxTreeBuilder)); // .Parse2(pSyntaxTreeBuilder, goal); } }
public FirstSetBuilder(cfg cfg) { FirstSets = new FirstSet(); this.cfg = cfg; Empty=System.Configuration.ConfigurationManager.AppSettings["Empty"][0]; foreach (char c in cfg.Vn) { FirstSets.Add(c, new HashSet<char>()); } Build(); }
public static void Closure( this ItemSet itemset,cfg cfg, FirstSet first) { HashSet<Production> productions = cfg.Productions; bool changed; do { changed = false; char end = System.Configuration.ConfigurationManager.AppSettings["End"][0]; for (int i = 0; i < itemset.Count; ++i) { Item item = itemset.ElementAt(i); int index = item.Index; string right = item.Production.Right; if (index >= right.Length || !cfg.isVn(right[index])) { continue; } foreach (Production production in productions) { if (production.Left != right[index]) { continue; } int length = right.Length; if (index + 1 == right.Length) { Item newItem = new Item(production, 0,item.Symbol); changed |= itemset.AddItem(newItem); continue; } if (index + 1 < right.Length) { char empty = System.Configuration.ConfigurationManager.AppSettings["Empty"][0]; HashSet<char> symbols = first.GetFirst(right[index + 1]); foreach (char c in symbols) { if (c == empty) { Item newItem = new Item(production, 0, item.Symbol); changed |= itemset.Add(newItem); } else { Item newItem = new Item(production, 0, c); changed |= itemset.Add(newItem); } } } } } } while (changed); }
public override bool Scan(SyntaxTreeBuilder pSyntaxTreeBuilder) { if (!pSyntaxTreeBuilder.KeepScanning) { return(true); } if (FirstSet.Contains(pSyntaxTreeBuilder.TokenScanner.Current.tokenId) && CheckPredicate(pSyntaxTreeBuilder)) { return(node.Scan(pSyntaxTreeBuilder)); } return(true); }
public override bool Scan(SyntaxTreeBuilder pSyntaxTreeBuilder) { if (!pSyntaxTreeBuilder.KeepScanning) { return(true); } if (!FirstSet.Contains(pSyntaxTreeBuilder.TokenScanner.Current.tokenId)) { return(false); } pSyntaxTreeBuilder.TokenScanner.MoveNext(); return(true); }
public string ToString(ParseNode_Root parser) { var result = new StringBuilder(m_sNtName + " : " + rhs + " ."); if (FirstSet != null) { result.Append("\n lookahead " + FirstSet.ToString(parser)); } if (FollowSet != null) { result.Append("\n follow " + FollowSet.ToString(parser)); } return(result.ToString()); }
public override bool Scan(SyntaxTreeBuilder pSyntaxTreeBuilder) { if (!pSyntaxTreeBuilder.KeepScanning) { return(true); } var ifNode = node as ParseNode_ManyOpt_If ?? node as ParseNode_ManyOpt_IfNot; if (ifNode != null) { int tokenIndex, line; do { tokenIndex = pSyntaxTreeBuilder.TokenScanner.CurrentTokenIndex; line = pSyntaxTreeBuilder.TokenScanner.CurrentLine - 1; if (!node.Scan(pSyntaxTreeBuilder)) { return(false); } if (!pSyntaxTreeBuilder.KeepScanning) { return(true); } } while (pSyntaxTreeBuilder.TokenScanner.CurrentTokenIndex != tokenIndex || pSyntaxTreeBuilder.TokenScanner.CurrentLine - 1 != line); } else { while (FirstSet.Contains(pSyntaxTreeBuilder.TokenScanner.Current.tokenId)) { int tokenIndex = pSyntaxTreeBuilder.TokenScanner.CurrentTokenIndex; int line = pSyntaxTreeBuilder.TokenScanner.CurrentLine; if (!node.Scan(pSyntaxTreeBuilder)) { return(false); } if (!pSyntaxTreeBuilder.KeepScanning) { return(true); } if (pSyntaxTreeBuilder.TokenScanner.CurrentTokenIndex == tokenIndex && pSyntaxTreeBuilder.TokenScanner.CurrentLine - 1 == line) { throw new Exception("Infinite loop!!!"); } } } return(true); }
public override bool Scan(SyntaxTreeBuilder pSyntaxTreeBuilder) { if (!pSyntaxTreeBuilder.KeepScanning) { return(true); } if (FirstSet.Contains(pSyntaxTreeBuilder.TokenScanner.Current)) { return(rhs.Scan(pSyntaxTreeBuilder)); } else if (!FirstSet.ContainsEmpty()) { return(false); } return(true); }
public override ParseNode_Base Parse(SyntaxTreeBuilder pSyntaxTreeBuilder) { foreach (var node in nodes) { if (node.Matches(pSyntaxTreeBuilder)) { return(node.Parse(pSyntaxTreeBuilder)); } } if (FirstSet.ContainsEmpty()) { return(NextAfterChild(this, pSyntaxTreeBuilder)); } pSyntaxTreeBuilder.SyntaxErrorExpected(FirstSet); return(this); }
public override ParseNode_Base Parse(SyntaxTreeBuilder pSyntaxTreeBuilder) { if (!FirstSet.Contains(pSyntaxTreeBuilder.TokenScanner.Current.tokenId)) { pSyntaxTreeBuilder.SyntaxErrorExpected(FirstSet); return(this); } pSyntaxTreeBuilder.SyntaxRule_Cur.AddToken(pSyntaxTreeBuilder.TokenScanner).ParseNode = this; pSyntaxTreeBuilder.TokenScanner.MoveNext(); if (pSyntaxTreeBuilder.ErrorMessage == null) { pSyntaxTreeBuilder.SyntaxRule_Err = pSyntaxTreeBuilder.SyntaxRule_Cur; pSyntaxTreeBuilder.ParseNode_Err = pSyntaxTreeBuilder.ParseNode_Cur; } return(parent.NextAfterChild(this, pSyntaxTreeBuilder)); }
public override ParseNode_Base Parse(SyntaxTreeBuilder pSyntaxTreeBuilder) { bool wasError = pSyntaxTreeBuilder.ErrorMessage != null; ParseNode_Base ret = null; if (FirstSet.Contains(pSyntaxTreeBuilder.TokenScanner.Current)) { ret = rhs.Parse(pSyntaxTreeBuilder); } if ((ret == null || !wasError && pSyntaxTreeBuilder.ErrorMessage != null) && !FirstSet.ContainsEmpty()) { pSyntaxTreeBuilder.SyntaxErrorExpected(FirstSet); return(ret ?? this); } if (ret != null) { return(ret); } return(NextAfterChild(rhs, pSyntaxTreeBuilder)); // ready to be reduced }
public override bool Scan(SyntaxTreeBuilder pSyntaxTreeBuilder) { if (!pSyntaxTreeBuilder.KeepScanning) { return(true); } foreach (var node in nodes) { if (node.Matches(pSyntaxTreeBuilder)) { return(node.Scan(pSyntaxTreeBuilder)); } } if (!FirstSet.ContainsEmpty()) { return(false); } return(true); }
public override ParseNode_Base Parse(SyntaxTreeBuilder pSyntaxTreeBuilder) { var ifNode = node as ParseNode_ManyOpt_If; if (ifNode != null) { if (!ifNode.Matches(pSyntaxTreeBuilder)) { return(parent.NextAfterChild(this, pSyntaxTreeBuilder)); } var tokenIndex = pSyntaxTreeBuilder.TokenScanner.CurrentTokenIndex; var line = pSyntaxTreeBuilder.TokenScanner.CurrentLine; var nextNode = node.Parse(pSyntaxTreeBuilder); if (nextNode != this || pSyntaxTreeBuilder.TokenScanner.CurrentTokenIndex != tokenIndex || pSyntaxTreeBuilder.TokenScanner.CurrentLine != line) { return(nextNode); } //Debug.Log("Exiting Many " + this + " in goal: " + pSyntaxTreeBuilder.CurrentParseTreeNode); } else { if (!FirstSet.Contains(pSyntaxTreeBuilder.TokenScanner.Current.tokenId)) { return(parent.NextAfterChild(this, pSyntaxTreeBuilder)); } var tokenIndex = pSyntaxTreeBuilder.TokenScanner.CurrentTokenIndex; var line = pSyntaxTreeBuilder.TokenScanner.CurrentLine; var nextNode = node.Parse(pSyntaxTreeBuilder); if (!(nextNode == this && pSyntaxTreeBuilder.TokenScanner.CurrentTokenIndex == tokenIndex && pSyntaxTreeBuilder.TokenScanner.CurrentLine == line)) { // throw new Exception("Infinite loop!!! while parsing " + pSyntaxTreeBuilder.Current + " on line " + pSyntaxTreeBuilder.CurrentLine()); return(nextNode); } } return(parent.NextAfterChild(this, pSyntaxTreeBuilder)); }
public TableBuilder(cfg cfg, FirstSet first) { this.CFG = cfg; this.First = first; Build(); }
public override bool IsMember(int value) { return(FirstSet.IsMember(value) || SecondSet.IsMember(value)); }
public void initAutomatons() { /* * Goal -> Expr EOF * Expr -> Term Expr2 * Expr2 -> + Term Expr2 * | - Term Expr2 * | epsilon * Term -> Factor Term2 * Term2 -> * Factor Term2 * | / Factor Term2 * | epsilon * Factor -> '(' Expr ')' * | num * | name */ var goal = new StringSymbol("Goal"); var expr = new StringSymbol("Expr"); var expr2 = new StringSymbol("Expr2"); var term = new StringSymbol("Term"); var term2 = new StringSymbol("Term2"); var factor = new StringSymbol("Factor"); var openbracket = new StringSymbol("("); var closebracket = new StringSymbol(")"); var add = new StringSymbol("+"); var subtract = new StringSymbol("-"); var multiply = new StringSymbol("*"); var divide = new StringSymbol("/"); var num = new StringSymbol("num"); var name = new StringSymbol("name"); var eof = new StringSymbol("EOF"); automatons = new Dictionary <StringSymbol, Dfa <StringSymbol> > (); // NULLABLE nullable = CreateSet(); nullable.Add(expr2); nullable.Add(term2); // FIRST first = new Dictionary <StringSymbol, ISet <StringSymbol> >(); first [openbracket] = CreateSet(openbracket); first [closebracket] = CreateSet(closebracket); first [add] = CreateSet(add); first [subtract] = CreateSet(subtract); first [multiply] = CreateSet(multiply); first [divide] = CreateSet(divide); first [num] = CreateSet(num); first [name] = CreateSet(name); first [eof] = CreateSet(eof); //nonterminals first [goal] = CreateSet(goal, expr, term, factor, openbracket, name, num); first [expr] = CreateSet(expr, term, factor, openbracket, name, num); first [expr2] = CreateSet(expr2, add, subtract); first [term] = CreateSet(term, factor, openbracket, name, num); first [term2] = CreateSet(term2, multiply, divide); first [factor] = CreateSet(factor, openbracket, name, num); firstSet = new FirstSet <StringSymbol>(first); // FOLLOW follow = new Dictionary <StringSymbol, ISet <StringSymbol> >(); follow [openbracket] = CreateSet(expr, term, factor, openbracket, name, num); follow [closebracket] = CreateSet(eof, add, subtract, multiply, divide, closebracket, term2, expr2); follow [add] = CreateSet(term, openbracket, num, name, factor); follow [subtract] = CreateSet(term, openbracket, num, name, factor); follow [multiply] = CreateSet(factor, openbracket, name, num); follow [divide] = CreateSet(factor, openbracket, name, num); follow [num] = follow [closebracket]; follow [name] = follow [closebracket]; follow [eof] = CreateSet(); //nonterminals follow [expr] = CreateSet(eof, closebracket); follow [expr2] = CreateSet(eof, closebracket); follow [term] = CreateSet(eof, add, subtract, closebracket, expr2); follow [term2] = CreateSet(eof, add, subtract, closebracket, expr2); follow [factor] = CreateSet(eof, add, subtract, multiply, divide, closebracket, term2, expr2); // ** Build automatons ** /* * // for terminals - nulls * automatons [openbracket] = CreateDummyAcceptingDfa (); * automatons [closebracket] = CreateDummyAcceptingDfa (); * automatons [add] = CreateDummyAcceptingDfa (); * automatons [subtract] = CreateDummyAcceptingDfa (); * automatons [multiply] = CreateDummyAcceptingDfa (); * automatons [divide] = CreateDummyAcceptingDfa (); * automatons [num] = CreateDummyAcceptingDfa (); * automatons [name] = CreateDummyAcceptingDfa (); * automatons [eof] = CreateDummyAcceptingDfa (); */ // Goal -> Expr EOF // Expr -> Term Expr2 // Expr2 -> + Term Expr2 // | - Term Expr2 // | epsilon // Term -> Factor Term2 // Term2 -> * Factor Term2 // | / Factor Term2 // | epsilon // Factor -> '(' Expr ')' // | num // | name // Goal -> Expr EOF // (a --Expr--> b --EOF--> c) var a = new DfaState <StringSymbol> (); var b = new DfaState <StringSymbol> (); var c = new DfaState <StringSymbol> (); var aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); var bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); var cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> > (eof, c)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(1, cTransitionList); automatons [goal] = new Dfa <StringSymbol> (a); // Expr -> Term Expr2 // a --Term--> b --Expr2--> c a = new DfaState <StringSymbol> (); b = new DfaState <StringSymbol> (); c = new DfaState <StringSymbol> (); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(term, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr2, c)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(1, cTransitionList); automatons [expr] = new Dfa <StringSymbol> (a); // Expr2 -> + Term Expr2 // | - Term Expr2 // | epsilon // a --'+'--> b --Term--> c --Expr2--> d // a --'-'--> b // a = accepting (because of epsilon) a = new DfaState <StringSymbol> (); b = new DfaState <StringSymbol> (); c = new DfaState <StringSymbol> (); var d = new DfaState <StringSymbol> (); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); var dTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(add, b)); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(subtract, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(term, c)); cTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr2, d)); a.Initialize(1, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(0, cTransitionList); d.Initialize(1, dTransitionList); automatons [expr2] = new Dfa <StringSymbol> (a); // Term -> Factor Term2 // a --Factor--> b --Term2--> c a = new DfaState <StringSymbol> (); b = new DfaState <StringSymbol> (); c = new DfaState <StringSymbol> (); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(factor, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(term2, c)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(1, cTransitionList); automatons [term] = new Dfa <StringSymbol> (a); // Term2 -> * Factor Term2 // | / Factor Term2 // | epsilon // a --'*'--> b --Factor--> c --Term2--> d // a --'/'--> b // a = accepting (because of epsilon) a = new DfaState <StringSymbol> (); b = new DfaState <StringSymbol> (); c = new DfaState <StringSymbol> (); d = new DfaState <StringSymbol> (); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); dTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(multiply, b)); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(divide, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(factor, c)); cTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(term2, d)); a.Initialize(1, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(0, cTransitionList); d.Initialize(1, dTransitionList); automatons [term2] = new Dfa <StringSymbol> (a); // Factor -> '(' Expr ')' // | num // | name // a --'('--> b --Expr--> c --')'--> d // a --num--> d // a --name-> d a = new DfaState <StringSymbol> (); b = new DfaState <StringSymbol> (); c = new DfaState <StringSymbol> (); d = new DfaState <StringSymbol> (); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); dTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > > (); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(openbracket, b)); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(num, d)); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(name, d)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr, c)); cTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(closebracket, d)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(0, cTransitionList); d.Initialize(1, dTransitionList); automatons [factor] = new Dfa <StringSymbol> (a); /*---------------------Automatons for LeftRecursion * Goal -> Expr '(' Expr ')' * Expr -> Expr2 '+' Expr * Expr2 -> Term '-' Term * Term -> Expr '*' */ automatons_lr = new Dictionary <StringSymbol, Dfa <StringSymbol> >(); //init automatons null for terminals //automatons_lr[openbracket] = CreateDummyAcceptingDfa(); //automatons_lr[closebracket] = CreateDummyAcceptingDfa(); //automatons_lr[add] = CreateDummyAcceptingDfa(); //automatons_lr[subtract] = CreateDummyAcceptingDfa(); //automatons_lr[multiply] = CreateDummyAcceptingDfa(); //nullable nullable_lr = CreateSet(); //Goal -> Expr '(' Expr ')' // a --Expr--> b --'('--> c --Expr--> d --')'--> e a = new DfaState <StringSymbol>(); b = new DfaState <StringSymbol>(); c = new DfaState <StringSymbol>(); d = new DfaState <StringSymbol>(); var e = new DfaState <StringSymbol>(); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); dTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); var eTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(openbracket, c)); cTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr, d)); dTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(closebracket, e)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(0, cTransitionList); d.Initialize(0, dTransitionList); e.Initialize(1, eTransitionList); automatons_lr[goal] = new Dfa <StringSymbol>(a); //Expr -> Expr2 '+' Expr // a --Expr2--> b --'+'--> c --Expr--> d a = new DfaState <StringSymbol>(); b = new DfaState <StringSymbol>(); c = new DfaState <StringSymbol>(); d = new DfaState <StringSymbol>(); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); dTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr2, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(add, c)); cTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr, d)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(0, cTransitionList); d.Initialize(1, dTransitionList); automatons_lr[expr] = new Dfa <StringSymbol>(a); //Expr2 -> Term '-' Term // a --Term--> b --'-'--> c --Term--> d a = new DfaState <StringSymbol>(); b = new DfaState <StringSymbol>(); c = new DfaState <StringSymbol>(); d = new DfaState <StringSymbol>(); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); dTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(term, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(subtract, c)); cTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(term, d)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(0, cTransitionList); d.Initialize(1, dTransitionList); automatons_lr[expr2] = new Dfa <StringSymbol>(a); //Term -> Expr '*' // a --Expr--> b --'*'--> c a = new DfaState <StringSymbol>(); b = new DfaState <StringSymbol>(); c = new DfaState <StringSymbol>(); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(multiply, c)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(1, cTransitionList); automatons_lr[term] = new Dfa <StringSymbol>(a); /*---------------------Automatons for LeftRecursion with Nullable edges * Goal -> Expr * | Expr2 * Expr -> Term '-' * Expr2 -> Term2 '+' * Term -> Factor Expr2 * Term2 -> Term '*' * Factor -> '('Expr')' * | epsilon */ automatons_lr_nullable = new Dictionary <StringSymbol, Dfa <StringSymbol> >(); //init automatons null for terminals //automatons_lr_nullable[openbracket] = CreateDummyAcceptingDfa(); //automatons_lr_nullable[closebracket] = CreateDummyAcceptingDfa(); //automatons_lr_nullable[add] = CreateDummyAcceptingDfa(); //automatons_lr_nullable[subtract] = CreateDummyAcceptingDfa(); //automatons_lr_nullable[multiply] = CreateDummyAcceptingDfa(); //nullable nullable_lr_nullable = CreateSet(); nullable_lr_nullable.Add(factor); // Goal -> Expr // | Expr2 // a --Expr--> b // a --Expr2--> c a = new DfaState <StringSymbol>(); b = new DfaState <StringSymbol>(); c = new DfaState <StringSymbol>(); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr, b)); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr2, c)); a.Initialize(0, aTransitionList); b.Initialize(1, bTransitionList); c.Initialize(1, cTransitionList); automatons_lr_nullable[goal] = new Dfa <StringSymbol>(a); // Expr -> Term '-' // a --Term--> b --'-'--> c a = new DfaState <StringSymbol>(); b = new DfaState <StringSymbol>(); c = new DfaState <StringSymbol>(); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(term, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(subtract, c)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(1, cTransitionList); automatons_lr_nullable[expr] = new Dfa <StringSymbol>(a); // Expr2 -> Term2 '+' // a --Term2--> b --'+'--> c a = new DfaState <StringSymbol>(); b = new DfaState <StringSymbol>(); c = new DfaState <StringSymbol>(); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(term2, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(add, c)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(1, cTransitionList); automatons_lr_nullable[expr2] = new Dfa <StringSymbol>(a); // Term -> Factor Expr2 // a --Factor--> b --Expr2--> c a = new DfaState <StringSymbol>(); b = new DfaState <StringSymbol>(); c = new DfaState <StringSymbol>(); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(factor, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr2, c)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(1, cTransitionList); automatons_lr_nullable[term] = new Dfa <StringSymbol>(a); // Term2 -> Term '*' // a --Term--> b --'*'--> c a = new DfaState <StringSymbol>(); b = new DfaState <StringSymbol>(); c = new DfaState <StringSymbol>(); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(term, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(multiply, c)); a.Initialize(0, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(1, cTransitionList); automatons_lr_nullable[term2] = new Dfa <StringSymbol>(a); // Factor -> '('Expr')' // | epsilon // a --'('--> b --Expr--> c --')'--> d // a accepting a = new DfaState <StringSymbol>(); b = new DfaState <StringSymbol>(); c = new DfaState <StringSymbol>(); d = new DfaState <StringSymbol>(); aTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); bTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); cTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); dTransitionList = new List <KeyValuePair <StringSymbol, DfaState <StringSymbol> > >(); aTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(openbracket, b)); bTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(expr, c)); cTransitionList.Add(new KeyValuePair <StringSymbol, DfaState <StringSymbol> >(closebracket, d)); a.Initialize(1, aTransitionList); b.Initialize(0, bTransitionList); c.Initialize(0, cTransitionList); d.Initialize(1, dTransitionList); automatons_lr_nullable[factor] = new Dfa <StringSymbol>(a); }
public override bool Matches(SyntaxTreeBuilder pSyntaxTreeBuilder) { return(FirstSet.Contains(pSyntaxTreeBuilder.TokenScanner.Current) && CheckPredicate(pSyntaxTreeBuilder)); }