- Timestamp:
- 10/18/13 21:33:56 (11 years ago)
- 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 23 23 24 24 namespace 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 25 46 public class Attribute : IAttribute { 26 47 public AttributeType AttributeType { get; private set; } 27 48 public string Type { get; private set; } 28 49 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 } 29 64 } 30 65 } -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/AttributedGrammar.cs
r10051 r10067 24 24 using System.Diagnostics; 25 25 using System.Linq; 26 using System.Text.RegularExpressions;27 26 28 27 namespace HeuristicLab.Grammars { 29 28 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 30 32 public AttributedGrammar(ISymbol startSymbol) 31 33 : base(startSymbol) { … … 34 36 } 35 37 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 } 41 46 42 // first line is the rule for the start-symbol43 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); 49 54 } 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))); 57 57 58 return g;58 list.Add(production); 59 59 } 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 //} 60 99 } 61 100 } -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Grammar.cs
r10051 r10067 54 54 } 55 55 56 public v oid AddProductionRule(ISymbol ntSymbol, Sequence production) {56 public virtual void AddProductionRule(ISymbol ntSymbol, Sequence production) { 57 57 Debug.Assert(ntSymbol != EmptySymbol); 58 58 … … 96 96 var g = new Grammar(startSymbol); 97 97 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>())); 99 99 } 100 100 foreach (var line in lines.Skip(1)) { … … 102 102 var ntSymbol = new Symbol(m.Groups["ntSymbol"].Value); 103 103 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>())); 105 105 } 106 106 } -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/HeuristicLab.Grammars-3.3.csproj
r10063 r10067 105 105 <Compile Include="AttributedGrammar.cs" /> 106 106 <Compile Include="Attribute.cs" /> 107 <Compile Include="SemanticSymbol.cs" /> 107 108 <Compile Include="Sequence.cs" /> 108 109 <Compile Include="Test.cs" /> -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Interfaces/IAttribute.cs
r10051 r10067 20 20 #endregion 21 21 22 using System; 23 22 24 namespace HeuristicLab.Grammars { 23 public enum AttributeType { In, Out, Ref }; 25 24 26 public interface IAttribute { 25 27 AttributeType AttributeType { get; } -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Interfaces/ISymbol.cs
r10051 r10067 20 20 #endregion 21 21 22 using System.Collections.Generic; 23 22 24 namespace HeuristicLab.Grammars { 23 25 public interface ISymbol { 24 26 string Name { get; } 25 IAttribute Attribute { get; } 27 IEnumerable<IAttribute> Attributes { get; } 28 string GetAttributeString(); 26 29 } 27 30 } -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Language.cs
r10051 r10067 31 31 public class Language : IEnumerable<IEnumerable<string>> { 32 32 private class PartiallyProcessedSequence { 33 private SequenceterminalSeq;33 private List<ISymbol> terminalSeq; 34 34 private Sequence remainingSeq; 35 35 private IGrammar grammar; … … 38 38 private PartiallyProcessedSequence(PartiallyProcessedSequence parent) { 39 39 this.parent = parent; 40 this.terminalSeq = new Sequence();40 this.terminalSeq = new List<ISymbol>(); 41 41 } 42 42 43 43 public PartiallyProcessedSequence(Sequence seq, IGrammar g) { 44 this.terminalSeq = new Sequence();44 this.terminalSeq = new List<ISymbol>(); 45 45 this.remainingSeq = seq; 46 46 this.grammar = g; … … 59 59 ntSymbol = enumerator.Current; 60 60 61 remainingSeq = new Sequence(remainingSeq.Skip(i + 1) );61 remainingSeq = new Sequence(remainingSeq.Skip(i + 1).ToList()); 62 62 return true; 63 63 } … … 66 66 var p = new PartiallyProcessedSequence(this); 67 67 p.grammar = grammar; 68 p.remainingSeq = new Sequence(seq.Concat(remainingSeq) );68 p.remainingSeq = new Sequence(seq.Concat(remainingSeq).ToList()); 69 69 return p; 70 70 } 71 public SequenceGetSequence() {71 public IList<ISymbol> GetSequence() { 72 72 if (parent == null) return terminalSeq; 73 73 else { 74 return new Sequence(parent.GetSequence().Concat(terminalSeq) );74 return new Sequence(parent.GetSequence().Concat(terminalSeq).ToList()); 75 75 } 76 76 } -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Sequence.cs
r10063 r10067 21 21 22 22 using System.Collections.Generic; 23 using System.Collections.ObjectModel; 23 24 using System.Linq; 24 25 using System.Text; 25 26 26 27 namespace 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) { 38 31 } 39 32 … … 50 43 51 44 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); 55 46 } 56 47 } -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Symbol.cs
r10063 r10067 21 21 22 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 25 23 26 namespace HeuristicLab.Grammars { 24 public class Symbol : ISymbol , IEquatable<ISymbol>{27 public class Symbol : ISymbol { 25 28 public string Name { get; private set; } 26 public I Attribute Attribute{ get; private set; }27 public Symbol(string name, I Attribute attribute= null) {29 public IEnumerable<IAttribute> Attributes { get; private set; } 30 public Symbol(string name, IEnumerable<IAttribute> attributes = null) { 28 31 this.Name = name; 29 this.Attribute = attribute; 32 if (attributes != null) this.Attributes = attributes; 33 else this.Attributes = Enumerable.Empty<IAttribute>(); 30 34 } 31 35 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()); 34 39 } 35 40 … … 37 42 if (obj == null) return false; 38 43 if (obj == this) return true; 39 var other = obj as ISymbol;44 var other = obj as Symbol; 40 45 if (other == null) return false; 41 return Equals(other);46 return this.Name == other.Name; // compare names only 42 47 } 43 48 … … 45 50 return Name.GetHashCode(); 46 51 } 52 53 public override string ToString() { 54 return Name + "<" + GetAttributeString() + ">"; 55 } 47 56 } 48 57 }
Note: See TracChangeset
for help on using the changeset viewer.