comparison Implab/Parsing/Grammar.cs @ 55:c0bf853aa04f

Added initial JSON support +JSONParser +JSONWriter
author cin
date Sun, 15 Jun 2014 19:39:11 +0400
parents
children 130781364799
comparison
equal deleted inserted replaced
51:2c332a9c64c0 55:c0bf853aa04f
1 using Implab;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
7
8 namespace Implab.Parsing {
9 /// <summary>
10 /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>.
11 /// </summary>
12 /// <typeparam name="TGrammar"></typeparam>
13 public abstract class Grammar<TGrammar> where TGrammar: Grammar<TGrammar>, new() {
14 Alphabet m_alphabet = new Alphabet();
15 static TGrammar _instance;
16
17 public static TGrammar Instance{
18 get {
19 if (_instance == null)
20 _instance = new TGrammar();
21 return _instance;
22 }
23 }
24
25 public SymbolToken UnclassifiedToken() {
26 return new SymbolToken(Alphabet.UNCLASSIFIED);
27 }
28
29 public void DefineAlphabet(IEnumerable<char> alphabet) {
30 Safe.ArgumentNotNull(alphabet, "alphabet");
31
32 foreach (var ch in alphabet)
33 m_alphabet.DefineSymbol(ch);
34 }
35 public Token SymbolRangeToken(char start, char end) {
36 return SymbolToken(Enumerable.Range(start, end - start + 1).Select(x => (char)x));
37 }
38
39 public Token SymbolToken(char symbol) {
40 return Token.New(TranslateOrAdd(symbol));
41 }
42
43 public Token SymbolToken(IEnumerable<char> symbols) {
44 Safe.ArgumentNotNull(symbols, "symbols");
45
46 return Token.New(TranslateOrAdd(symbols).ToArray());
47 }
48
49 public Token SymbolSetToken(params char[] set) {
50 return SymbolToken(set);
51 }
52
53 int TranslateOrAdd(char ch) {
54 var t = m_alphabet.Translate(ch);
55 if (t == Alphabet.UNCLASSIFIED)
56 t = m_alphabet.DefineSymbol(ch);
57 return t;
58 }
59
60 IEnumerable<int> TranslateOrAdd(IEnumerable<char> symbols) {
61 return symbols.Distinct().Select(TranslateOrAdd);
62 }
63
64 int TranslateOrDie(char ch) {
65 var t = m_alphabet.Translate(ch);
66 if (t == Alphabet.UNCLASSIFIED)
67 throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch));
68 return t;
69 }
70
71 IEnumerable<int> TranslateOrDie(IEnumerable<char> symbols) {
72 return symbols.Distinct().Select(TranslateOrDie);
73 }
74
75 public Token SymbolTokenExcept(IEnumerable<char> symbols) {
76 Safe.ArgumentNotNull(symbols, "symbols");
77
78 return Token.New( Enumerable.Range(0, m_alphabet.Count).Except(TranslateOrDie(symbols)).ToArray());
79 }
80
81 protected CDFADefinition BuildDFA(Token lang) {
82 Safe.ArgumentNotNull(lang, "lang");
83
84 var dfa = new CDFADefinition(m_alphabet);
85
86 var builder = new DFABuilder();
87
88 lang.Accept( builder );
89
90 builder.BuildDFA(dfa);
91 if (dfa.InitialStateIsFinal)
92 throw new ApplicationException("The specified language contains empty token");
93
94 return dfa.Optimize();
95 }
96
97
98
99 //protected abstract TGrammar CreateInstance();
100 }
101
102
103 }