Changeset 10086 for branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/AttributedGrammar.cs
- Timestamp:
- 10/27/13 20:17:17 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Grammars/3.3/AttributedGrammar.cs
r10067 r10086 27 27 namespace HeuristicLab.Grammars { 28 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>>();29 private readonly Dictionary<ISymbol, string> localDefinitions; 30 private readonly Dictionary<ISymbol, List<Sequence>> alternatives; 31 31 32 public AttributedGrammar(ISymbol startSymbol) 33 : base(startSymbol) { 34 Debug.Assert(!startSymbol.Equals(EmptySymbol)); 32 public AttributedGrammar(ISymbol startSymbol, IEnumerable<ISymbol> nonTerminals, IEnumerable<ISymbol> terminals) 33 : base(startSymbol, nonTerminals, terminals) { 35 34 this.StartSymbol = startSymbol; 35 localDefinitions = nonTerminals.ToDictionary(nt => nt, nt => string.Empty); 36 alternatives = nonTerminals.ToDictionary(nt => nt, nt => new List<Sequence>()); 36 37 } 37 38 38 39 public IEnumerable<Sequence> GetAlternativesWithSemanticActions(ISymbol ntSymbol) { 39 Debug.Assert(!ntSymbol.Equals(EmptySymbol));40 40 return alternatives[ntSymbol]; 41 41 } 42 42 public Sequence GetAlternativeWithSemanticActions(ISymbol ntSymbol, int index) { 43 Debug.Assert(!ntSymbol.Equals(EmptySymbol));44 43 return alternatives[ntSymbol][index]; 45 44 } … … 48 47 base.AddProductionRule(ntSymbol, new Sequence(production.Where(symb => !(symb is SemanticSymbol)).ToList())); 49 48 50 IList<Sequence> list; 51 if (!alternatives.TryGetValue(ntSymbol, out list)) { 52 list = new List<Sequence>(); 53 alternatives.Add(ntSymbol, list); 54 } 49 IList<Sequence> list = alternatives[ntSymbol]; 50 55 51 // make sure that there is not equivalent production registered already 56 52 Debug.Assert(!list.Any(s => s.SequenceEqual(production))); … … 61 57 public void AddLocalDefinitions(ISymbol ntSymbol, string localDefCode) { 62 58 Debug.Assert(!ntSymbol.Equals(EmptySymbol)); 63 Debug.Assert(!localDefinitions.ContainsKey(ntSymbol));64 59 65 localDefinitions .Add(ntSymbol, localDefCode);60 localDefinitions[ntSymbol] = localDefCode; 66 61 } 67 62 public string GetLocalDefinitions(ISymbol ntSymbol) { 68 string code; 69 if (localDefinitions.TryGetValue(ntSymbol, out code)) return code; 70 else return string.Empty; 63 return localDefinitions[ntSymbol]; 71 64 } 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 lines80 81 // // first line is the rule for the start-symbol82 // 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 //}99 65 } 100 66 }
Note: See TracChangeset
for help on using the changeset viewer.