annotate Implab/Parsing/Grammar.cs @ 65:653c4e04968b

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