Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs @ 15800

Last change on this file since 15800 was 15800, checked in by lkammere, 6 years ago

#2886: Refactor code and fix performance issues.

File size: 1.7 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
4
5  public abstract class Symbol {
6    public string StringRepresentation { get; }
7
8    protected Symbol(string representation) {
9      StringRepresentation = representation;
10    }
11
12    public override string ToString() {
13      return StringRepresentation;
14    }
15  }
16
17  public class TerminalSymbol : Symbol {
18    public TerminalSymbol(string representation) : base(representation) { }
19  }
20
21  public class NonterminalSymbol : Symbol {
22    public List<Production> Alternatives { get; }
23
24    public NonterminalSymbol(string representation) : base(representation) {
25      Alternatives = new List<Production>();
26    }
27
28    public void AddProduction(params Symbol[] production) {
29      Alternatives.Add(new Production(production));
30    }
31  }
32
33  public class VariableSymbol : NonterminalSymbol { // Convenience class
34    public IEnumerable<TerminalSymbol> VariableTerminalSymbols { get; }
35
36    public VariableSymbol(string representation, IEnumerable<string> variableNames) : base(representation) {
37      List<TerminalSymbol> createdSymbols = new List<TerminalSymbol>();
38      VariableTerminalSymbols = createdSymbols;
39
40      foreach (string variableName in variableNames) {
41        TerminalSymbol s = new TerminalSymbol(variableName);
42        createdSymbols.Add(s);
43        AddProduction(s);
44      }
45    }
46  }
47
48  public class Production : List<Symbol> {
49
50    public Production(params Symbol[] symbols) : base(symbols) { }
51
52    public Production(IEnumerable<Symbol> symbols) : base(symbols) { }
53
54    public override string ToString() {
55      return string.Join(" ", this);
56    }
57  }
58}
Note: See TracBrowser for help on using the repository browser.