using Implab; using System; using System.Collections.Generic; using System.Linq; namespace Implab.Automaton.RegularExpressions { /// /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа char. /// public abstract class Grammar { protected abstract IAlphabetBuilder AlphabetBuilder { get; } protected SymbolToken UnclassifiedToken() { return new SymbolToken(DFAConst.UNCLASSIFIED_INPUT); } protected void DefineAlphabet(IEnumerable alphabet) { Safe.ArgumentNotNull(alphabet, "alphabet"); foreach (var ch in alphabet) AlphabetBuilder.DefineSymbol(ch); } protected Token SymbolToken(TSymbol symbol) { return Token.New(TranslateOrAdd(symbol)); } protected Token SymbolToken(IEnumerable symbols) { Safe.ArgumentNotNull(symbols, "symbols"); return Token.New(TranslateOrAdd(symbols).ToArray()); } protected Token SymbolSetToken(params TSymbol[] set) { return SymbolToken(set); } int TranslateOrAdd(TSymbol ch) { var t = AlphabetBuilder.Translate(ch); if (t == DFAConst.UNCLASSIFIED_INPUT) t = AlphabetBuilder.DefineSymbol(ch); return t; } IEnumerable TranslateOrAdd(IEnumerable symbols) { return symbols.Distinct().Select(TranslateOrAdd); } int TranslateOrDie(TSymbol ch) { var t = AlphabetBuilder.Translate(ch); if (t == DFAConst.UNCLASSIFIED_INPUT) throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch)); return t; } IEnumerable TranslateOrDie(IEnumerable symbols) { return symbols.Distinct().Select(TranslateOrDie); } protected Token SymbolTokenExcept(IEnumerable symbols) { Safe.ArgumentNotNull(symbols, "symbols"); return Token.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() ); } protected void BuildDFA(Token lang, IDFATableBuilder dfaTable, IAlphabetBuilder dfaAlphabet) { Safe.ArgumentNotNull(lang, "lang"); Safe.ArgumentNotNull(dfaAlphabet, "dfaAlphabet"); var dfa = new RegularDFADefinition(AlphabetBuilder); var builder = new RegularDFABuilder(); lang.Accept( builder ); builder.BuildDFA(dfa); if (dfa.IsFinalState(dfa.InitialState)) throw new ApplicationException("The specified language contains empty token"); dfa.Optimize(dfaTable, dfaAlphabet); } } }