Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 2.5 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 readonly Dictionary<ISymbol, string> localDefinitions;
30    private readonly Dictionary<ISymbol, List<Sequence>> alternatives;
31
32    public AttributedGrammar(ISymbol startSymbol, IEnumerable<ISymbol> nonTerminals, IEnumerable<ISymbol> terminals)
33      : base(startSymbol, nonTerminals, terminals) {
34      this.StartSymbol = startSymbol;
35      localDefinitions = nonTerminals.ToDictionary(nt => nt, nt => string.Empty);
36      alternatives = nonTerminals.ToDictionary(nt => nt, nt => new List<Sequence>());
37    }
38
39    public IEnumerable<Sequence> GetAlternativesWithSemanticActions(ISymbol ntSymbol) {
40      return alternatives[ntSymbol];
41    }
42    public Sequence GetAlternativeWithSemanticActions(ISymbol ntSymbol, int index) {
43      return alternatives[ntSymbol][index];
44    }
45
46    public override void AddProductionRule(ISymbol ntSymbol, Sequence production) {
47      base.AddProductionRule(ntSymbol, new Sequence(production.Where(symb => !(symb is SemanticSymbol)).ToList()));
48
49      IList<Sequence> list = alternatives[ntSymbol];
50
51      // make sure that there is not equivalent production registered already
52      Debug.Assert(!list.Any(s => s.SequenceEqual(production)));
53
54      list.Add(production);
55    }
56
57    public void AddLocalDefinitions(ISymbol ntSymbol, string localDefCode) {
58      Debug.Assert(!ntSymbol.Equals(EmptySymbol));
59
60      localDefinitions[ntSymbol] = localDefCode;
61    }
62    public string GetLocalDefinitions(ISymbol ntSymbol) {
63      return localDefinitions[ntSymbol];
64    }
65  }
66}
Note: See TracBrowser for help on using the repository browser.