public void CreateTable() { //• Para toda aresta I X−→ J Na linha I coluna X //insira: – shift J se X ´e terminal // – goto J se X ´e n˜ao-terminal • // S’ → S.$ – insira accept //• A → α. – insira reduce para todo token // A tabela LR(1) ´e constru´ıda da mesma forma que a LR(0) exceto na maneira de inserir os //reduces (redu¸c˜oes). Quando h´a um ´ıtem [A → α., a] em um estado i, //ent˜ao deve-se inserir na linha i coluna a a redu¸c˜ao da regra de produ¸c˜ao A → α. var symbols = GetOrdenedSymbols(); foreach (var state in Automa.States) { Table.Add(state, new Dictionary <Symbol, List <Operation> >()); foreach (var symbol in symbols) { Table[state].Add(symbol, new List <Operation>()); } ValueState val = state.GetValue <ValueState>(); foreach (var rule in val.Rules) { Operation operation = new Operation(state, rule); operation.Pertinence = 1; operation.Symbol = rule.Source; if (rule.Pointer == rule.Destiny.Count) { if (rule.Source == SymbolInitialLine && rule.Destiny.Count == 1 && rule.Destiny[0] == SymbolInitial) { operation.Type = TypeOperation.Acept; } else { operation.Type = TypeOperation.Reduce; } Table[state][rule.Lookahead].Add(operation); } } } foreach (var transition in Automa.Transitions) { ValueState valTo = transition.To.GetValue <ValueState>(); ValueState valFrom = transition.To.GetValue <ValueState>(); foreach (var rule in valTo.Rules) { Operation op = new Operation(transition.To, rule); op.Pertinence = 1; op.Symbol = rule.Source; if (transition.Rule.Symbol.Terminal) { op.Type = TypeOperation.Shift; } else { op.Type = TypeOperation.Goto; op.State = transition.To; } if (!Table[transition.From][transition.Rule.Symbol].Contains(op)) { Table[transition.From][transition.Rule.Symbol].Add(op); } } } }
private void CreateAutoma() { ValueState value = new ValueState(); RuleProductionState rule = new RuleProductionState(); rule.Source = GrammarLine.VariableStart; rule.Destiny = GrammarLine.GetRules(rule.Source)[0].Destiny; rule.Id = GrammarLine.GetRules(rule.Source)[0].Id; rule.TypeName = GrammarLine.GetRules(rule.Source)[0].TypeName; rule.Pointer = 0; rule.Pertinence = 1; rule.Lookahead = Symbol.TapeFinal; rule.Parent = GrammarLine.GetRules(rule.Source)[0].Parent; value.Rules.Add(rule); State <Symbol> state = new State <Symbol>(0, "I000", 1, 0, Closure(value)); FirstState = state; // Rules.AddRange(state.GetValue<ValueState>().Rules); Automa.States.Add(state); bool change = true; // while (change) { change = false; for (int i = 0; i < Automa.States.Count; i++) { var stateActualI = Automa.States[i]; SymbolList symbols = Grammar.Symbols; for (int j = 0; j < symbols.Count; j++) { var symbolX = symbols[j]; var valStateI = stateActualI.GetValue <ValueState>(); var valueGoto = Goto(valStateI, symbolX); if (valueGoto.Rules.Count > 0) { //var item = value.Rules // foreach (var item in value.Rules) { state = Automa.States.FirstOrDefault(s => s.GetValue <ValueState>().Equals(valueGoto)); if (state == null) { state = new State <Symbol>(-1, "Temp", 1, 0, valueGoto); state.Name = string.Format("I{0:000}", state.Id); Automa.States.Add(state); } // Rules.AddRange(value.Rules); Transition <Symbol> transition = new Transition <Symbol>(); transition.From = stateActualI; stateActualI.Transitions.Add(transition); transition.To = state; var sir = new SimpleIncludeRule <Symbol>(symbolX, 1); // sir.Pertinence = item.Pertinence * Automa.States[i].GetValue<ValueState>().Rules[0].Pertinence; transition.Rule = sir; Automa.Transitions.Add(transition); change = true; } } } } } }
public ValueState Closure(ValueState state) { // ValueState returns = new ValueState(); bool change = true; //while (change) { change = false; // for (int i = 0; i < state.Rules.Count; i++) { //A->alphaBbeta, a Symbol A = state.Rules[i].Source; List <Symbol> alpha = state.Rules[i].Destiny.Take(state.Rules[i].Pointer).ToList(); if (alpha.Count == 0) { alpha.Add(Symbol.EmptySymbol); } Symbol B = Symbol.EmptySymbol; if (state.Rules[i].Destiny.Count > state.Rules[i].Pointer) { B = state.Rules[i].Destiny[state.Rules[i].Pointer]; } Symbol beta = Symbol.TapeFinal; if (state.Rules[i].Pointer + 1 < state.Rules[i].Destiny.Count) { beta = state.Rules[i].Destiny[state.Rules[i].Pointer + 1]; } Symbol a = state.Rules[i].Lookahead; var rolesB = GrammarLine.GetRules(B); for (int j = 0; j < rolesB.Count; j++) { var first = First(beta, a); for (int k = 0; k < first.Count; k++) { if (first[k].Terminal || first[k] == Symbol.TapeFinal) { RuleProductionState rule = new RuleProductionState(); rule.Id = rolesB[j].Id; rule.TypeName = rolesB[j].TypeName; rule.Source = B; rule.Destiny.AddRange(rolesB[j].Destiny); rule.Pointer = 0; rule.Lookahead = first[k]; rule.Parent = rolesB[j].Parent; rule.CalculateHash(); // state.Rules[i].Pertinence; if (!state.HashCodeRules.ContainsKey(rule.HashCode)) //if (!state.Rules.Any(r => r.Equals(rule))) { rule.Pertinence = rolesB[j].Pertinence;// *state.Rules[i].Pertinence;// 0.8; state.AddRule(rule); change = true; } } } } } } return(state); }