public OperatorDetails(string precedenceLevel, AssociativityType associativityType, Action <Grammar, string, Operand, Operand> ag, string closer) { Associativity = associativityType; PrecedenceLevel = precedenceLevel; Goal = closer; ReduceActionGen = ag; }
public Operator(int precedence, AssociativityType associativity) : this(precedence, (int)associativity) { }
public void GenerateInternal(AnalyzerState state, int priority, AssociativityType associativity) { var shifts = new MultiValueDictionary <TerminalPredicate, Item>(); var reduces = new Dictionary <TerminalPredicate, ProductionRule>(); var gotos = new MultiValueDictionary <object, Item>(); foreach (var item in state.ItemSet) { if (item.Cursor < item.ProductionRule.Length) { switch (item.ProductionRule.Predicates[item.Cursor]) { case TerminalPredicate tp: shifts.Add(tp, Item.Advance(item)); break; case KeyedNonTerminalPredicate kp: gotos.Add(kp.Key, Item.Advance(item)); break; default: throw new GeneratorException(); } } else { var follow = FollowSets[item.ProductionRule.Key]; foreach (var syntaxPredicate in follow.OfType <TerminalPredicate>()) { if (reduces.ContainsKey(syntaxPredicate)) { throw new GeneratorException("Reduce-reduce conflict"); } reduces.Add(syntaxPredicate, item.ProductionRule); } } } var s_r_conflict = new List <TerminalPredicate>(shifts.Keys.Intersect(reduces.Keys)); foreach (var terminalPredicate in s_r_conflict) { if (terminalPredicate.Precedence > priority) { state.Action.Add(terminalPredicate, new ShiftOperation() { NextState = ResolveAnalyzerState(CommonUtils.Closure(new HashSet <Item>(shifts[terminalPredicate]), ProductionDict), terminalPredicate.Precedence, terminalPredicate.Associativity) }); continue; } else if (terminalPredicate.Precedence == priority) { if (terminalPredicate.Associativity == AssociativityType.Right) { state.Action.Add(terminalPredicate, new ShiftOperation() { NextState = ResolveAnalyzerState(CommonUtils.Closure(new HashSet <Item>(shifts[terminalPredicate]), ProductionDict), terminalPredicate.Precedence, terminalPredicate.Associativity) }); continue; } } state.Action.Add(terminalPredicate, new ReduceOperation() { ReduceBy = reduces[terminalPredicate] }); } foreach (var shift in shifts.Keys.Except(s_r_conflict)) { state.Action.Add(shift, new ShiftOperation() { NextState = ResolveAnalyzerState(CommonUtils.Closure(new HashSet <Item>(shifts[shift]), ProductionDict), shift.Precedence, shift.Associativity) }); } foreach (var predicate in reduces.Keys.Except(s_r_conflict)) { state.Action.Add(predicate, new ReduceOperation() { ReduceBy = reduces[predicate] }); } foreach (var key in gotos) { state.GotoTable.Add(key.Key, ResolveAnalyzerState(CommonUtils.Closure(new HashSet <Item>(key.Value), ProductionDict), priority, associativity)); } }
private AnalyzerState ResolveAnalyzerState(HashSet <Item> itemSet, int precedence, AssociativityType associativity) { if (ItemSets.TryGetValue(itemSet, out var state)) { return(state); } state = new AnalyzerState() { ItemSet = itemSet }; ItemSets.Add(itemSet, state); GenerateInternal(state, precedence, associativity); return(state); }