Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/AttributedGrammar.cs @ 10067

Last change on this file since 10067 was 10067, checked in by gkronber, 11 years ago

#2026 worked on brute force solver for GPDL problems.

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Linq;
26
27namespace HeuristicLab.Grammars {
28  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>>();
31
32    public AttributedGrammar(ISymbol startSymbol)
33      : base(startSymbol) {
34      Debug.Assert(!startSymbol.Equals(EmptySymbol));
35      this.StartSymbol = startSymbol;
36    }
37
38    public IEnumerable<Sequence> GetAlternativesWithSemanticActions(ISymbol ntSymbol) {
39      Debug.Assert(!ntSymbol.Equals(EmptySymbol));
40      return alternatives[ntSymbol];
41    }
42    public Sequence GetAlternativeWithSemanticActions(ISymbol ntSymbol, int index) {
43      Debug.Assert(!ntSymbol.Equals(EmptySymbol));
44      return alternatives[ntSymbol][index];
45    }
46
47    public override void AddProductionRule(ISymbol ntSymbol, Sequence production) {
48      base.AddProductionRule(ntSymbol, new Sequence(production.Where(symb => !(symb is SemanticSymbol)).ToList()));
49
50      IList<Sequence> list;
51      if (!alternatives.TryGetValue(ntSymbol, out list)) {
52        list = new List<Sequence>();
53        alternatives.Add(ntSymbol, list);
54      }
55      // make sure that there is not equivalent production registered already
56      Debug.Assert(!list.Any(s => s.SequenceEqual(production)));
57
58      list.Add(production);
59    }
60
61    public void AddLocalDefinitions(ISymbol ntSymbol, string localDefCode) {
62      Debug.Assert(!ntSymbol.Equals(EmptySymbol));
63      Debug.Assert(!localDefinitions.ContainsKey(ntSymbol));
64
65      localDefinitions.Add(ntSymbol, localDefCode);
66    }
67    public string GetLocalDefinitions(ISymbol ntSymbol) {
68      string code;
69      if (localDefinitions.TryGetValue(ntSymbol, out code)) return code;
70      else return string.Empty;
71    }
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    //}
99  }
100}
Note: See TracBrowser for help on using the repository browser.