public void AddChoice(GrammarNodeD grammarNodeD) { // assert parent is concatenation ConcatenationD parent = grammarNodeD.Parent as ConcatenationD; if (parent == null) throw new Exception("The grammarNodeD parent is not of type concatenation"); // assert parent parent is choiceD ChoiceD parentParent = parent.Parent as ChoiceD; if (parentParent == null) throw new Exception("The grammarNodeD parent parent is not of type choiceD"); ConcatenationD concatD = new ConcatenationD(parentParent); parentParent.appendChild(concatD); concatD.appendChild(new UndefinedD(concatD)); }
/// <summary> /// Returns the rule structure, without the ChoiceLineD /// </summary> /// <param name="productionRuleLexemes"></param> /// <returns></returns> private void CreateRuleDataStructure(List<Lexeme> productionRuleLexemes, ChoiceLineD choiceLineD) { //GrammarNodeD returnVal; OperationD current = choiceLineD; // points to which level we are at //returnVal = current; // Split by Choice as this has the highest precedence List<Lexeme> choiceLexemes = new List<Lexeme>(); ArrayList choices = new ArrayList(); foreach (Lexeme lexeme in productionRuleLexemes) { if (lexeme.Type == LexemeType.Choice) { choices.Add(choiceLexemes); choiceLexemes = new List<Lexeme>(); } else { choiceLexemes.Add(lexeme); } } // add last choice list choices.Add(choiceLexemes); // put in a choiceD ChoiceD choiceD = new ChoiceD(current); current.appendChild(choiceD); current = choiceD; // move down a level // add the concatenated items for each Choice foreach (List<Lexeme> concatLexemes in choices) { ConcatenationD concatenation = new ConcatenationD(current); foreach (Lexeme lexeme in concatLexemes) { if (lexeme.Type == LexemeType.Nonterminal) { // create nonterminal tail from the nonterminalhead NonterminalHeadD nontHead = getNonterminalHead(lexeme.Value); if (nontHead == null) throw new Exception("Could not find a defined rule for the Nonterminal: " + lexeme.Value); NonterminalTailD nontTail = new NonterminalTailD(nontHead, concatenation); // add the tail to current children concatenation.appendChild(nontTail); } else if (lexeme.Type == LexemeType.Terminal) { TerminalD terminalD = new TerminalD(lexeme.Value, concatenation); concatenation.appendChild(terminalD); } else if (lexeme.Type == LexemeType.Undefined) { UndefinedD undefinedD = new UndefinedD(concatenation); concatenation.appendChild(undefinedD); } else if (lexeme.Type != LexemeType.Concatenation) { // Lexes should only be of type Nont, T and Concat. Error throw new Exception("Lexeme was not of type Nonterminal, Terminal or Concatenation"); } } current.appendChild(concatenation); } }
/// <summary> /// Creates a new nonterminal and add's it to the defintive grammar. /// It's rule is a simple Nont -> ChoiceLineD -> ConcatD -> Undefined /// </summary> /// <param name="name"></param> public NonterminalHeadD CreateNewNonterminal(String name) { if (model.DefinitiveGrammar.getNonterminal(name) != null) throw new Exception("The given name already exists as a nonterminal, dummy"); // build nonterminal NonterminalHeadD newNonterminal = new NonterminalHeadD(name); ChoiceLineD choiceLineD = new ChoiceLineD(newNonterminal); newNonterminal.Rule = choiceLineD; ChoiceD choiceD = new ChoiceD(choiceLineD); newNonterminal.Rule.appendChild(choiceD); ConcatenationD concatD = new ConcatenationD(choiceD); choiceD.appendChild(concatD); concatD.appendChild(new UndefinedD(concatD)); // add to model model.DefinitiveGrammar.Nonterminals.Add(newNonterminal); return newNonterminal; }
public void Delete(GrammarNodeD grammarNodeD) { if (grammarNodeD.Parent is ConcatenationD) { ConcatenationD parent = grammarNodeD.Parent as ConcatenationD; // do the removal grammarNodeD.Dispose(); parent.Children.Remove(grammarNodeD); if (parent.Children.Count == 0) // add undefined parent.appendChild(new UndefinedD(parent)); } else if (grammarNodeD.Parent is ChoiceD) { // removing a choice ChoiceD parent = grammarNodeD.Parent as ChoiceD; // do removal grammarNodeD.Dispose(); parent.Children.Remove(grammarNodeD); if (parent.Children.Count == 0) { ConcatenationD concatD = new ConcatenationD(parent); parent.appendChild(concatD); concatD.appendChild(new UndefinedD(concatD)); } } else throw new Exception("The parent was not of type concatenation, I don't know what to do..."); }