Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/18/13 21:33:56 (11 years ago)
Author:
gkronber
Message:

#2026 worked on brute force solver for GPDL problems.

Location:
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Attribute.cs

    r10063 r10067  
    2323
    2424namespace HeuristicLab.Grammars {
     25  public sealed class AttributeType {
     26    public static readonly AttributeType In = new AttributeType("");
     27    public static readonly AttributeType Out = new AttributeType("out");
     28    public static readonly AttributeType Ref = new AttributeType("ref");
     29    private string attributeType;
     30    private AttributeType(string attributeType) {
     31      this.attributeType = attributeType;
     32    }
     33
     34    public static AttributeType Parse(string attributeType) {
     35      if (string.IsNullOrEmpty(attributeType)) return In;
     36      else if (attributeType == "ref") return Ref;
     37      else if (attributeType == "out") return Out;
     38      else throw new ArgumentException("Unknown attribute type string.", attributeType);
     39    }
     40
     41    public override string ToString() {
     42      return attributeType;
     43    }
     44  };
     45
    2546  public class Attribute : IAttribute {
    2647    public AttributeType AttributeType { get; private set; }
    2748    public string Type { get; private set; }
    2849    public string Name { get; private set; }
     50
     51    public Attribute(string name, string type)
     52      : this(name, type, AttributeType.In) {
     53    }
     54
     55    public Attribute(string name, string type, AttributeType attributeType) {
     56      this.Name = name;
     57      this.Type = type;
     58      this.AttributeType = attributeType;
     59    }
     60
     61    public override string ToString() {
     62      return AttributeType + " " + Type + " " + Name;
     63    }
    2964  }
    3065}
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/AttributedGrammar.cs

    r10051 r10067  
    2424using System.Diagnostics;
    2525using System.Linq;
    26 using System.Text.RegularExpressions;
    2726
    2827namespace HeuristicLab.Grammars {
    2928  public class AttributedGrammar : Grammar {
     29    private Dictionary<ISymbol, string> localDefinitions = new Dictionary<ISymbol, string>();
     30    private Dictionary<ISymbol, IList<Sequence>> alternatives = new Dictionary<ISymbol, IList<Sequence>>();
     31
    3032    public AttributedGrammar(ISymbol startSymbol)
    3133      : base(startSymbol) {
     
    3436    }
    3537
    36     private static Regex ruleExpr = new Regex(@"\s*(?<ntSymbol>\w+)\s*(?<formPar>\<\w+\>)+\s*->\s*(?<alternative>\w+\s*(?<actPar>\<\w+\>)+(?:\s+\w+\s*(?<actPar>\<\w+\>)+)*)(?:\s*\|\s*(?<alternative>\w+\s*(?<formPar>\<\w+\>)+(?:\s+\w+\s*(?<formPar>\<\w+\>)+)*))*");
    37     private static Regex empty = new Regex(@"^\s*$");
    38     public static Grammar FromString(string gStr) {
    39       var lines = gStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    40       lines = lines.Where(l => !empty.IsMatch(l)).ToArray(); // remove empty lines
     38    public IEnumerable<Sequence> GetAlternativesWithSemanticActions(ISymbol ntSymbol) {
     39      Debug.Assert(!ntSymbol.Equals(EmptySymbol));
     40      return alternatives[ntSymbol];
     41    }
     42    public Sequence GetAlternativeWithSemanticActions(ISymbol ntSymbol, int index) {
     43      Debug.Assert(!ntSymbol.Equals(EmptySymbol));
     44      return alternatives[ntSymbol][index];
     45    }
    4146
    42       // first line is the rule for the start-symbol
    43       var m = ruleExpr.Match(lines.First());
    44       var startSymbol = new Symbol(m.Groups["ntSymbol"].Value);
    45      
    46       var g = new Grammar(startSymbol);
    47       foreach (var alt in m.Groups["alternative"].Captures) {
    48         g.AddProductionRule(startSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n))));
     47    public override void AddProductionRule(ISymbol ntSymbol, Sequence production) {
     48      base.AddProductionRule(ntSymbol, new Sequence(production.Where(symb => !(symb is SemanticSymbol)).ToList()));
     49
     50      IList<Sequence> list;
     51      if (!alternatives.TryGetValue(ntSymbol, out list)) {
     52        list = new List<Sequence>();
     53        alternatives.Add(ntSymbol, list);
    4954      }
    50       foreach (var line in lines.Skip(1)) {
    51         m = ruleExpr.Match(line);
    52         var ntSymbol = new Symbol(m.Groups["ntSymbol"].Value);
    53         foreach (var alt in m.Groups["alternative"].Captures) {
    54           g.AddProductionRule(ntSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n))));
    55         }
    56       }
     55      // make sure that there is not equivalent production registered already
     56      Debug.Assert(!list.Any(s => s.SequenceEqual(production)));
    5757
    58       return g;
     58      list.Add(production);
    5959    }
     60
     61    public void AddLocalDefinitions(ISymbol ntSymbol, string localDefCode) {
     62      Debug.Assert(!ntSymbol.Equals(EmptySymbol));
     63      Debug.Assert(!localDefinitions.ContainsKey(ntSymbol));
     64
     65      localDefinitions.Add(ntSymbol, localDefCode);
     66    }
     67    public string GetLocalDefinitions(ISymbol ntSymbol) {
     68      string code;
     69      if (localDefinitions.TryGetValue(ntSymbol, out code)) return code;
     70      else return string.Empty;
     71    }
     72
     73
     74
     75    //private static Regex ruleExpr = new Regex(@"\s*(?<ntSymbol>\w+)\s*(?<formPar>\<\w+\>)+\s*->\s*(?<alternative>\w+\s*(?<actPar>\<\w+\>)+(?:\s+\w+\s*(?<actPar>\<\w+\>)+)*)(?:\s*\|\s*(?<alternative>\w+\s*(?<formPar>\<\w+\>)+(?:\s+\w+\s*(?<formPar>\<\w+\>)+)*))*");
     76    //private static Regex empty = new Regex(@"^\s*$");
     77    //public static Grammar FromString(string gStr) {
     78    //  var lines = gStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
     79    //  lines = lines.Where(l => !empty.IsMatch(l)).ToArray(); // remove empty lines
     80
     81    //  // first line is the rule for the start-symbol
     82    //  var m = ruleExpr.Match(lines.First());
     83    //  var startSymbol = new Symbol(m.Groups["ntSymbol"].Value);
     84
     85    //  var g = new Grammar(startSymbol);
     86    //  foreach (var alt in m.Groups["alternative"].Captures) {
     87    //    g.AddProductionRule(startSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n))));
     88    //  }
     89    //  foreach (var line in lines.Skip(1)) {
     90    //    m = ruleExpr.Match(line);
     91    //    var ntSymbol = new Symbol(m.Groups["ntSymbol"].Value);
     92    //    foreach (var alt in m.Groups["alternative"].Captures) {
     93    //      g.AddProductionRule(ntSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n))));
     94    //    }
     95    //  }
     96
     97    //  return g;
     98    //}
    6099  }
    61100}
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Grammar.cs

    r10051 r10067  
    5454    }
    5555
    56     public void AddProductionRule(ISymbol ntSymbol, Sequence production) {
     56    public virtual void AddProductionRule(ISymbol ntSymbol, Sequence production) {
    5757      Debug.Assert(ntSymbol != EmptySymbol);
    5858
     
    9696      var g = new Grammar(startSymbol);
    9797      foreach (var alt in m.Groups["alternative"].Captures) {
    98         g.AddProductionRule(startSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n))));
     98        g.AddProductionRule(startSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n)).ToList<ISymbol>()));
    9999      }
    100100      foreach (var line in lines.Skip(1)) {
     
    102102        var ntSymbol = new Symbol(m.Groups["ntSymbol"].Value);
    103103        foreach (var alt in m.Groups["alternative"].Captures) {
    104           g.AddProductionRule(ntSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n))));
     104          g.AddProductionRule(ntSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n)).ToList<ISymbol>()));
    105105        }
    106106      }
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/HeuristicLab.Grammars-3.3.csproj

    r10063 r10067  
    105105    <Compile Include="AttributedGrammar.cs" />
    106106    <Compile Include="Attribute.cs" />
     107    <Compile Include="SemanticSymbol.cs" />
    107108    <Compile Include="Sequence.cs" />
    108109    <Compile Include="Test.cs" />
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Interfaces/IAttribute.cs

    r10051 r10067  
    2020#endregion
    2121
     22using System;
     23
    2224namespace HeuristicLab.Grammars {
    23   public enum AttributeType { In, Out, Ref };
     25
    2426  public interface IAttribute {
    2527    AttributeType AttributeType { get; }
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Interfaces/ISymbol.cs

    r10051 r10067  
    2020#endregion
    2121
     22using System.Collections.Generic;
     23
    2224namespace HeuristicLab.Grammars {
    2325  public interface ISymbol {
    2426    string Name { get; }
    25     IAttribute Attribute { get; }
     27    IEnumerable<IAttribute> Attributes { get; }
     28    string GetAttributeString();
    2629  }
    2730}
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Language.cs

    r10051 r10067  
    3131  public class Language : IEnumerable<IEnumerable<string>> {
    3232    private class PartiallyProcessedSequence {
    33       private Sequence terminalSeq;
     33      private List<ISymbol> terminalSeq;
    3434      private Sequence remainingSeq;
    3535      private IGrammar grammar;
     
    3838      private PartiallyProcessedSequence(PartiallyProcessedSequence parent) {
    3939        this.parent = parent;
    40         this.terminalSeq = new Sequence();
     40        this.terminalSeq = new List<ISymbol>();
    4141      }
    4242
    4343      public PartiallyProcessedSequence(Sequence seq, IGrammar g) {
    44         this.terminalSeq = new Sequence();
     44        this.terminalSeq = new List<ISymbol>();
    4545        this.remainingSeq = seq;
    4646        this.grammar = g;
     
    5959          ntSymbol = enumerator.Current;
    6060
    61           remainingSeq = new Sequence(remainingSeq.Skip(i + 1));
     61          remainingSeq = new Sequence(remainingSeq.Skip(i + 1).ToList());
    6262          return true;
    6363        }
     
    6666        var p = new PartiallyProcessedSequence(this);
    6767        p.grammar = grammar;
    68         p.remainingSeq = new Sequence(seq.Concat(remainingSeq));
     68        p.remainingSeq = new Sequence(seq.Concat(remainingSeq).ToList());
    6969        return p;
    7070      }
    71       public Sequence GetSequence() {
     71      public IList<ISymbol> GetSequence() {
    7272        if (parent == null) return terminalSeq;
    7373        else {
    74           return new Sequence(parent.GetSequence().Concat(terminalSeq));
     74          return new Sequence(parent.GetSequence().Concat(terminalSeq).ToList());
    7575        }
    7676      }
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Sequence.cs

    r10063 r10067  
    2121
    2222using System.Collections.Generic;
     23using System.Collections.ObjectModel;
    2324using System.Linq;
    2425using System.Text;
    2526
    2627namespace HeuristicLab.Grammars {
    27   public class Sequence : List<ISymbol> {
    28     public Sequence()
    29       : base() {
    30     }
    31 
    32     public Sequence(IEnumerable<ISymbol> seq)
    33       : base() {
    34       this.AddRange(seq);
    35     }
    36     public IEnumerator<ISymbol> GetEnumerator() {
    37       return (IEnumerator<ISymbol>)base.GetEnumerator();
     28  public class Sequence : ReadOnlyCollection<ISymbol> {
     29    public Sequence(IList<ISymbol> seq)
     30      : base(seq) {
    3831    }
    3932
     
    5043
    5144    public override string ToString() {
    52       var b = new StringBuilder();
    53       ForEach(s => b.AppendFormat("{0} ", s));
    54       return b.ToString();
     45      return this.Aggregate("", (str, symb) => str + symb);
    5546    }
    5647  }
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Symbol.cs

    r10063 r10067  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
     25
    2326namespace HeuristicLab.Grammars {
    24   public class Symbol : ISymbol, IEquatable<ISymbol> {
     27  public class Symbol : ISymbol {
    2528    public string Name { get; private set; }
    26     public IAttribute Attribute { get; private set; }
    27     public Symbol(string name, IAttribute attribute = null) {
     29    public IEnumerable<IAttribute> Attributes { get; private set; }
     30    public Symbol(string name, IEnumerable<IAttribute> attributes = null) {
    2831      this.Name = name;
    29       this.Attribute = attribute;
     32      if (attributes != null) this.Attributes = attributes;
     33      else this.Attributes = Enumerable.Empty<IAttribute>();
    3034    }
    3135
    32     public bool Equals(ISymbol other) {
    33       return Name == other.Name;
     36    public string GetAttributeString() {
     37      if (!Attributes.Any()) return string.Empty;
     38      return Attributes.Skip(1).Aggregate(Attributes.First().ToString(), (str, a) => str + ", " + a.ToString());
    3439    }
    3540
     
    3742      if (obj == null) return false;
    3843      if (obj == this) return true;
    39       var other = obj as ISymbol;
     44      var other = obj as Symbol;
    4045      if (other == null) return false;
    41       return Equals(other);
     46      return this.Name == other.Name; // compare names only
    4247    }
    4348
     
    4550      return Name.GetHashCode();
    4651    }
     52
     53    public override string ToString() {
     54      return Name + "<" + GetAttributeString() + ">";
     55    }
    4756  }
    4857}
Note: See TracChangeset for help on using the changeset viewer.