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

Last change on this file since 15714 was 15714, checked in by lkammere, 22 months ago

#2886 Add tree hashing for addition and multiplication.

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