annotate 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
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>
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
12 public abstract class Grammar<TSymbol, TTag> {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
13
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
14 public abstract IAlphabetBuilder<TSymbol> Alphabet {
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
15 get;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
16 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
17
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
18 public SymbolToken<TTag> UnclassifiedToken() {
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
19 return new SymbolToken<TTag>(DFAConst.UNCLASSIFIED_INPUT);
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
20 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
21
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
22 public void DefineAlphabet(IEnumerable<TSymbol> alphabet) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
23 Safe.ArgumentNotNull(alphabet, "alphabet");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
24
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
25 foreach (var ch in alphabet)
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
26 Alphabet.DefineSymbol(ch);
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
27 }
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
28
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
29 public Token<TTag> SymbolToken(TSymbol symbol) {
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
30 return Token<TTag>.New(TranslateOrAdd(symbol));
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
31 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
32
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
33 public Token<TTag> SymbolToken(IEnumerable<TSymbol> symbols) {
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
34 Safe.ArgumentNotNull(symbols, "symbols");
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
35
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
36 return Token<TTag>.New(TranslateOrAdd(symbols).ToArray());
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
37 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
38
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
39 public Token<TTag> SymbolSetToken(params TSymbol[] set) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
40 return SymbolToken(set);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
41 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
42
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
43 int TranslateOrAdd(TSymbol ch) {
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
44 var t = Alphabet.Translate(ch);
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
45 if (t == DFAConst.UNCLASSIFIED_INPUT)
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
46 t = Alphabet.DefineSymbol(ch);
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
47 return t;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
48 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
49
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
50 IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
51 return symbols.Distinct().Select(TranslateOrAdd);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
52 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
53
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
54 int TranslateOrDie(TSymbol ch) {
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
55 var t = Alphabet.Translate(ch);
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
56 if (t == DFAConst.UNCLASSIFIED_INPUT)
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
57 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch));
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
58 return t;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
59 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
60
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
61 IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
62 return symbols.Distinct().Select(TranslateOrDie);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
63 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
64
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
65 public Token<TTag> SymbolTokenExcept(IEnumerable<TSymbol> symbols) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
66 Safe.ArgumentNotNull(symbols, "symbols");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
67
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
68 return Token<TTag>.New( Enumerable.Range(0, Alphabet.Count).Except(TranslateOrDie(symbols)).ToArray() );
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
69 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
70
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
71 protected CDFADefinition BuildDFA(Token<TTag> lang) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
72 Safe.ArgumentNotNull(lang, "lang");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
73
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
74 var dfa = new CDFADefinition(Alphabet);
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
75
163
419aa51b04fd JSON moved to Formats namespace
cin
parents: 162
diff changeset
76 var builder = new RegularDFABuilder<TTag>();
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
77
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
78 lang.Accept( builder );
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
79
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
80 builder.BuildDFA(dfa);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
81 if (dfa.InitialStateIsFinal)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
82 throw new ApplicationException("The specified language contains empty token");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
83
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
84 return dfa.Optimize();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
85 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
86
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
87
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
88
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
89 //protected abstract TGrammar CreateInstance();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
90 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
91
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
92
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
93 }