comparison Implab/Automaton/RegularExpressions/Grammar.cs @ 172:92d5278d1b10 ref20160224

Working on text scanner
author cin
date Mon, 14 Mar 2016 01:19:38 +0300
parents e227e78d72e4
children 0c3c69fe225b
comparison
equal deleted inserted replaced
171:0f70905b4652 172:92d5278d1b10
64 Safe.ArgumentNotNull(symbols, "symbols"); 64 Safe.ArgumentNotNull(symbols, "symbols");
65 65
66 return Token<TTag>.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() ); 66 return Token<TTag>.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() );
67 } 67 }
68 68
69 protected void BuildDFA(Token<TTag> lang, IDFATableBuilder<TTag> dfaTable, IAlphabetBuilder<TSymbol> dfaAlphabet) { 69 protected abstract IAlphabetBuilder<TSymbol> CreateAlphabet();
70 Safe.ArgumentNotNull(lang, "lang");
71 Safe.ArgumentNotNull(dfaAlphabet, "dfaAlphabet");
72 70
73 var dfa = new RegularDFADefinition<TSymbol, TTag>(AlphabetBuilder); 71 protected RegularDFA<TSymbol, TTag> BuildDFA(Token<TTag> regexp) {
72
73 var dfa = new RegularDFA<TSymbol, TTag>(AlphabetBuilder);
74 74
75 var builder = new RegularDFABuilder<TTag>(); 75 var visitor = new RegularExpressionVisitor<TTag>();
76 regexp.Accept( visitor );
76 77
77 lang.Accept( builder ); 78 visitor.BuildDFA(dfa);
78
79 builder.BuildDFA(dfa);
80 79
81 if (dfa.IsFinalState(dfa.InitialState)) 80 if (dfa.IsFinalState(dfa.InitialState))
82 throw new ApplicationException("The specified language contains empty token"); 81 throw new ApplicationException("The specified language contains empty token");
83 82
84 dfa.Optimize(dfaTable, dfaAlphabet); 83 return dfa.Optimize(CreateAlphabet());
85
86 } 84 }
87 85
88 } 86 }
89 87
90 88