Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15899 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
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;
26using System.Text.RegularExpressions;
27
28namespace HeuristicLab.Grammars {
29  public class Grammar : IGrammar {
30    public static readonly ISymbol EmptySymbol = new Symbol("EPS");
31    private readonly Dictionary<ISymbol, List<Sequence>> rules;
32    private readonly HashSet<ISymbol> allSymbols;
33
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; } }
38
39    public Grammar(ISymbol startSymbol, IEnumerable<ISymbol> nonTerminals, IEnumerable<ISymbol> terminals) {
40      Debug.Assert(startSymbol != EmptySymbol);
41      this.StartSymbol = startSymbol;
42      this.allSymbols = new HashSet<ISymbol>(nonTerminals.Concat(terminals));
43      this.rules = nonTerminals.ToDictionary(nt => nt, nt => new List<Sequence>());
44    }
45
46    public int NumberOfAlternatives(ISymbol ntSymbol) {
47      return rules[ntSymbol].Count;
48    }
49
50    public IEnumerable<Sequence> GetAlternatives(ISymbol ntSymbol) {
51      return rules[ntSymbol].AsReadOnly();
52    }
53
54    public Sequence GetAlternative(ISymbol ntSymbol, int index) {
55      return rules[ntSymbol][index];
56    }
57
58    public virtual void AddProductionRule(ISymbol ntSymbol, Sequence production) {
59      Debug.Assert(ntSymbol != EmptySymbol);
60      Debug.Assert(rules.ContainsKey(ntSymbol));
61      Debug.Assert(production.All(s => allSymbols.Contains(s)));
62
63      var l = rules[ntSymbol];
64      Debug.Assert(!l.Any(s => s.SequenceEqual(production)));
65
66      l.Add(production);
67    }
68
69    public bool IsTerminal(ISymbol symbol) {
70      // terminals must not have rules but must occur in the set of all symbols
71      return !rules.ContainsKey(symbol) && allSymbols.Contains(symbol);
72    }
73
74    public bool IsNonTerminal(ISymbol symbol) {
75      return rules.ContainsKey(symbol);
76    }
77
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*$");
80    public static Grammar FromString(string gStr) {
81      var lines = gStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
82      lines = lines.Where(l => !empty.IsMatch(l)).ToArray(); // remove empty lines
83
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
88      // first line is the rule for the start-symbol
89      var m = ruleExpr.Match(lines.First());
90      var startSymbol = new Symbol(m.Groups["ntSymbol"].Value);
91
92      nonTerminals.Add(startSymbol);
93      allSymbols.Add(startSymbol);
94
95      // parse first line
96      foreach (var alt in m.Groups["alternative"].Captures) {
97        foreach (var s in alt.ToString()
98          .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
99          .Select(n => new Symbol(n))) allSymbols.Add(s);
100      }
101      // parse all remaining lines
102      foreach (var line in lines.Skip(1)) {
103        m = ruleExpr.Match(line);
104        var ntSymbol = new Symbol(m.Groups["ntSymbol"].Value);
105        nonTerminals.Add(ntSymbol);
106        allSymbols.Add(ntSymbol);
107
108        foreach (var alt in m.Groups["alternative"].Captures) {
109          foreach (var s in alt.ToString()
110          .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
111          .Select(n => new Symbol(n))) allSymbols.Add(s);
112        }
113      }
114
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
137      return g;
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.