comparison Implab/Automaton/RegularExpressions/Grammar.cs @ 176:0c3c69fe225b ref20160224

rewritten the text scanner
author cin
date Tue, 22 Mar 2016 18:58:40 +0300
parents 92d5278d1b10
children
comparison
equal deleted inserted replaced
175:96a89dcb4060 176:0c3c69fe225b
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 abstract IAlphabetBuilder<TSymbol> CreateAlphabet(); 69 protected abstract IndexedAlphabetBase<TSymbol> CreateAlphabet();
70 70
71 protected RegularDFA<TSymbol, TTag> BuildDFA(Token<TTag> regexp) { 71 protected ScannerContext<TTag> BuildScannerContext(Token<TTag> regexp) {
72 72
73 var dfa = new RegularDFA<TSymbol, TTag>(AlphabetBuilder); 73 var dfa = new RegularDFA<TSymbol, TTag>(AlphabetBuilder);
74 74
75 var visitor = new RegularExpressionVisitor<TTag>(); 75 var visitor = new RegularExpressionVisitor<TTag>();
76 regexp.Accept( visitor ); 76 regexp.Accept( visitor );
78 visitor.BuildDFA(dfa); 78 visitor.BuildDFA(dfa);
79 79
80 if (dfa.IsFinalState(dfa.InitialState)) 80 if (dfa.IsFinalState(dfa.InitialState))
81 throw new ApplicationException("The specified language contains empty token"); 81 throw new ApplicationException("The specified language contains empty token");
82 82
83 return dfa.Optimize(CreateAlphabet()); 83 var ab = CreateAlphabet();
84 var optimal = dfa.Optimize(ab);
85
86 return new ScannerContext<TTag>(
87 optimal.CreateTransitionTable(),
88 optimal.CreateFinalStateTable(),
89 optimal.CreateTagTable(),
90 optimal.InitialState,
91 ab.GetTranslationMap()
92 );
84 } 93 }
85 94
86 } 95 }
87 96
88 97