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

Last change on this file since 10031 was 10031, checked in by gkronber, 9 years ago

#2026: worked on plugin for grammars and made some changes for compatibility with the latest stable version of HL

File size: 3.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.Linq;
25using System.Text.RegularExpressions;
26
27namespace HeuristicLab.Grammars {
28  using Sequence = IEnumerable<string>;
29
30  public class Grammar : IGrammar {
31    private Dictionary<string, List<Sequence>> rules = new Dictionary<string, List<Sequence>>();
32    private HashSet<string> allSymbols = new HashSet<string>();
33
34    public string StartSymbol { get; set; }
35    public IEnumerable<string> TerminalSymbols { get { return allSymbols.Except(NonTerminalSymbols); } }
36    public IEnumerable<string> NonTerminalSymbols { get { return rules.Keys; } }
37    public IEnumerable<string> Symbols { get { return allSymbols; } }
38
39    public Grammar(string startSymbol) {
40      this.StartSymbol = startSymbol;
41    }
42
43    public int NumberOfAlternatives(string ntSymbol) {
44      return rules[ntSymbol].Count;
45    }
46
47    public IEnumerable<Sequence> GetAlternatives(string ntSymbol) {
48      return rules[ntSymbol].AsReadOnly();
49    }
50
51    public Sequence GetAlternative(string ntSymbol, int index) {
52      return rules[ntSymbol][index];
53    }
54
55    public void AddProductionRule(string ntSymbol, Sequence production) {
56      List<Sequence> l;
57      if (!rules.TryGetValue(ntSymbol, out l)) {
58        l = new List<Sequence>();
59        rules.Add(ntSymbol, l);
60
61        allSymbols.Add(ntSymbol); // register new nt-symbol
62      }
63      l.Add(production);
64
65      foreach (var s in production) allSymbols.Add(s); // register all symbols in the production
66    }
67
68    private static Regex ruleExpr = new Regex(@"\s*(?<ntSymbol>\w+)\s*->\s*(?<alternative>\w+(?:\s+\w+)*)(?:\s*\|\s*(?<alternative>\w+(?:\s+\w+)*))*");
69    private static Regex empty = new Regex(@"^\s*$");
70    public static IGrammar FromString(string gStr) {
71      var lines = gStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
72      lines = lines.Where(l => !empty.IsMatch(l)).ToArray(); // remove empty lines
73
74      // first line is the rule for the start-symbol
75      var m = ruleExpr.Match(lines.First());
76      var startSymbol = m.Groups["ntSymbol"].Value;
77      var g = new Grammar(startSymbol);
78      foreach (var alt in m.Groups["alternative"].Captures) {
79        g.AddProductionRule(startSymbol, alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
80      }
81      foreach (var line in lines.Skip(1)) {
82        m = ruleExpr.Match(line);
83        var ntSymbol = m.Groups["ntSymbol"].Value;
84        foreach (var alt in m.Groups["alternative"].Captures) {
85          g.AddProductionRule(ntSymbol, alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
86        }
87      }
88      return g;
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.