public void AddGoto(Symbol s, State next) { this.Goto[s] = next; if (s is Terminal) terminalTransitions.Add((Terminal)s); else nonTerminalTransitions.Add((NonTerminal)s, new Transition(this, (NonTerminal)s, next)); }
private void ExpandState(Symbol sym, State newState) { newState.accessedBy = sym; this.states.Add(newState); if (!this.accessedBy.ContainsKey(sym)) { this.accessedBy[sym] = new List<State>(); } this.accessedBy[sym].Add(newState); newState.AddClosure(); this.ComputeGoto(newState); }
private void ComputeGoto(State state) { foreach (ProductionItem current in state.all_items) { if (!current.expanded && !current.isReduction()) { current.expanded = true; Symbol symbol = current.production.rhs[current.pos]; List<ProductionItem> list = new List<ProductionItem>(); list.Add(new ProductionItem(current.production, current.pos + 1)); foreach (ProductionItem current2 in state.all_items) { if (!current2.expanded && !current2.isReduction()) { Symbol symbol2 = current2.production.rhs[current2.pos]; if (symbol == symbol2) { current2.expanded = true; list.Add(new ProductionItem(current2.production, current2.pos + 1)); } } } State state2 = this.FindExistingState(symbol, list); if (state2 == null) { State state3 = new State(list); state.AddGoto(symbol, state3); this.ExpandState(symbol, state3); } else { state.AddGoto(symbol, state2); } } } }
private void ComputeGoto(State state) { foreach (ProductionItem item in state.all_items) if (!item.expanded && !item.isReduction()) { item.expanded = true; Symbol s1 = item.production.rhs[item.pos]; // Create itemset for new state ... List<ProductionItem> itemSet = new List<ProductionItem>(); itemSet.Add(new ProductionItem(item.production, item.pos+1)); foreach (ProductionItem item2 in state.all_items) if (!item2.expanded && !item2.isReduction()) { Symbol s2 = item2.production.rhs[item2.pos]; if (s1 == s2) { item2.expanded = true; itemSet.Add(new ProductionItem(item2.production, item2.pos+1)); } } State existingState = FindExistingState(s1, itemSet); if (existingState == null) { State newState = new State(itemSet); state.AddGoto(s1, newState); ExpandState(s1, newState); } else state.AddGoto(s1, existingState); } }
public Transition(State p, NonTerminal A, State next) { this.p = p; this.A = A; this.next = next; }
private int GetDefaultAction(State state) { IEnumerator<ParserAction> enumerator = state.parseTable.Values.GetEnumerator(); enumerator.MoveNext(); int defaultAction = enumerator.Current.ToNum(); if (defaultAction > 0) return 0; // can't have default shift action foreach (KeyValuePair<Terminal, ParserAction> transition in state.parseTable) if (transition.Value.ToNum() != defaultAction) return 0; return defaultAction; }
private void GenerateState(int state_nr, State state) { Console.Write(" AddState({0},new State(", state_nr); int defaultAction = GetDefaultAction(state); if (defaultAction != 0) Console.Write(defaultAction); else { Console.Write("new int[]{"); bool first = true; foreach (KeyValuePair<Terminal, ParserAction> transition in state.parseTable) { if (!first) Console.Write(","); Console.Write("{0},{1}", transition.Key.num, transition.Value.ToNum()); first = false; } Console.Write('}'); } if (state.nonTerminalTransitions.Count > 0) { Console.Write(",new int[]{"); bool first = true; foreach (Transition transition in state.nonTerminalTransitions.Values) { if (!first) Console.Write(","); Console.Write("{0},{1}", transition.A.num, transition.next.num); first = false; } Console.Write('}'); } Console.WriteLine("));"); }
public Shift(State next) { this.next = next; }
private int GetDefaultAction(State state) { IEnumerator<ParserAction> enumerator = state.parseTable.Values.GetEnumerator(); enumerator.MoveNext(); int num = enumerator.Current.ToNum(); if (num > 0) { return 0; } foreach (KeyValuePair<Terminal, ParserAction> current in state.parseTable) { if (current.Value.ToNum() != num) { return 0; } } return num; }
public ShiftReduceConflict(Terminal sy, string s1, string s2, State from, State to) : base(sy, s1, s2) { fromState = from; toState = to; }
private State PathTo(State q, Production prod, int prefix) { // q -> prod.rhs[0] ... prod.rhs[prefix] -> ??? for (int i = 0; i < prefix; i++) { Symbol s = prod.rhs[i]; if (q.Goto.ContainsKey(s)) q = q.Goto[s]; else return null; } return q; }
private void GenerateState(State state) { int defaultAction = state.GetDefaultAction(); Console.Write(" "); // default action is always < 0 (reduction) if (defaultAction == 0 || state.nonTerminalTransitions.Count > 0) { // actions: if (defaultAction == 0) { Console.Write("{0},", state.parseTable.Count); } else { Console.Write("0,"); } // gotos: Console.Write("{0},", state.nonTerminalTransitions.Count); } if (defaultAction == 0) { Console.Write(" /* actions: */ "); foreach (KeyValuePair<Terminal, ParserAction> transition in state.parseTable) { Console.Write("{0},{1},", transition.Key.num, transition.Value.ToNum()); } } else { Console.Write(" /* default action: */ "); Console.Write("{0},", defaultAction); } if (state.nonTerminalTransitions.Count > 0) { Console.Write(" /* gotos: */ "); foreach (Transition transition in state.nonTerminalTransitions.Values) { Console.Write("{0},{1},", transition.A.num, transition.next.num); } } Console.WriteLine(); }
private State PathTo(State q, Production prod, int prefix) { for (int i = 0; i < prefix; i++) { Symbol key = prod.rhs[i]; if (!q.Goto.ContainsKey(key)) { return null; } q = q.Goto[key]; } return q; }