Mercurial > pub > ImplabNet
comparison Implab/Automaton/RegularExpressions/Grammar.cs @ 163:419aa51b04fd ref20160224
JSON moved to Formats namespace
Working in RegularDFA
author | cin |
---|---|
date | Wed, 24 Feb 2016 20:12:52 +0300 |
parents | 0526412bbb26 |
children | ec35731ae299 |
comparison
equal
deleted
inserted
replaced
162:0526412bbb26 | 163:419aa51b04fd |
---|---|
7 | 7 |
8 namespace Implab.Automaton.RegularExpressions { | 8 namespace Implab.Automaton.RegularExpressions { |
9 /// <summary> | 9 /// <summary> |
10 /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>. | 10 /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>. |
11 /// </summary> | 11 /// </summary> |
12 /// <typeparam name="TGrammar"></typeparam> | 12 public abstract class Grammar<TSymbol, TTag> { |
13 public abstract class Grammar<TGrammar> where TGrammar: Grammar<TGrammar>, new() { | |
14 static TGrammar _instance; | |
15 | 13 |
16 public static TGrammar Instance{ | 14 public abstract IAlphabetBuilder<TSymbol> Alphabet { |
17 get { | 15 get; |
18 if (_instance == null) | |
19 _instance = new TGrammar(); | |
20 return _instance; | |
21 } | |
22 } | 16 } |
23 | 17 |
24 readonly CharAlphabet m_alphabet = new CharAlphabet(); | 18 public SymbolToken<TTag> UnclassifiedToken() { |
25 | 19 return new SymbolToken<TTag>(DFAConst.UNCLASSIFIED_INPUT); |
26 public CharAlphabet Alphabet { | |
27 get { return m_alphabet; } | |
28 } | 20 } |
29 | 21 |
30 public SymbolToken UnclassifiedToken() { | 22 public void DefineAlphabet(IEnumerable<TSymbol> alphabet) { |
31 return new SymbolToken(CharAlphabet.UNCLASSIFIED); | |
32 } | |
33 | |
34 public void DefineAlphabet(IEnumerable<char> alphabet) { | |
35 Safe.ArgumentNotNull(alphabet, "alphabet"); | 23 Safe.ArgumentNotNull(alphabet, "alphabet"); |
36 | 24 |
37 foreach (var ch in alphabet) | 25 foreach (var ch in alphabet) |
38 m_alphabet.DefineSymbol(ch); | 26 Alphabet.DefineSymbol(ch); |
39 } | |
40 public Token SymbolRangeToken(char start, char end) { | |
41 return SymbolToken(Enumerable.Range(start, end - start + 1).Select(x => (char)x)); | |
42 } | 27 } |
43 | 28 |
44 public Token SymbolToken(char symbol) { | 29 public Token<TTag> SymbolToken(TSymbol symbol) { |
45 return Token.New(TranslateOrAdd(symbol)); | 30 return Token<TTag>.New(TranslateOrAdd(symbol)); |
46 } | 31 } |
47 | 32 |
48 public Token SymbolToken(IEnumerable<char> symbols) { | 33 public Token<TTag> SymbolToken(IEnumerable<TSymbol> symbols) { |
49 Safe.ArgumentNotNull(symbols, "symbols"); | 34 Safe.ArgumentNotNull(symbols, "symbols"); |
50 | 35 |
51 return Token.New(TranslateOrAdd(symbols).ToArray()); | 36 return Token<TTag>.New(TranslateOrAdd(symbols).ToArray()); |
52 } | 37 } |
53 | 38 |
54 public Token SymbolSetToken(params char[] set) { | 39 public Token<TTag> SymbolSetToken(params TSymbol[] set) { |
55 return SymbolToken(set); | 40 return SymbolToken(set); |
56 } | 41 } |
57 | 42 |
58 int TranslateOrAdd(char ch) { | 43 int TranslateOrAdd(TSymbol ch) { |
59 var t = m_alphabet.Translate(ch); | 44 var t = Alphabet.Translate(ch); |
60 if (t == CharAlphabet.UNCLASSIFIED) | 45 if (t == DFAConst.UNCLASSIFIED_INPUT) |
61 t = m_alphabet.DefineSymbol(ch); | 46 t = Alphabet.DefineSymbol(ch); |
62 return t; | 47 return t; |
63 } | 48 } |
64 | 49 |
65 IEnumerable<int> TranslateOrAdd(IEnumerable<char> symbols) { | 50 IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) { |
66 return symbols.Distinct().Select(TranslateOrAdd); | 51 return symbols.Distinct().Select(TranslateOrAdd); |
67 } | 52 } |
68 | 53 |
69 int TranslateOrDie(char ch) { | 54 int TranslateOrDie(TSymbol ch) { |
70 var t = m_alphabet.Translate(ch); | 55 var t = Alphabet.Translate(ch); |
71 if (t == CharAlphabet.UNCLASSIFIED) | 56 if (t == DFAConst.UNCLASSIFIED_INPUT) |
72 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch)); | 57 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch)); |
73 return t; | 58 return t; |
74 } | 59 } |
75 | 60 |
76 IEnumerable<int> TranslateOrDie(IEnumerable<char> symbols) { | 61 IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) { |
77 return symbols.Distinct().Select(TranslateOrDie); | 62 return symbols.Distinct().Select(TranslateOrDie); |
78 } | 63 } |
79 | 64 |
80 public Token SymbolTokenExcept(IEnumerable<char> symbols) { | 65 public Token<TTag> SymbolTokenExcept(IEnumerable<TSymbol> symbols) { |
81 Safe.ArgumentNotNull(symbols, "symbols"); | 66 Safe.ArgumentNotNull(symbols, "symbols"); |
82 | 67 |
83 return Token.New( Enumerable.Range(0, m_alphabet.Count).Except(TranslateOrDie(symbols)).ToArray()); | 68 return Token<TTag>.New( Enumerable.Range(0, Alphabet.Count).Except(TranslateOrDie(symbols)).ToArray() ); |
84 } | 69 } |
85 | 70 |
86 protected CDFADefinition BuildDFA(Token lang) { | 71 protected CDFADefinition BuildDFA(Token<TTag> lang) { |
87 Safe.ArgumentNotNull(lang, "lang"); | 72 Safe.ArgumentNotNull(lang, "lang"); |
88 | 73 |
89 var dfa = new CDFADefinition(m_alphabet); | 74 var dfa = new CDFADefinition(Alphabet); |
90 | 75 |
91 var builder = new DFABuilder(); | 76 var builder = new RegularDFABuilder<TTag>(); |
92 | 77 |
93 lang.Accept( builder ); | 78 lang.Accept( builder ); |
94 | 79 |
95 builder.BuildDFA(dfa); | 80 builder.BuildDFA(dfa); |
96 if (dfa.InitialStateIsFinal) | 81 if (dfa.InitialStateIsFinal) |