##// END OF EJS Templates
sync
sync

File last commit:

r165:e227e78d72e4 ref20160224
r167:96681e9d0cea ref20160224
Show More
Grammar.cs
91 lines | 3.1 KiB | text/x-csharp | CSharpLexer
cin
DFA refactoring
r162 using Implab;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Implab.Automaton.RegularExpressions {
/// <summary>
/// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>.
/// </summary>
cin
DFA refactoring
r165 public abstract class Grammar<TSymbol, TTag> {
cin
DFA refactoring
r162
cin
DFA refactoring
r165 protected abstract IAlphabetBuilder<TSymbol> AlphabetBuilder {
cin
JSON moved to Formats namespace...
r163 get;
cin
DFA refactoring
r162 }
cin
DFA refactoring
r165 protected SymbolToken<TTag> UnclassifiedToken() {
cin
JSON moved to Formats namespace...
r163 return new SymbolToken<TTag>(DFAConst.UNCLASSIFIED_INPUT);
cin
DFA refactoring
r162 }
cin
DFA refactoring
r165 protected void DefineAlphabet(IEnumerable<TSymbol> alphabet) {
cin
DFA refactoring
r162 Safe.ArgumentNotNull(alphabet, "alphabet");
foreach (var ch in alphabet)
cin
DFA refactoring
r165 AlphabetBuilder.DefineSymbol(ch);
cin
DFA refactoring
r162 }
cin
JSON moved to Formats namespace...
r163
cin
DFA refactoring
r165 protected Token<TTag> SymbolToken(TSymbol symbol) {
cin
JSON moved to Formats namespace...
r163 return Token<TTag>.New(TranslateOrAdd(symbol));
cin
DFA refactoring
r162 }
cin
DFA refactoring
r165 protected Token<TTag> SymbolToken(IEnumerable<TSymbol> symbols) {
cin
JSON moved to Formats namespace...
r163 Safe.ArgumentNotNull(symbols, "symbols");
return Token<TTag>.New(TranslateOrAdd(symbols).ToArray());
cin
DFA refactoring
r162 }
cin
DFA refactoring
r165 protected Token<TTag> SymbolSetToken(params TSymbol[] set) {
cin
DFA refactoring
r162 return SymbolToken(set);
}
cin
JSON moved to Formats namespace...
r163 int TranslateOrAdd(TSymbol ch) {
cin
DFA refactoring
r165 var t = AlphabetBuilder.Translate(ch);
cin
JSON moved to Formats namespace...
r163 if (t == DFAConst.UNCLASSIFIED_INPUT)
cin
DFA refactoring
r165 t = AlphabetBuilder.DefineSymbol(ch);
cin
DFA refactoring
r162 return t;
}
cin
JSON moved to Formats namespace...
r163 IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) {
cin
DFA refactoring
r162 return symbols.Distinct().Select(TranslateOrAdd);
}
cin
JSON moved to Formats namespace...
r163 int TranslateOrDie(TSymbol ch) {
cin
DFA refactoring
r165 var t = AlphabetBuilder.Translate(ch);
cin
JSON moved to Formats namespace...
r163 if (t == DFAConst.UNCLASSIFIED_INPUT)
cin
DFA refactoring
r162 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch));
return t;
}
cin
JSON moved to Formats namespace...
r163 IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) {
cin
DFA refactoring
r162 return symbols.Distinct().Select(TranslateOrDie);
}
cin
DFA refactoring
r165 protected Token<TTag> SymbolTokenExcept(IEnumerable<TSymbol> symbols) {
cin
DFA refactoring
r162 Safe.ArgumentNotNull(symbols, "symbols");
cin
DFA refactoring
r165 return Token<TTag>.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() );
cin
DFA refactoring
r162 }
cin
DFA refactoring
r165 protected void BuildDFA(Token<TTag> lang, IDFATableBuilder<TTag> dfaTable, IAlphabetBuilder<TSymbol> dfaAlphabet) {
cin
DFA refactoring
r162 Safe.ArgumentNotNull(lang, "lang");
cin
DFA refactoring
r165 Safe.ArgumentNotNull(dfaAlphabet, "dfaAlphabet");
cin
DFA refactoring
r162
cin
DFA refactoring
r165 var dfa = new RegularDFADefinition<TSymbol, TTag>(AlphabetBuilder);
cin
Almost complete DFA refactoring
r164
cin
JSON moved to Formats namespace...
r163 var builder = new RegularDFABuilder<TTag>();
cin
DFA refactoring
r162
lang.Accept( builder );
cin
DFA refactoring
r165 builder.BuildDFA(dfa);
if (dfa.IsFinalState(dfa.InitialState))
cin
DFA refactoring
r162 throw new ApplicationException("The specified language contains empty token");
cin
DFA refactoring
r165 dfa.Optimize(dfaTable, dfaAlphabet);
cin
DFA refactoring
r162 }
}
}