Mercurial > pub > ImplabNet
diff 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 |
line wrap: on
line diff
--- a/Implab/Automaton/RegularExpressions/Grammar.cs Wed Feb 24 08:39:53 2016 +0300 +++ b/Implab/Automaton/RegularExpressions/Grammar.cs Wed Feb 24 20:12:52 2016 +0300 @@ -9,86 +9,71 @@ /// <summary> /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>. /// </summary> - /// <typeparam name="TGrammar"></typeparam> - public abstract class Grammar<TGrammar> where TGrammar: Grammar<TGrammar>, new() { - static TGrammar _instance; + public abstract class Grammar<TSymbol, TTag> { - public static TGrammar Instance{ - get { - if (_instance == null) - _instance = new TGrammar(); - return _instance; - } + public abstract IAlphabetBuilder<TSymbol> Alphabet { + get; } - readonly CharAlphabet m_alphabet = new CharAlphabet(); - - public CharAlphabet Alphabet { - get { return m_alphabet; } + public SymbolToken<TTag> UnclassifiedToken() { + return new SymbolToken<TTag>(DFAConst.UNCLASSIFIED_INPUT); } - public SymbolToken UnclassifiedToken() { - return new SymbolToken(CharAlphabet.UNCLASSIFIED); - } - - public void DefineAlphabet(IEnumerable<char> alphabet) { + public void DefineAlphabet(IEnumerable<TSymbol> alphabet) { Safe.ArgumentNotNull(alphabet, "alphabet"); foreach (var ch in alphabet) - m_alphabet.DefineSymbol(ch); + Alphabet.DefineSymbol(ch); } - public Token SymbolRangeToken(char start, char end) { - return SymbolToken(Enumerable.Range(start, end - start + 1).Select(x => (char)x)); + + public Token<TTag> SymbolToken(TSymbol symbol) { + return Token<TTag>.New(TranslateOrAdd(symbol)); } - public Token SymbolToken(char symbol) { - return Token.New(TranslateOrAdd(symbol)); + public Token<TTag> SymbolToken(IEnumerable<TSymbol> symbols) { + Safe.ArgumentNotNull(symbols, "symbols"); + + return Token<TTag>.New(TranslateOrAdd(symbols).ToArray()); } - public Token SymbolToken(IEnumerable<char> symbols) { - Safe.ArgumentNotNull(symbols, "symbols"); - - return Token.New(TranslateOrAdd(symbols).ToArray()); - } - - public Token SymbolSetToken(params char[] set) { + public Token<TTag> SymbolSetToken(params TSymbol[] set) { return SymbolToken(set); } - int TranslateOrAdd(char ch) { - var t = m_alphabet.Translate(ch); - if (t == CharAlphabet.UNCLASSIFIED) - t = m_alphabet.DefineSymbol(ch); + int TranslateOrAdd(TSymbol ch) { + var t = Alphabet.Translate(ch); + if (t == DFAConst.UNCLASSIFIED_INPUT) + t = Alphabet.DefineSymbol(ch); return t; } - IEnumerable<int> TranslateOrAdd(IEnumerable<char> symbols) { + IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) { return symbols.Distinct().Select(TranslateOrAdd); } - int TranslateOrDie(char ch) { - var t = m_alphabet.Translate(ch); - if (t == CharAlphabet.UNCLASSIFIED) + int TranslateOrDie(TSymbol ch) { + var t = Alphabet.Translate(ch); + if (t == DFAConst.UNCLASSIFIED_INPUT) throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch)); return t; } - IEnumerable<int> TranslateOrDie(IEnumerable<char> symbols) { + IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) { return symbols.Distinct().Select(TranslateOrDie); } - public Token SymbolTokenExcept(IEnumerable<char> symbols) { + public Token<TTag> SymbolTokenExcept(IEnumerable<TSymbol> symbols) { Safe.ArgumentNotNull(symbols, "symbols"); - return Token.New( Enumerable.Range(0, m_alphabet.Count).Except(TranslateOrDie(symbols)).ToArray()); + return Token<TTag>.New( Enumerable.Range(0, Alphabet.Count).Except(TranslateOrDie(symbols)).ToArray() ); } - protected CDFADefinition BuildDFA(Token lang) { + protected CDFADefinition BuildDFA(Token<TTag> lang) { Safe.ArgumentNotNull(lang, "lang"); - var dfa = new CDFADefinition(m_alphabet); + var dfa = new CDFADefinition(Alphabet); - var builder = new DFABuilder(); + var builder = new RegularDFABuilder<TTag>(); lang.Accept( builder );