1: /* ----------------------------------------------------------------------------
2: * Regex2Automaton
3: * ----------------------------------------------------------------------------
4: * Author: Luca Del Tongo
5: * Blog: blogs.ugidotnet.org/wetblog
6: * File: NFABuilder.cs
7: * Summary: Class used to build an NFA from a Regex Pattern
8: * Date: 31/03/2010
9: * ----------------------------------------------------------------------------
10: *
11: */
12:
13: namespace Regex2Automaton
14: {
15: using System;
16: using System.Collections.Generic;
17: using System.Linq;
18: using System.Text;
19:
20: /// <summary>
21: /// Class used to build an NFA from a Regex Pattern
22: /// </summary>
23: public class NFABuilder
24: {
25: public RegexParser RegexParser { get; set; }
26:
27: public NFABuilder()
28: {
29: this.RegexParser = new RegexParser();
30: }
31:
32: public NonDeterministicAutomaton BuildNFA(string pattern)
33: {
34: ParseTree parseTree = RegexParser.BuildParseTree(pattern);
35: return this.RecursiveTree2Automaton(parseTree);
36: }
37:
38: public NonDeterministicAutomaton BuildNFA(ParseTree patternParseTree)
39: {
40: return this.RecursiveTree2Automaton(patternParseTree);
41: }
42:
43: private NonDeterministicAutomaton RecursiveTree2Automaton(ParseTree current)
44: {
45: NonDeterministicAutomaton left,right,star;
46: left = new NonDeterministicAutomaton();
47: right = new NonDeterministicAutomaton();
48: star = new NonDeterministicAutomaton();
49: switch (current.NodeType)
50: {
51: case NodeType.AlternationOp:
52: left = this.RecursiveTree2Automaton(current.Left);
53: right = this.RecursiveTree2Automaton(current.Right);
54: return Alternation.Alternate2NFA(left, right);
55:
56: case NodeType.ConcatenationOp:
57: left = this.RecursiveTree2Automaton(current.Left);
58: right = this.RecursiveTree2Automaton(current.Right);
59: return Concatenation.Concatenate2NFA(left, right);
60:
61: case NodeType.KleenStarOp:
62: star = this.RecursiveTree2Automaton(current.Right);
63: return KleenStar.Iterate2NFA(star);
64:
65: case NodeType.Character:
66: return new AlphabetSymbol(current.Symbol).Convert2NFA();
67: }
68:
69: throw new Exception("Unknown conversion symbol");
70: }
71: }
72: }