diff Implab/Automaton/RegularExpressions/Grammar.cs @ 165:e227e78d72e4 ref20160224

DFA refactoring
author cin
date Mon, 29 Feb 2016 02:02:17 +0300
parents ec35731ae299
children 92d5278d1b10
line wrap: on
line diff
--- a/Implab/Automaton/RegularExpressions/Grammar.cs	Thu Feb 25 02:11:13 2016 +0300
+++ b/Implab/Automaton/RegularExpressions/Grammar.cs	Mon Feb 29 02:02:17 2016 +0300
@@ -2,48 +2,46 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 
 namespace Implab.Automaton.RegularExpressions {
     /// <summary>
     /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>.
     /// </summary>
-        public abstract class Grammar<TSymbol, TTag> {
+    public abstract class Grammar<TSymbol, TTag> {
         
-        public abstract IAlphabetBuilder<TSymbol> Alphabet {
+        protected abstract IAlphabetBuilder<TSymbol> AlphabetBuilder {
             get;
         }
 
-        public SymbolToken<TTag> UnclassifiedToken() {
+        protected SymbolToken<TTag> UnclassifiedToken() {
             return new SymbolToken<TTag>(DFAConst.UNCLASSIFIED_INPUT);
         }
 
-        public void DefineAlphabet(IEnumerable<TSymbol> alphabet) {
+        protected void DefineAlphabet(IEnumerable<TSymbol> alphabet) {
             Safe.ArgumentNotNull(alphabet, "alphabet");
 
             foreach (var ch in alphabet)
-                Alphabet.DefineSymbol(ch);
+                AlphabetBuilder.DefineSymbol(ch);
         }
 
-        public Token<TTag> SymbolToken(TSymbol symbol) {
+        protected Token<TTag> SymbolToken(TSymbol symbol) {
             return Token<TTag>.New(TranslateOrAdd(symbol));
         }
 
-        public Token<TTag> SymbolToken(IEnumerable<TSymbol> symbols) {
+        protected Token<TTag> SymbolToken(IEnumerable<TSymbol> symbols) {
             Safe.ArgumentNotNull(symbols, "symbols");
 
             return Token<TTag>.New(TranslateOrAdd(symbols).ToArray());
         }
 
-        public Token<TTag> SymbolSetToken(params TSymbol[] set) {
+        protected Token<TTag> SymbolSetToken(params TSymbol[] set) {
             return SymbolToken(set);
         }
 
         int TranslateOrAdd(TSymbol ch) {
-            var t = Alphabet.Translate(ch);
+            var t = AlphabetBuilder.Translate(ch);
             if (t == DFAConst.UNCLASSIFIED_INPUT)
-                t = Alphabet.DefineSymbol(ch);
+                t = AlphabetBuilder.DefineSymbol(ch);
             return t;
         }
 
@@ -52,7 +50,7 @@
         }
 
         int TranslateOrDie(TSymbol ch) {
-            var t = Alphabet.Translate(ch);
+            var t = AlphabetBuilder.Translate(ch);
             if (t == DFAConst.UNCLASSIFIED_INPUT)
                     throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch));
             return t;
@@ -62,33 +60,31 @@
             return symbols.Distinct().Select(TranslateOrDie);
         }
 
-        public Token<TTag> SymbolTokenExcept(IEnumerable<TSymbol> symbols) {
+        protected Token<TTag> SymbolTokenExcept(IEnumerable<TSymbol> symbols) {
             Safe.ArgumentNotNull(symbols, "symbols");
 
-            return Token<TTag>.New( Enumerable.Range(0, Alphabet.Count).Except(TranslateOrDie(symbols)).ToArray() );
+            return Token<TTag>.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() );
         }
 
-        protected CDFADefinition BuildDFA(Token<TTag> lang) {
+        protected void BuildDFA(Token<TTag> lang, IDFATableBuilder<TTag> dfaTable, IAlphabetBuilder<TSymbol> dfaAlphabet) {
             Safe.ArgumentNotNull(lang, "lang");
+            Safe.ArgumentNotNull(dfaAlphabet, "dfaAlphabet");
 
-            var dfa = new RegularDFADefinition<TSymbol, TTag>(Alphabet, 0);
-
-            var table = new DFATransitionTable<TTag>();
+            var dfa = new RegularDFADefinition<TSymbol, TTag>(AlphabetBuilder);
 
             var builder = new RegularDFABuilder<TTag>();
 
             lang.Accept( builder );
 
-            var initialState = builder.BuildDFA(table);
-            if (table.IsFinalState(initialState))
+            builder.BuildDFA(dfa);
+
+            if (dfa.IsFinalState(dfa.InitialState))
                 throw new ApplicationException("The specified language contains empty token");
 
-            return dfa.Optimize();
+            dfa.Optimize(dfaTable, dfaAlphabet);
+
         }
 
-        
-
-        //protected abstract TGrammar CreateInstance();
     }