Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Grammar.cs @ 10149

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

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

File size: 5.5 KB
RevLine 
[10031]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;
[10051]24using System.Diagnostics;
[10031]25using System.Linq;
26using System.Text.RegularExpressions;
27
28namespace HeuristicLab.Grammars {
29  public class Grammar : IGrammar {
[10051]30    public static readonly ISymbol EmptySymbol = new Symbol("EPS");
[10086]31    private readonly Dictionary<ISymbol, List<Sequence>> rules;
32    private readonly HashSet<ISymbol> allSymbols;
[10031]33
[10051]34    public ISymbol StartSymbol { get; set; }
35    public IEnumerable<ISymbol> TerminalSymbols { get { return allSymbols.Except(NonTerminalSymbols); } }
36    public IEnumerable<ISymbol> NonTerminalSymbols { get { return rules.Keys; } }
37    public IEnumerable<ISymbol> Symbols { get { return allSymbols; } }
[10031]38
[10086]39    public Grammar(ISymbol startSymbol, IEnumerable<ISymbol> nonTerminals, IEnumerable<ISymbol> terminals) {
[10051]40      Debug.Assert(startSymbol != EmptySymbol);
[10031]41      this.StartSymbol = startSymbol;
[10086]42      this.allSymbols = new HashSet<ISymbol>(nonTerminals.Concat(terminals));
43      this.rules = nonTerminals.ToDictionary(nt => nt, nt => new List<Sequence>());
[10031]44    }
45
[10051]46    public int NumberOfAlternatives(ISymbol ntSymbol) {
[10031]47      return rules[ntSymbol].Count;
48    }
49
[10051]50    public IEnumerable<Sequence> GetAlternatives(ISymbol ntSymbol) {
[10031]51      return rules[ntSymbol].AsReadOnly();
52    }
53
[10051]54    public Sequence GetAlternative(ISymbol ntSymbol, int index) {
[10031]55      return rules[ntSymbol][index];
56    }
57
[10067]58    public virtual void AddProductionRule(ISymbol ntSymbol, Sequence production) {
[10051]59      Debug.Assert(ntSymbol != EmptySymbol);
[10086]60      Debug.Assert(rules.ContainsKey(ntSymbol));
61      Debug.Assert(production.All(s => allSymbols.Contains(s)));
[10051]62
[10086]63      var l = rules[ntSymbol];
[10051]64      Debug.Assert(!l.Any(s => s.SequenceEqual(production)));
65
[10031]66      l.Add(production);
67    }
68
[10051]69    public bool IsTerminal(ISymbol symbol) {
[10086]70      // terminals must not have rules but must occur in the set of all symbols
[10051]71      return !rules.ContainsKey(symbol) && allSymbols.Contains(symbol);
72    }
[10086]73
[10051]74    public bool IsNonTerminal(ISymbol symbol) {
75      return rules.ContainsKey(symbol);
76    }
77
[10031]78    private static Regex ruleExpr = new Regex(@"\s*(?<ntSymbol>\w+)\s*->\s*(?<alternative>\w+(?:\s+\w+)*)(?:\s*\|\s*(?<alternative>\w+(?:\s+\w+)*))*");
79    private static Regex empty = new Regex(@"^\s*$");
[10051]80    public static Grammar FromString(string gStr) {
[10031]81      var lines = gStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
82      lines = lines.Where(l => !empty.IsMatch(l)).ToArray(); // remove empty lines
83
[10086]84      // make two passes: 1) find all symbols 2) add production rules
85      var nonTerminals = new List<ISymbol>();
86      var allSymbols = new List<ISymbol>();
87
[10031]88      // first line is the rule for the start-symbol
89      var m = ruleExpr.Match(lines.First());
[10051]90      var startSymbol = new Symbol(m.Groups["ntSymbol"].Value);
[10086]91
92      nonTerminals.Add(startSymbol);
93      allSymbols.Add(startSymbol);
94
95      // parse first line
[10031]96      foreach (var alt in m.Groups["alternative"].Captures) {
[10086]97        foreach (var s in alt.ToString()
98          .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
99          .Select(n => new Symbol(n))) allSymbols.Add(s);
[10031]100      }
[10086]101      // parse all remaining lines
[10031]102      foreach (var line in lines.Skip(1)) {
103        m = ruleExpr.Match(line);
[10051]104        var ntSymbol = new Symbol(m.Groups["ntSymbol"].Value);
[10086]105        nonTerminals.Add(ntSymbol);
106        allSymbols.Add(ntSymbol);
107
[10031]108        foreach (var alt in m.Groups["alternative"].Captures) {
[10086]109          foreach (var s in alt.ToString()
110          .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
111          .Select(n => new Symbol(n))) allSymbols.Add(s);
[10031]112        }
113      }
[10051]114
[10086]115      var g = new Grammar(startSymbol, nonTerminals, allSymbols.Except(nonTerminals));
116
117      m = ruleExpr.Match(lines.First());
118      // add production rules
119      foreach (var alt in m.Groups["alternative"].Captures) {
120        g.AddProductionRule(startSymbol,
121          new Sequence(alt.ToString()
122            .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
123            .Select(n => allSymbols.Single(s => s.Name == n)).ToList<ISymbol>()));
124      }
125      // parse all remaining lines
126      foreach (var line in lines.Skip(1)) {
127        m = ruleExpr.Match(line);
128        var ntSymbol = nonTerminals.Single(s => s.Name == m.Groups["ntSymbol"].Value);
129        foreach (var alt in m.Groups["alternative"].Captures) {
130          g.AddProductionRule(ntSymbol,
131            new Sequence(alt.ToString()
132              .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
133              .Select(n => allSymbols.Single(s => s.Name == n)).ToList<ISymbol>()));
134        }
135      }
136
[10031]137      return g;
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.