##// END OF EJS Templates
sync
sync

File last commit:

r165:e227e78d72e4 ref20160224
r168:8fb9c9507a26 ref20160224
Show More
Grammar.cs
91 lines | 3.1 KiB | text/x-csharp | CSharpLexer
using Implab;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Implab.Automaton.RegularExpressions {
/// <summary>
/// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>.
/// </summary>
public abstract class Grammar<TSymbol, TTag> {
protected abstract IAlphabetBuilder<TSymbol> AlphabetBuilder {
get;
}
protected SymbolToken<TTag> UnclassifiedToken() {
return new SymbolToken<TTag>(DFAConst.UNCLASSIFIED_INPUT);
}
protected void DefineAlphabet(IEnumerable<TSymbol> alphabet) {
Safe.ArgumentNotNull(alphabet, "alphabet");
foreach (var ch in alphabet)
AlphabetBuilder.DefineSymbol(ch);
}
protected Token<TTag> SymbolToken(TSymbol symbol) {
return Token<TTag>.New(TranslateOrAdd(symbol));
}
protected Token<TTag> SymbolToken(IEnumerable<TSymbol> symbols) {
Safe.ArgumentNotNull(symbols, "symbols");
return Token<TTag>.New(TranslateOrAdd(symbols).ToArray());
}
protected Token<TTag> 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<int> TranslateOrAdd(IEnumerable<TSymbol> 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<int> TranslateOrDie(IEnumerable<TSymbol> symbols) {
return symbols.Distinct().Select(TranslateOrDie);
}
protected Token<TTag> SymbolTokenExcept(IEnumerable<TSymbol> symbols) {
Safe.ArgumentNotNull(symbols, "symbols");
return Token<TTag>.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() );
}
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>(AlphabetBuilder);
var builder = new RegularDFABuilder<TTag>();
lang.Accept( builder );
builder.BuildDFA(dfa);
if (dfa.IsFinalState(dfa.InitialState))
throw new ApplicationException("The specified language contains empty token");
dfa.Optimize(dfaTable, dfaAlphabet);
}
}
}