annotate Implab/Automaton/RegularExpressions/Grammar.cs @ 162:0526412bbb26 ref20160224

DFA refactoring
author cin
date Wed, 24 Feb 2016 08:39:53 +0300
parents
children 419aa51b04fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
1 using Implab;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
2 using System;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
3 using System.Collections.Generic;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
4 using System.Linq;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
5 using System.Text;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
6 using System.Threading.Tasks;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
7
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
8 namespace Implab.Automaton.RegularExpressions {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
9 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
10 /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
11 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
12 /// <typeparam name="TGrammar"></typeparam>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
13 public abstract class Grammar<TGrammar> where TGrammar: Grammar<TGrammar>, new() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
14 static TGrammar _instance;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
15
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
16 public static TGrammar Instance{
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
17 get {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
18 if (_instance == null)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
19 _instance = new TGrammar();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
20 return _instance;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
21 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
22 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
23
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
24 readonly CharAlphabet m_alphabet = new CharAlphabet();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
25
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
26 public CharAlphabet Alphabet {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
27 get { return m_alphabet; }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
28 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
29
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
30 public SymbolToken UnclassifiedToken() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
31 return new SymbolToken(CharAlphabet.UNCLASSIFIED);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
32 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
33
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
34 public void DefineAlphabet(IEnumerable<char> alphabet) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
35 Safe.ArgumentNotNull(alphabet, "alphabet");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
36
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
37 foreach (var ch in alphabet)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
38 m_alphabet.DefineSymbol(ch);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
39 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
40 public Token SymbolRangeToken(char start, char end) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
41 return SymbolToken(Enumerable.Range(start, end - start + 1).Select(x => (char)x));
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
42 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
43
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
44 public Token SymbolToken(char symbol) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
45 return Token.New(TranslateOrAdd(symbol));
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
46 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
47
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
48 public Token SymbolToken(IEnumerable<char> symbols) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
49 Safe.ArgumentNotNull(symbols, "symbols");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
50
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
51 return Token.New(TranslateOrAdd(symbols).ToArray());
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
52 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
53
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
54 public Token SymbolSetToken(params char[] set) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
55 return SymbolToken(set);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
56 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
57
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
58 int TranslateOrAdd(char ch) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
59 var t = m_alphabet.Translate(ch);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
60 if (t == CharAlphabet.UNCLASSIFIED)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
61 t = m_alphabet.DefineSymbol(ch);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
62 return t;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
63 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
64
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
65 IEnumerable<int> TranslateOrAdd(IEnumerable<char> symbols) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
66 return symbols.Distinct().Select(TranslateOrAdd);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
67 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
68
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
69 int TranslateOrDie(char ch) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
70 var t = m_alphabet.Translate(ch);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
71 if (t == CharAlphabet.UNCLASSIFIED)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
72 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch));
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
73 return t;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
74 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
75
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
76 IEnumerable<int> TranslateOrDie(IEnumerable<char> symbols) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
77 return symbols.Distinct().Select(TranslateOrDie);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
78 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
79
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
80 public Token SymbolTokenExcept(IEnumerable<char> symbols) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
81 Safe.ArgumentNotNull(symbols, "symbols");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
82
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
83 return Token.New( Enumerable.Range(0, m_alphabet.Count).Except(TranslateOrDie(symbols)).ToArray());
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
84 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
85
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
86 protected CDFADefinition BuildDFA(Token lang) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
87 Safe.ArgumentNotNull(lang, "lang");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
88
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
89 var dfa = new CDFADefinition(m_alphabet);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
90
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
91 var builder = new DFABuilder();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
92
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
93 lang.Accept( builder );
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
94
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
95 builder.BuildDFA(dfa);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
96 if (dfa.InitialStateIsFinal)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
97 throw new ApplicationException("The specified language contains empty token");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
98
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
99 return dfa.Optimize();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
100 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
101
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
102
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
103
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
104 //protected abstract TGrammar CreateInstance();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
105 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
106
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
107
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
108 }