public LL1Table(InputGrammer inputGrammer) : base(inputGrammer) { generatedFirst = new GenerateFirst(inputGrammer); GenerateFollow generatedFollow = new GenerateFollow(inputGrammer, generatedFirst); if (!checkLeft()) { throw new FailToGenerateLL1Exception("含有左递归或左公因子"); } foreach (Production production in inputGrammer.userProductions) { List <string> firstSetOfRightPart = generatedFirst.getFirstFromPart(production.Values); foreach (string token in firstSetOfRightPart.Where(i => (!inputGrammer.nonTerminalTokens.Contains(i) && i != PublicFunc.EPSILON))) { Item item = new Item(production.Key, token, production.ToString()); checkConflict(item); table.Add(item); } if (firstSetOfRightPart.Contains(PublicFunc.EPSILON)) { foreach (string token in generatedFollow[production.Key].Where(i => !inputGrammer.nonTerminalTokens.Contains(i))) { Item item = new Item(production.Key, token, production.ToString()); checkConflict(item); table.Add(item); } } } }
public SLR1Table(DFAGraphBase DFAGraph, InputGrammer inputGrammer) : base(DFAGraph, inputGrammer) { GenerateFirst generatedFirst = new GenerateFirst(inputGrammer); generatedFollow = new GenerateFollow(inputGrammer, generatedFirst); generateLRTable(); }
public DFAGraphFromLR1(InputGrammer inputGrammer) : base(inputGrammer) { generatedFirst = new GenerateFirst(inputGrammer); DFANodes.Add(new DFANode(closure(new List <ProductionInLR0> { new ProductionInLR1(inputGrammer.userProductions[0], new List <string> { PublicFunc.ENDSYMBOL }) }), DFANodes.Count.ToString())); generateDFAGraph <ProductionInLR1>(DFANodes); }
private void button1_Click(object sender, EventArgs e) { try { InputGrammer inputGrammer = new InputGrammer(InputArea.Text); GenerateFirst generatedFirst = new GenerateFirst(inputGrammer); if (formFirstAndFollow == null) { formFirstAndFollow = new FormFirstAndFollow(); } formFirstAndFollow.getTextData(generatedFirst); formFirstAndFollow.Show(); } catch (NoValidGrammerException ee) { MessageBox.Show(ee.Message, ee.Message); } }
public GenerateFollow(InputGrammer inputGrammer, GenerateFirst generatedFirst) { foreach (string token in inputGrammer.nonTerminalTokens) { followSet.Add(new Production(token, new List <string>())); } // 加入$符号 GetValuesByKey(inputGrammer.nonTerminalTokens[0], followSet).Add(ENDSYMBOL); int changeTotal; do { changeTotal = 0; foreach (Production production in inputGrammer.userProductions) { List <string> valuesInFollow = GetValuesByKey(production.Key, followSet); for (int i = 0; i < production.Values.Count; i++) { string token = production.Values[i]; if (inputGrammer.nonTerminalTokens.Contains(token)) { List <string> valuesOfTokenInFollow = GetValuesByKey(token, followSet); if (i != production.Values.Count - 1) { bool hasEpsilon = false; if (ExtendElementWithoutEpsilon(generatedFirst.getFirstFromPart(production.Values, i + 1), valuesOfTokenInFollow, ref hasEpsilon)) { changeTotal++; } if (hasEpsilon && ExtendElement(valuesInFollow, valuesOfTokenInFollow)) { changeTotal++; } } else if (ExtendElement(valuesInFollow, valuesOfTokenInFollow)) { changeTotal++; } } } } } while (changeTotal != 0); }