Changeset 10067 for branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/AttributedGrammar.cs
- Timestamp:
- 10/18/13 21:33:56 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.