- Timestamp:
- 10/17/13 15:19:45 (11 years ago)
- Location:
- branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3
- Files:
-
- 6 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Grammar.cs
r10031 r10051 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Diagnostics; 24 25 using System.Linq; 25 26 using System.Text.RegularExpressions; 26 27 27 28 namespace HeuristicLab.Grammars { 28 using Sequence = IEnumerable<string>; 29 public class Grammar : IGrammar { 30 public static readonly ISymbol EmptySymbol = new Symbol("EPS"); 31 private Dictionary<ISymbol, List<Sequence>> rules = new Dictionary<ISymbol, List<Sequence>>(); 32 private HashSet<ISymbol> allSymbols = new HashSet<ISymbol>(); 29 33 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>(); 34 public ISymbol StartSymbol { get; set; } 35 public IEnumerable<ISymbol> TerminalSymbols { get { return allSymbols.Except(NonTerminalSymbols); } } 36 public IEnumerable<ISymbol> NonTerminalSymbols { get { return rules.Keys; } } 37 public IEnumerable<ISymbol> Symbols { get { return allSymbols; } } 33 38 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) { 39 public Grammar(ISymbol startSymbol) { 40 Debug.Assert(startSymbol != EmptySymbol); 40 41 this.StartSymbol = startSymbol; 41 42 } 42 43 43 public int NumberOfAlternatives( stringntSymbol) {44 public int NumberOfAlternatives(ISymbol ntSymbol) { 44 45 return rules[ntSymbol].Count; 45 46 } 46 47 47 public IEnumerable<Sequence> GetAlternatives( stringntSymbol) {48 public IEnumerable<Sequence> GetAlternatives(ISymbol ntSymbol) { 48 49 return rules[ntSymbol].AsReadOnly(); 49 50 } 50 51 51 public Sequence GetAlternative( stringntSymbol, int index) {52 public Sequence GetAlternative(ISymbol ntSymbol, int index) { 52 53 return rules[ntSymbol][index]; 53 54 } 54 55 55 public void AddProductionRule(string ntSymbol, Sequence production) { 56 public void AddProductionRule(ISymbol ntSymbol, Sequence production) { 57 Debug.Assert(ntSymbol != EmptySymbol); 58 56 59 List<Sequence> l; 57 60 if (!rules.TryGetValue(ntSymbol, out l)) { … … 61 64 allSymbols.Add(ntSymbol); // register new nt-symbol 62 65 } 66 // check if the same production exists already 67 Debug.Assert(!l.Any(s => s.SequenceEqual(production))); 68 63 69 l.Add(production); 64 70 … … 66 72 } 67 73 74 public bool IsTerminal(ISymbol symbol) { 75 return !rules.ContainsKey(symbol) && allSymbols.Contains(symbol); 76 } 77 public bool IsNonTerminal(ISymbol symbol) { 78 return rules.ContainsKey(symbol); 79 } 80 81 // checks if a rule exists for each NT symbol 82 public bool IsComplete() { 83 return rules.ContainsKey(StartSymbol) && 84 NonTerminalSymbols.All(nt => rules.ContainsKey(nt)); 85 } 86 68 87 private static Regex ruleExpr = new Regex(@"\s*(?<ntSymbol>\w+)\s*->\s*(?<alternative>\w+(?:\s+\w+)*)(?:\s*\|\s*(?<alternative>\w+(?:\s+\w+)*))*"); 69 88 private static Regex empty = new Regex(@"^\s*$"); 70 public static IGrammar FromString(string gStr) {89 public static Grammar FromString(string gStr) { 71 90 var lines = gStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); 72 91 lines = lines.Where(l => !empty.IsMatch(l)).ToArray(); // remove empty lines … … 74 93 // first line is the rule for the start-symbol 75 94 var m = ruleExpr.Match(lines.First()); 76 var startSymbol = m.Groups["ntSymbol"].Value;95 var startSymbol = new Symbol(m.Groups["ntSymbol"].Value); 77 96 var g = new Grammar(startSymbol); 78 97 foreach (var alt in m.Groups["alternative"].Captures) { 79 g.AddProductionRule(startSymbol, alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));98 g.AddProductionRule(startSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n)))); 80 99 } 81 100 foreach (var line in lines.Skip(1)) { 82 101 m = ruleExpr.Match(line); 83 var ntSymbol = m.Groups["ntSymbol"].Value;102 var ntSymbol = new Symbol(m.Groups["ntSymbol"].Value); 84 103 foreach (var alt in m.Groups["alternative"].Captures) { 85 g.AddProductionRule(ntSymbol, alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));104 g.AddProductionRule(ntSymbol, new Sequence(alt.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(n => new Symbol(n)))); 86 105 } 87 106 } 107 88 108 return g; 89 109 } -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/HeuristicLab.Grammars-3.3.csproj
r10031 r10051 7 7 <SchemaVersion>2.0</SchemaVersion> 8 8 <ProjectGuid>{A5452B63-B33B-4F9F-9E81-98B75EDB5612}</ProjectGuid> 9 <OutputType> Library</OutputType>9 <OutputType>Exe</OutputType> 10 10 <AppDesignerFolder>Properties</AppDesignerFolder> 11 11 <RootNamespace>HeuristicLab.Grammars</RootNamespace> … … 93 93 <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> 94 94 </PropertyGroup> 95 <PropertyGroup> 96 <StartupObject /> 97 </PropertyGroup> 95 98 <ItemGroup> 96 99 <Reference Include="HeuristicLab.PluginInfrastructure-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> … … 115 118 </ItemGroup> 116 119 <ItemGroup> 120 <Compile Include="AttributedGrammar.cs" /> 121 <Compile Include="Attribute.cs" /> 122 <Compile Include="Sequence.cs" /> 123 <Compile Include="Test.cs" /> 124 <Compile Include="Symbol.cs" /> 125 <Compile Include="Language.cs" /> 117 126 <Compile Include="Grammar.cs" /> 118 127 <Compile Include="Interfaces\IAttribute.cs" /> 119 <Compile Include="Interfaces\IAttributedGrammar.cs" />120 128 <Compile Include="Interfaces\ISymbol.cs" /> 121 129 <Compile Include="Interfaces\IGrammar.cs" /> -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Interfaces/IAttribute.cs
r10031 r10051 22 22 namespace HeuristicLab.Grammars { 23 23 public enum AttributeType { In, Out, Ref }; 24 interface IAttribute {24 public interface IAttribute { 25 25 AttributeType AttributeType { get; } 26 26 string Type { get; } -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Interfaces/IAttributedGrammar.cs
r10031 r10051 24 24 namespace HeuristicLab.Grammars { 25 25 interface IAttributedGrammar : IGrammar { 26 IEnumerable<IAttribute> GetAttributes(string symbol); 27 void AddAttribute(string symbol, IAttribute attribute); 26 ISymbol StartSymbol { get; set; } 27 IEnumerable<ISymbol> TerminalSymbols { get; } 28 IEnumerable<ISymbol> NonTerminalSymbols { get; } 29 IEnumerable<ISymbol> Symbols { get; } 30 bool IsTerminal(ISymbol symbol); 31 bool IsNonTerminal(ISymbol symbol); 32 33 int NumberOfAlternatives(ISymbol ntSymbol); 34 IEnumerable<Sequence> GetAlternatives(ISymbol ntSymbol); 35 Sequence GetAlternative(ISymbol ntSymbol, int index); 36 37 void AddProductionRule(ISymbol ntSymbol, Sequence production); 28 38 } 29 39 } -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Interfaces/IGrammar.cs
r10031 r10051 23 23 24 24 namespace HeuristicLab.Grammars { 25 using Sequence = IEnumerable<string>;26 25 27 26 public interface IGrammar { 28 string StartSymbol { get; set; } 29 IEnumerable<string> TerminalSymbols { get; } 30 IEnumerable<string> NonTerminalSymbols { get; } 31 IEnumerable<string> Symbols { get; } 27 ISymbol StartSymbol { get; set; } 28 IEnumerable<ISymbol> TerminalSymbols { get; } 29 IEnumerable<ISymbol> NonTerminalSymbols { get; } 30 IEnumerable<ISymbol> Symbols { get; } 31 bool IsTerminal(ISymbol symbol); 32 bool IsNonTerminal(ISymbol symbol); 32 33 33 int NumberOfAlternatives( stringntSymbol);34 IEnumerable<Sequence> GetAlternatives( stringntSymbol);35 Sequence GetAlternative( stringntSymbol, int index);34 int NumberOfAlternatives(ISymbol ntSymbol); 35 IEnumerable<Sequence> GetAlternatives(ISymbol ntSymbol); 36 Sequence GetAlternative(ISymbol ntSymbol, int index); 36 37 37 void AddProductionRule( stringntSymbol, Sequence production);38 void AddProductionRule(ISymbol ntSymbol, Sequence production); 38 39 } 39 40 } -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/Interfaces/ISymbol.cs
r10031 r10051 20 20 #endregion 21 21 22 /*23 22 namespace HeuristicLab.Grammars { 24 interface ISymbol {23 public interface ISymbol { 25 24 string Name { get; } 26 bool IsTerminal{ get; }25 IAttribute Attribute { get; } 27 26 } 28 27 } 29 */
Note: See TracChangeset
for help on using the changeset viewer.