162
|
1 using Implab;
|
|
2 using System;
|
|
3 using System.Collections.Generic;
|
|
4 using System.Linq;
|
|
5 using System.Text;
|
|
6 using System.Threading.Tasks;
|
|
7
|
|
8 namespace Implab.Automaton.RegularExpressions {
|
|
9 /// <summary>
|
|
10 /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>.
|
|
11 /// </summary>
|
163
|
12 public abstract class Grammar<TSymbol, TTag> {
|
162
|
13
|
163
|
14 public abstract IAlphabetBuilder<TSymbol> Alphabet {
|
|
15 get;
|
162
|
16 }
|
|
17
|
163
|
18 public SymbolToken<TTag> UnclassifiedToken() {
|
|
19 return new SymbolToken<TTag>(DFAConst.UNCLASSIFIED_INPUT);
|
162
|
20 }
|
|
21
|
163
|
22 public void DefineAlphabet(IEnumerable<TSymbol> alphabet) {
|
162
|
23 Safe.ArgumentNotNull(alphabet, "alphabet");
|
|
24
|
|
25 foreach (var ch in alphabet)
|
163
|
26 Alphabet.DefineSymbol(ch);
|
162
|
27 }
|
163
|
28
|
|
29 public Token<TTag> SymbolToken(TSymbol symbol) {
|
|
30 return Token<TTag>.New(TranslateOrAdd(symbol));
|
162
|
31 }
|
|
32
|
163
|
33 public Token<TTag> SymbolToken(IEnumerable<TSymbol> symbols) {
|
|
34 Safe.ArgumentNotNull(symbols, "symbols");
|
|
35
|
|
36 return Token<TTag>.New(TranslateOrAdd(symbols).ToArray());
|
162
|
37 }
|
|
38
|
163
|
39 public Token<TTag> SymbolSetToken(params TSymbol[] set) {
|
162
|
40 return SymbolToken(set);
|
|
41 }
|
|
42
|
163
|
43 int TranslateOrAdd(TSymbol ch) {
|
|
44 var t = Alphabet.Translate(ch);
|
|
45 if (t == DFAConst.UNCLASSIFIED_INPUT)
|
|
46 t = Alphabet.DefineSymbol(ch);
|
162
|
47 return t;
|
|
48 }
|
|
49
|
163
|
50 IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) {
|
162
|
51 return symbols.Distinct().Select(TranslateOrAdd);
|
|
52 }
|
|
53
|
163
|
54 int TranslateOrDie(TSymbol ch) {
|
|
55 var t = Alphabet.Translate(ch);
|
|
56 if (t == DFAConst.UNCLASSIFIED_INPUT)
|
162
|
57 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch));
|
|
58 return t;
|
|
59 }
|
|
60
|
163
|
61 IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) {
|
162
|
62 return symbols.Distinct().Select(TranslateOrDie);
|
|
63 }
|
|
64
|
163
|
65 public Token<TTag> SymbolTokenExcept(IEnumerable<TSymbol> symbols) {
|
162
|
66 Safe.ArgumentNotNull(symbols, "symbols");
|
|
67
|
163
|
68 return Token<TTag>.New( Enumerable.Range(0, Alphabet.Count).Except(TranslateOrDie(symbols)).ToArray() );
|
162
|
69 }
|
|
70
|
163
|
71 protected CDFADefinition BuildDFA(Token<TTag> lang) {
|
162
|
72 Safe.ArgumentNotNull(lang, "lang");
|
|
73
|
163
|
74 var dfa = new CDFADefinition(Alphabet);
|
162
|
75
|
163
|
76 var builder = new RegularDFABuilder<TTag>();
|
162
|
77
|
|
78 lang.Accept( builder );
|
|
79
|
|
80 builder.BuildDFA(dfa);
|
|
81 if (dfa.InitialStateIsFinal)
|
|
82 throw new ApplicationException("The specified language contains empty token");
|
|
83
|
|
84 return dfa.Optimize();
|
|
85 }
|
|
86
|
|
87
|
|
88
|
|
89 //protected abstract TGrammar CreateInstance();
|
|
90 }
|
|
91
|
|
92
|
|
93 }
|