private void HandleRulesWithSpecialConditions( ProductionRule rule, int begin, int part, int span, string key) { bool isBinary = rule.RHS.Length == 2; int[] tableKey = GetTableKey(begin + part, span - part); bool hasMatch = _table[tableKey].ContainsKey(rule.RHS[1].Text); if (isBinary && hasMatch) { bool satisfied = true; TableEntry left = _table[GetTableKey(begin, part)][key]; TableEntry right = _table[GetTableKey(begin + part, span - part)][rule.RHS[1].Text]; // Loop through all special conditions for (int i = 0; i < rule.Conditions.Length; i++) { SpecialCondition condition = rule.Conditions[i]; satisfied = condition.Check(left, right, _tokens); if (!satisfied) { break; } } // add entry to table if all rule conditions are satisfied if (satisfied) { AddEntry(new TableEntry(GetTableKey(begin, span), rule, new TableEntry[] { left, right })); } } }
// Procedura losuj¹ca zasadê wyprowadzania, która zostanie zastosowana // przy generowaniu nowego losowego wyrazu. private ProductionRule RandomProductionRule(char symbol) { ProductionRule[] rulesTempArray = new ProductionRule[productionRules.Length]; int i = 0; // Tworzenie tymczasowej tablicy, która zawiera zasady // wyprowadzania dla zadanego symbolu. foreach (var productionRule in productionRules) { if (productionRule.NonTerminal == symbol) { rulesTempArray[i++] = productionRule; } } // Je¿eli nie znaleziono zasady wyprowadzania dla danego // nieterminalu, to zwracany jest b³¹d. if (i == 0) { throw new System.Exception("Error! Grammar is not correct!"); } // Zwracanie losowej zasady z podanych. return(rulesTempArray[randomizer.Next(0, i)]); }
protected override CharsetNode CreateNonterminal(ProductionRule rule, IReadOnlyList <CharsetNode> nodes) { if (nodes.Count == 1) { return(nodes[0]); } switch (rule.ProductionSymbolId.ToInt32()) { case CharsetGrammar.SymNegateExpression: Debug.Assert(nodes.Count == 2); return(new CharsetNegate(nodes[1])); case CharsetGrammar.SymValueExpression: Debug.Assert(nodes.Count == 3); return(nodes[1]); case CharsetGrammar.SymUnionExpression: Debug.Assert(nodes.Count == 3); return(new CharsetUnion(nodes[0], nodes[2])); case CharsetGrammar.SymDifferenceExpression: Debug.Assert(nodes.Count == 3); return(new CharsetDifference(nodes[0], nodes[2])); case CharsetGrammar.SymIntersectExpression: Debug.Assert(nodes.Count == 3); return(new CharsetIntersection(nodes[0], nodes[2])); case CharsetGrammar.SymSubtractExpression: Debug.Assert(nodes.Count == 3); return(new CharsetSubtract(nodes[0], nodes[2])); } throw new InvalidOperationException("Unexpected non-terminal rule " + rule); }
public static ProductionRule operator%(Production p, Symbol s) { var r = new ProductionRule(p, s == null ? new Symbol[0] : new[] { s }); p._list.Add(r); return(r); }
public LRItem(ProductionRule rule, int marker, IEnumerable <Terminal_T> lookaheads = null, bool?isKernel = null) { Rule = rule; Marker = marker; IsKernel = isKernel ?? marker != 0; Lookaheads = new HashSet <Terminal_T>(lookaheads ?? Enumerable.Empty <Terminal_T>()); }
private void BuildTable() { // dynamic step -- build up to top of table for (int span = 2; span < _tokens.Length + 1; span++) { for (int begin = 0; begin < _tokens.Length - span + 1; begin++) { for (int part = 1; part < span; part++) { int[] tableKey = GetTableKey(begin, part); List <string> keyList = _table[tableKey].Keys.ToList(); for (int keyIndex = 0; keyIndex < keyList.Count; keyIndex++) { string key = keyList[keyIndex]; for (int ruleIndex = 0; ruleIndex < _grammar.RhsIndex[key].Count; ruleIndex++) { ProductionRule rule = _grammar.RhsIndex[key][ruleIndex]; #if DEBUG Console.WriteLine( string.Format("***** {0},{1},{2},{3},{4}", new object[] { span, begin, part, key, rule }) ); #endif bool hasConditions = rule.Conditions.Length > 0; bool isBinary = rule.RHS.Length == 2; int[] tableKey1 = GetTableKey(begin + part, span - part); bool hasMatch = isBinary && _table[tableKey1].ContainsKey(rule.RHS[1].Text); //handle matches for non-special rules (those that are in Conjunctive Normal Form and do not have any conditions) if (!hasConditions && isBinary && hasMatch) { TableEntry left = _table[GetTableKey(begin, part)][key]; TableEntry right = _table[GetTableKey(begin + part, span - part)][rule.RHS[1].Text]; #if DEBUG Console.WriteLine("MATCH"); Console.WriteLine("- left: " + left); Console.WriteLine("- right: " + right); Console.WriteLine("- parent: " + rule); #endif AddEntry(new TableEntry(GetTableKey(begin, span), rule, new TableEntry[] { left, right })); } else if (hasConditions) { HandleRulesWithSpecialConditions(rule, begin, part, span, key); } } } } } } }
public IApplicationParseNode CreateApplication(ProductionRule production, ParseNodePreference preference, IReadOnlyList<IParseNode> children) { Contract.Requires<ArgumentNullException>(production != null); Contract.Requires<InvalidEnumArgumentException>(Enum.IsDefined(typeof(ParseNodePreference), preference)); Contract.Requires<ArgumentNullException>(children != null); Contract.Ensures(Contract.Result<IApplicationParseNode>() != null); return default(IApplicationParseNode); }
public override string ToString() { return(ProductionRule.ToString() + (ProductionRule.Rhs.Count > 1 ? $"( {ProductionRule.Rhs[RulePartIndex].ToString()} )" : string.Empty) + $" {Position}" + $" :{Content}"); }
public override ReduceAction AddProductionRule(ProductionRule productionRule) { if (productionRule.Equals(this.ProductionRule)) { return(this); } return(new ReduceMultiAction(this.ProductionRule, productionRule)); }
public void SetUp() { mCfg = CfgBuilderGenerator.Generate().Build(); mParenthesizedProduction = new ProductionRule( Symbol.Of <Expression>(), new[] { Symbol.Of <SymLParen>(), Symbol.Of <Expression>(), Symbol.Of <SymRParen>() }, mDummyMethodInfo); }
public void LoadFooProduction() { var symbol = new Symbol(typeof(A)); var expected = new ProductionRule(symbol, new[] { symbol, symbol }, mFooInfo); var actual = ProductionRule.Load(mFooInfo); Assert.AreEqual(expected, actual); }
public static CfgBuilder AddAllProductionsInClass <T>(this CfgBuilder cfgBuilder) { foreach (var production in ProductionRule.LoadAllInClass <T>()) { cfgBuilder.AddProduction(production); } return(cfgBuilder); }
public void ReductionFoo() { var lhs = Node.Make(new A(42)); var rhs = Node.Make(new A(-41)); var production = ProductionRule.Load(mFooInfo); var product = production.Reduce(new[] { lhs, rhs }); Assert.AreEqual(1, (product.Payload as A).Val); }
public void LoadBarProduction() { var symbolA = new Symbol(typeof(A)); var symbolB = new Symbol(typeof(A.B)); var expected = new ProductionRule(symbolA, new[] { symbolA, symbolB, symbolA }, mBarInfo); var actual = ProductionRule.Load(mBarInfo); Assert.AreEqual(expected, actual); }
IEnumerator UpdateAxiom() { AxiomIsStepping = true; //Reset newAxiom newAxiom = ""; //Check each letter and replace it with the rule if it exists for (int i = 0; i < axiom.Length; i++) { char currentChar = axiom[i]; //Get the rules associated with the current char List <ProductionRule> currentRules = productionRuleOrganiser.GetRuleForChar(currentChar); if (currentRules != null) { bool aRuleWasApplied = false; //Check each rule to see if it is applicable for this char for (int j = 0; j < currentRules.Count; j++) { ProductionRule prodRule = currentRules[j]; //Get the different char for the rule char predecessor; char preLetter; char postLetter; GetLetters(out predecessor, out preLetter, out postLetter, i, axiom.Length); //Apply the rule to the predecessor, if the rule is applicable, apply it. string replacement = prodRule.ApplyRule(predecessor, preLetter, postLetter); if (replacement != null) { //Debug.Log("the rule " + replacement + " was applied on " + predecessor + " from " + prodRule.name); newAxiom += replacement; aRuleWasApplied = true; break; } } if (!aRuleWasApplied) { newAxiom += currentChar; } } else { newAxiom += currentChar; } if (i % 50 == 0) { yield return(1); } } axiom = newAxiom; visual.text = newAxiom; AxiomIsStepping = false; }
public void TestBuildingProductionRule() { string rule = "на случай дождя : если 'холодно - да' и 'влажность-высокая' то 'будет дождь - да', 'взять зонт - да'"; LogicalExpressionHelper eh = new LogicalExpressionHelper(); IBuilder <ILogicalStatement> builder = new ProductionRuleBuilder(eh); ProductionRule r = (ProductionRule)builder.Build( ProductionRuleGrammar.ProductionRule.Parse(rule)[0]); Assert.IsTrue(r.Actions.Count() == 2); }
internal AbstractSyntaxRuleNode(ProductionRule productionRule, int rulePartIndex, Position position, string content) : base(position, content) { Debug.Assert(productionRule != null, "Production Rule is required."); if (rulePartIndex >= productionRule.Rhs.Count) { throw new ArgumentException("Invalid rule part index.", nameof(rulePartIndex)); } ProductionRule = productionRule; RulePartIndex = rulePartIndex; }
public void ReductionBar() { var lhs = Node.Make(new A(1)); var mid = Node.Make(new A.B(2)); var rhs = Node.Make(new A(3)); var production = ProductionRule.Load(mBarInfo); var product = production.Reduce(new[] { lhs, mid, rhs }); Assert.AreEqual(6, (product.Payload as A).Val); }
public void LoadAllProductionsFromClass() { var expected = new[] { ProductionRule.Load(mFooInfo), ProductionRule.Load(mBarInfo) }; var actual = ProductionRule.LoadAllInClass(typeof(A)); Assert.IsTrue(actual.ToHashSet().SetEquals(expected)); }
//public ExamineExamplesDataGridItem(ExaminableExample examinedExample, ProductionRule productionRule, ArguedLearnableExample arguedExample) : this() public ExamineExamplesDataGridItem(ExaminableExample examinedExample, ProductionRule productionRule) : this() { foreach (var attributeValue in examinedExample.AllAttributes) { PredictiveAttributeValues.Add(attributeValue.Value); } DecisiveAttributeValue = examinedExample.DecisiveAttribute.Value; ExaminedAttributeValue = examinedExample.ExaminedAttribute.Value; ProductionRule = productionRule.GetStringValue(); //ArguedExample = arguedExample.BecauseExpression.GetValueString(); }
static public ProductionRule ReadProductionRule(string path) { string jsonText = HandleTextFile.ReadString(path); var parsedJson = JSON.Parse(jsonText); ProductionRule rule = ScriptableObject.CreateInstance <ProductionRule>(); rule.name = parsedJson["name"]; rule.SetPredecessor(ReadPredecessor(parsedJson["predecessor"])); rule.SetSuccesor(ReadSuccessor(parsedJson["successor"])); return(rule); }
public TableEntry(int[] key, ProductionRule rule, TableEntry[] children) { Key = key; Rule = rule; Base = rule.LHS.Text; Probability = rule.Probability; Children = children; //adjust probability using probabilities of children for (int i = 0; i < Children.Length; i++) { Probability *= Children[i].Probability; } }
private void AddEntry(TableEntry entry) { string entryBase = entry.Rule.LHS.Text; if (!_table.ContainsKey(entry.Key)) { _table.Add(entry.Key, new Dictionary <string, TableEntry>()); } // if table already has an entry with the same base, keep the more probable of the two if (_table[entry.Key].ContainsKey(entryBase)) { TableEntry oldEntry = _table[entry.Key][entryBase]; if (oldEntry.Probability < entry.Probability) { _table[entry.Key][entry.Base] = entry; #if DEBUG Console.WriteLine("OVERWRITING " + oldEntry + " WITH " + entry + " AT " + string.Format("({0},{1})", entry.Key[0], entry.Key[1])); #endif } #if DEBUG Console.WriteLine("FAILED TO OVERWRITE " + oldEntry + " WITH " + entry + " AT " + string.Format("({0},{1})", entry.Key[0], entry.Key[1])); #endif } else { _table[entry.Key].Add(entryBase, entry); #if DEBUG Console.WriteLine("ADDING " + entry + " AT " + string.Format("({0},{1})", entry.Key[0], entry.Key[1])); #endif } // handle alias rules (rules of the form A --> B) for (int i = 0; i < _grammar.RhsIndex[entry.Base].Count; i++) { ProductionRule rule = _grammar.RhsIndex[entry.Base][i]; if (rule.RHS.Length == 1) { AddEntry(new TableEntry(entry.Key, rule, new TableEntry[] { entry })); } } }
/// <summary> /// Получает возможные цели вывода. Используется в том случае, если в запросе не указано /// значение цели вывода. /// </summary> /// <param name="statement">Логичесткое утверждение.</param> /// <param name="target">Цель вывода.</param> /// <returns>Получает возможные цели вывода</returns> protected IData GetPossibleTarget(ILogicalStatement statement, IData target) { ProductionRule rule = statement as ProductionRule; if (rule != null) { foreach (var action in rule.Actions) { AddFactAction addFact = action as AddFactAction; if (addFact != null && addFact.Fact.Name == target.Name) { return(addFact.Fact); } } } return(null); }
public void AddRule(ProductionRule r) { float p = r.desiredProb; if (probMap.ContainsKey(r.pre.sym)) { r.prob = p / (1 - probMap[r.pre.sym]); probMap[r.pre.sym] += p; } else { r.prob = p; probMap.Add(r.pre.sym, p); } //Debug.Log("Desired: " + r.desiredProb + "\tEffective: " + r.prob); rules.Add(r); }
private static void ResolveProductionRule<T>(this IProductionRule rule, T entry, OilexerGrammarFile file, ICompilerErrorCollection errors) where T : IOilexerGrammarProductionRuleEntry { /* * * Copy the source, can't use just a standard IEnumerable because by design * it only enumerates the source when requested. If we tamper with the source, * before enumerating the transitionFirst time, the results are distorted. * */ IList<IProductionRuleItem> rCopy = new List<IProductionRuleItem>(rule); ProductionRule pr = rule as ProductionRule; IEnumerable<IProductionRuleItem> finalVersion = from item in rCopy select (item.ResolveProductionRuleItem(entry, file, errors)); pr.BaseCollection.Clear(); foreach (IProductionRuleItem iti in finalVersion) pr.BaseCollection.Add(iti); }
/// <summary> /// Возвращает <c>true</c>, если заданное правило содержит полностю заданную цель. /// </summary> /// <param name="statement">Логичесткое утверждение.</param> /// <param name="target">Цель вывода.</param> /// <returns>Возвращает <c>true</c>, если заданное правило содержит полностю заданную цель.</returns> protected bool FullyContainsTarget(ILogicalStatement statement, IData target) { ProductionRule rule = statement as ProductionRule; if (rule != null) { foreach (var action in rule.Actions) { AddFactAction addFact = action as AddFactAction; if (addFact != null && addFact.Fact.Name == target.Name && addFact.Fact.Value == target.Value) { return(true); } } } return(false); }
private void DoReduce(ProductionRule rule) { var currentState = this.context.currentState; // Take all AST nodes required for reduction from stack var nodes = new TAstNode[rule.RuleSymbolIds.Count]; for (var i = nodes.Length - 1; i >= 0; i--) { nodes[i] = currentState.Node; currentState = currentState.Parent; } var node = this.CreateNonterminal(rule, nodes); // Get the next transition key based on the item being reduced (should exist as goto in the table) and push onto the stack var topState = currentState.State; var newState = ((GotoAction)this.table.Action[new StateKey <SymbolId>(topState, rule.ProductionSymbolId)]).NewState; // Transition to the top state this.context.currentState = new ParserState <TAstNode>(node, newState, currentState); }
/// <summary> /// Процедура рекурсивного вывода заданной цели. /// </summary> /// <param name="target">Цель вывода.</param> /// <returns>Возвращает <c>true</c>, если вывод удачен.</returns> protected bool GetResultRec(IData target) { if (AlredySolved(target)) { return(true); } IEnumerable <ILogicalStatement> set = GetSetContainingTarget(target); foreach (var statement in set) { ProductionRule rule = (ProductionRule)statement; if (GetResultRec(rule.Condition)) { knBase.AddData(target); return(true); } } return(false); }
public LR1AutomataState(int stateName, ProductionRule header, List <ProductionRule> contents = null, Dictionary <Symbol, ProductionRule> kernelTransitions = null) { StateName = stateName; Header = header; Explored = false; Contents = contents; RuleToReduce = null; if (Contents == null) { Contents = new List <ProductionRule>(); } KernelTransitions = kernelTransitions; if (KernelTransitions == null) { KernelTransitions = new Dictionary <Symbol, ProductionRule>(); } }
/// <summary> /// Applies the suppled production rule starting at token index /// This will recursively call further production rules /// </summary> /// <returns><c>true</c>, if rule was applied, <c>false</c> otherwise.</returns> /// <param name="r">The red component.</param> /// <param name="tokenIndex">Token index.</param> /// <param name="indent">Indent.</param> bool ApplyRule(ProductionRule r, ref int tokenIndex, int indent = 0) { Log("Applying " + r.ToString() + " to " + string.Join(", ", _tokens.GetRange(tokenIndex, _tokens.Count - tokenIndex).Select(t => $"{t.TokenType.Name} ({t.Value})")), indent); _rulesStack.Push(r.Name); var incomingTokenIndex = tokenIndex; // In order to pass this rule one of the RuleParts must evaluate bool evaluatedRuleSuccessfully = false; ProductionRulePart successfullyEvaluatedRulePart = null; for (int i = 0; i < r.Rhs.Count; i++) { // Take a copy of tokens. if (ApplyRulePart(r.Rhs[i], ref tokenIndex, indent)) { // We have passed - so can break out. // Otherwise, try the next rule part evaluatedRuleSuccessfully = true; successfullyEvaluatedRulePart = r.Rhs[i]; break; } } if (evaluatedRuleSuccessfully) { var nodePosition = GetRuleNodePosition(tokenIndex, incomingTokenIndex); _treeNodeStack.Push( new AbstractSyntaxRuleNode( r, r.Rhs.IndexOf(successfullyEvaluatedRulePart), nodePosition, _source.Substring(nodePosition.Index, nodePosition.Length) ) ); } else { // restore the index - we need to try a different rule tokenIndex = incomingTokenIndex; } _rulesStack.Pop(); return(evaluatedRuleSuccessfully); }
/// <summary> /// Checks whether the production is a completion production, and if so, checks /// the boundaries for the completion region, the parser location and the cursor /// location, and whether the parser is in completion mode. /// Otherwise, when the production is not a completion production, /// checks whether it is a recover production, or the parser is in fine grained recovery mode. /// </summary> /// <param name="stack"></param> /// <param name="production"></param> /// <returns></returns> private bool RecoverModeOk(Frame stack, ProductionRule production) { if (!production.IsCompletion) return !production.IsRecover || isFineGrainedMode; // TODO: isFineGrainedMode return InCompletionMode(production); }
/// <summary> /// Ajouter la production en entée à l'ensemble des regles des productions /// </summary> /// <param name="production"></param> /// <returns></returns> public int AddProduction(ProductionRule production) { if (getProductions(production.A).Contains(production.W + production.B)) return 1; // production existe if (!this.V.Contains(production.A) || (!this.V.Contains(production.B) && production.B != NOVARIABLE)) return -2; //erreur: Variable n'existe pas if (!InAlphabet(production.W)) return -3; //error: word not in X this.P[toIndex(production.A)].Add(production.W + production.B); if (!this.Read.Contains(production.W) && (production.W.Length > 1)) this.Read.Add(production.W); return 0; //instruction ajoutée avec succée }
/// <summary> /// supprimer la regle de l'ensemble des regles des productions /// </summary> /// <param name="production"></param> public void RemoveProduction(ProductionRule production) { this.P[toIndex(production.A)].Remove(production.W + production.B); }
private bool InCompletionMode(ProductionRule production) { // TODO return false; }
/// <summary> /// /// </summary> /// <param name="production"></param> /// <param name="kids"></param> /// <param name="line"></param> /// <param name="column"></param> /// <param name="isLayout"></param> /// <param name="isIgnoreLayout"></param> /// <returns></returns> private IParseNode ApplyProduction(ProductionRule production, IReadOnlyList<IParseNode> kids, int line, int column, bool isLayout, bool isIgnoreLayout) { // TODO: Use other parameters. switch (production.Type) { case ProductionType.None: return this.parseNodeFactory.CreateApplication(production, ParseNodePreference.None, kids); case ProductionType.Prefer: return this.parseNodeFactory.CreateApplication(production, ParseNodePreference.Prefer, kids); case ProductionType.Avoid: return this.parseNodeFactory.CreateApplication(production, ParseNodePreference.Avoid, kids); case ProductionType.Reject: var node = this.parseNodeFactory.CreateApplication(production, ParseNodePreference.None, kids); node.Reject(); return node; default: throw new NotSupportedException(); } }
private void HandleRulesWithSpecialConditions( ProductionRule rule, int begin, int part, int span, string key) { bool isBinary = rule.RHS.Length == 2; int[] tableKey = GetTableKey(begin + part, span - part); bool hasMatch = _table[tableKey].ContainsKey(rule.RHS[1].Text); if (isBinary && hasMatch) { bool satisfied = true; TableEntry left = _table[GetTableKey(begin, part)][key]; TableEntry right = _table[GetTableKey(begin + part, span - part)][rule.RHS[1].Text]; // Loop through all special conditions for (int i = 0; i < rule.Conditions.Length; i++) { SpecialCondition condition = rule.Conditions[i]; satisfied = condition.Check(left, right, _tokens); if (!satisfied) break; } // add entry to table if all rule conditions are satisfied if (satisfied) AddEntry(new TableEntry(GetTableKey(begin, span), rule, new TableEntry[] { left, right })); } }
/// <inheritdoc /> public IApplicationParseNode CreateApplication(ProductionRule production, ParseNodePreference preference, IReadOnlyList<IParseNode> children) { // CONTRACT: Inherited from IParseNodeFactory return new ApplicationParseNode(production, preference, children); }
public TableEntry(int[] key, ProductionRule rule) : this(key, rule, new TableEntry[0]) { }