/// <summary> /// indirect way of add production by string, format string as: /// Pre ->|:|| Suf0 Suf1 ... [@SemantDelegateName[$attr]] /// like this: /// E -> F G /// or like this: /// E -> E add E @SemantAdd /// or like this: /// E | E opt E @SementOpt /// or like this: /// E : Delimiter E Delimiter @Semant /// or with attr in string (attr will be string type), just add $attr directly behand @SemantDelegateName /// E -> E F @Semant$deal /// </summary> /// <param name="productSentence">formated string</param> /// <exception cref="ProductSentenceException">if productSentence is illeagal</exception> public void AddProductionByString(string productSentence) { string[] vs = productSentence.Split(" "); if (vs[1] != "->" && vs[1] != "|" && vs[1] != ":") { throw new ProductSentenceException(productSentence, "ProductSentence Error: sentence \"" + productSentence + "\" is illeagal."); } int i = vs.Length - 1; SemantDelegate semantDelegate = null; object attr = null; if (vs[i].StartsWith('@')) { string[] s = vs[i].TrimStart('@').Split("$", 2); semantDelegate = SemantDelegateTable[s[0]]; if (s[1].Length > 0) { attr = s[1]; } i--; } string[] sufs = new string[i]; for (int j = 2; j <= i; j++) { sufs[j - 2] = vs[j]; } Production production = new Production(vs[0], sufs, semantDelegate, attr); AddProduction(production); }
/// <summary> /// the production of grammar /// </summary> /// <param name="pre">antecedent</param> /// <param name="sufs">seccedents</param> /// <param name="semantDelegate">delegate for semanting</param> /// <param name="attr">attr for advansing usage</param> public Production(string pre, string[] sufs, SemantDelegate semantDelegate, object attr) { Pre = pre; Sufs = sufs; SemantDelegate = semantDelegate; Attr = attr; }
/// <summary> /// part of indirect way of add production, add the SemantDelegate to table. /// </summary> /// <param name="name">name of delegate method</param> /// <param name="semantDelegate">the delegate method</param> /// <exception cref="ProductSentenceException">if ProductSentence is illeagal</exception> public void AddSemantDelegateTable(string name, SemantDelegate semantDelegate) => SemantDelegateTable.Add(name, semantDelegate);