private Queue <TransToken> ConvertInput(string input) { Queue <TransToken> newInputQueue = new Queue <TransToken> { }; string[] splitInput = input.Split(' '); foreach (string token in splitInput) { int val = 0; TransToken termToAdd; if (Int32.TryParse(token, out val)) { termToAdd = new TransToken("i", val); } else { termToAdd = new TransToken(token); } newInputQueue.Enqueue(termToAdd); } newInputQueue.Enqueue(new TransToken("$")); return(newInputQueue); }
private TItemState ComputeClosure(TItemState itemState) { List <TransRule> rules = parent.rules; HashSet <TItem> closure = new HashSet <TItem>(itemState.stateItems); Queue <TItem> closeless = new Queue <TItem>(itemState.stateItems); while (closeless.Count != 0) { TItem item = closeless.Dequeue(); if (rules[item.ruleNum].body.Count == item.dotPos) { continue; } TransToken nextToken = rules[item.ruleNum].body[item.dotPos]; for (int i = 0; i < rules.Count; ++i) { if (nextToken.token != rules[i].head.token) { continue; } TItem newItem = new TItem(i, 0); if (closure.Add(newItem)) { closeless.Enqueue(newItem); } } } return(new TItemState(closure)); }
public override bool Equals(object obj) { if (!(obj is TransToken)) { return(false); } TransToken other = (TransToken)obj; return(this.token == other.token); }
private Dictionary <TransToken, HashSet <TransToken> > ComputeFollowSets() { Dictionary <TransToken, HashSet <TransToken> > followSets = InitilizeSets(); followSets[rules[0].head].Add(new TransToken("$")); bool notDone; do { notDone = false; foreach (TransRule rule in rules) { for (int i = 0; i < rule.body.Count; ++i) { TransToken token = rule.body[i]; if (!nonterms.Contains(token)) { continue; } HashSet <TransToken> nextTokenFirsts = GetNextTokenFirsts(rule, i + 1); foreach (TransToken first in nextTokenFirsts) { if (first.Equals(new TransToken(""))) { foreach (TransToken headFirst in followSets[rule.head]) { notDone |= followSets[token].Add(headFirst); } } else { notDone |= followSets[token].Add(first); } } } } } while (notDone); return(followSets); }
private HashSet <TransToken> GetNextTokenFirsts(TransRule rule, int nextTokenIndex) { HashSet <TransToken> result = new HashSet <TransToken> { }; bool containsEpsilon = true; for (int i = nextTokenIndex; i < rule.body.Count; ++i) { TransToken token = rule.body[i]; containsEpsilon = false; if (terms.Contains(token)) { result.Add(token); break; } foreach (TransToken first in firstSets[token]) { if (first.Equals(new TransToken(""))) { containsEpsilon = true; } result.Add(first); } containsEpsilon |= firstSets[token].Count == 0; if (!containsEpsilon) { break; } } if (containsEpsilon) { result.Add(new TransToken("")); } return(result); }
private TItemState Goto(TItemState itemState, TransToken nextTerm) { HashSet <TItem> resultingSet = new HashSet <TItem> { }; foreach (TItem item in itemState.stateItems) { TransRule rule = parent.rules[item.ruleNum]; if (rule.body.Count == item.dotPos) { continue; } if (rule.body[item.dotPos].token == nextTerm.token) { resultingSet.Add(new TItem(item.ruleNum, item.dotPos + 1)); } } return(new TItemState(resultingSet)); }
private int GetStateFromGoto(int state, TransToken token) { return(gotoTable[new Tuple <int, TransToken> (state, token)]); }
public TransRule(TransToken head, List <TransToken> body, Action <TransRule> semanticRule) { this.head = head; this.body = body; this.semanticRule = semanticRule; }
private bool GetEndCondition() { TransToken inputEndSymbol = new TransToken("$"); return(inputQueue.Peek().Equals(inputEndSymbol) && stateStack.Peek() == 1); }