Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/27/13 20:17:17 (10 years ago)
Author:
gkronber
Message:

#2026 worked on random search solver (now all examples are working)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/AttributedGrammar.cs

    r10067 r10086  
    2727namespace HeuristicLab.Grammars {
    2828  public class AttributedGrammar : Grammar {
    29     private Dictionary<ISymbol, string> localDefinitions = new Dictionary<ISymbol, string>();
    30     private Dictionary<ISymbol, IList<Sequence>> alternatives = new Dictionary<ISymbol, IList<Sequence>>();
     29    private readonly Dictionary<ISymbol, string> localDefinitions;
     30    private readonly Dictionary<ISymbol, List<Sequence>> alternatives;
    3131
    32     public AttributedGrammar(ISymbol startSymbol)
    33       : base(startSymbol) {
    34       Debug.Assert(!startSymbol.Equals(EmptySymbol));
     32    public AttributedGrammar(ISymbol startSymbol, IEnumerable<ISymbol> nonTerminals, IEnumerable<ISymbol> terminals)
     33      : base(startSymbol, nonTerminals, terminals) {
    3534      this.StartSymbol = startSymbol;
     35      localDefinitions = nonTerminals.ToDictionary(nt => nt, nt => string.Empty);
     36      alternatives = nonTerminals.ToDictionary(nt => nt, nt => new List<Sequence>());
    3637    }
    3738
    3839    public IEnumerable<Sequence> GetAlternativesWithSemanticActions(ISymbol ntSymbol) {
    39       Debug.Assert(!ntSymbol.Equals(EmptySymbol));
    4040      return alternatives[ntSymbol];
    4141    }
    4242    public Sequence GetAlternativeWithSemanticActions(ISymbol ntSymbol, int index) {
    43       Debug.Assert(!ntSymbol.Equals(EmptySymbol));
    4443      return alternatives[ntSymbol][index];
    4544    }
     
    4847      base.AddProductionRule(ntSymbol, new Sequence(production.Where(symb => !(symb is SemanticSymbol)).ToList()));
    4948
    50       IList<Sequence> list;
    51       if (!alternatives.TryGetValue(ntSymbol, out list)) {
    52         list = new List<Sequence>();
    53         alternatives.Add(ntSymbol, list);
    54       }
     49      IList<Sequence> list = alternatives[ntSymbol];
     50
    5551      // make sure that there is not equivalent production registered already
    5652      Debug.Assert(!list.Any(s => s.SequenceEqual(production)));
     
    6157    public void AddLocalDefinitions(ISymbol ntSymbol, string localDefCode) {
    6258      Debug.Assert(!ntSymbol.Equals(EmptySymbol));
    63       Debug.Assert(!localDefinitions.ContainsKey(ntSymbol));
    6459
    65       localDefinitions.Add(ntSymbol, localDefCode);
     60      localDefinitions[ntSymbol] = localDefCode;
    6661    }
    6762    public string GetLocalDefinitions(ISymbol ntSymbol) {
    68       string code;
    69       if (localDefinitions.TryGetValue(ntSymbol, out code)) return code;
    70       else return string.Empty;
     63      return localDefinitions[ntSymbol];
    7164    }
    72 
    73 
    74 
    75     //private static Regex ruleExpr = new Regex(@"\s*(?<ntSymbol>\w+)\s*(?<formPar>\<\w+\>)+\s*->\s*(?<alternative>\w+\s*(?<actPar>\<\w+\>)+(?:\s+\w+\s*(?<actPar>\<\w+\>)+)*)(?:\s*\|\s*(?<alternative>\w+\s*(?<formPar>\<\w+\>)+(?:\s+\w+\s*(?<formPar>\<\w+\>)+)*))*");
    76     //private static Regex empty = new Regex(@"^\s*$");
    77     //public static Grammar FromString(string gStr) {
    78     //  var lines = gStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    79     //  lines = lines.Where(l => !empty.IsMatch(l)).ToArray(); // remove empty lines
    80 
    81     //  // first line is the rule for the start-symbol
    82     //  var m = ruleExpr.Match(lines.First());
    83     //  var startSymbol = new Symbol(m.Groups["ntSymbol"].Value);
    84 
    85     //  var g = new Grammar(startSymbol);
    86     //  foreach (var alt in m.Groups["alternative"].Captures) {
    87     //    g.AddProductionRule(startSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n))));
    88     //  }
    89     //  foreach (var line in lines.Skip(1)) {
    90     //    m = ruleExpr.Match(line);
    91     //    var ntSymbol = new Symbol(m.Groups["ntSymbol"].Value);
    92     //    foreach (var alt in m.Groups["alternative"].Captures) {
    93     //      g.AddProductionRule(ntSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n))));
    94     //    }
    95     //  }
    96 
    97     //  return g;
    98     //}
    9965  }
    10066}
Note: See TracChangeset for help on using the changeset viewer.