Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

An Implementation of Regular Expression Parser in C#

, 24 Jun 2009 CPOL
An article on how one can implement regular expression parser
LexAnal3_regexpr2nfa.zip
LexAnal3_regexpr2nfa.pdf
LexAnal4_nfa2dfa.zip
LexAnal4_nfa2dfa.pdf
LexAnal5_minimaldfa.zip
LexAnal5_minimaldfa.pdf
RegExDemo.zip
RegExDemo
Driver
MatchInfoDS.xsc
MatchInfoDS.xss
Properties
Settings.settings
RegExImpl.suo
RegularExpression
FsaDS.xsc
FsaDS.xss
Properties
Settings.settings
RegExSource.zip
RegExSource
RegularExpression
FsaDS.xsc
FsaDS.xss
Properties
Settings.settings
RegEx_Demo.zip
RegularExpression.dll
Driver.exe
RegEx_Src.zip
RegularExpression
Properties
Settings.settings
Driver
MatchInfoDS.xsc
MatchInfoDS.xss
Properties
Settings.settings
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace RegularExpression
{
  /// <summary>
  /// this class represent a Set in Mathmatics, nothing else.
  /// Since .NET does not have any built-in class for Set, I decided to create one myself.
  /// Since this programm does many Set opertions, this class will help writing clean code
  /// Many of the methods of this class have not been used in this program.
  /// </summary>
  public class Set : CollectionBase
  {
    public object this[int index]
    {
      get
      {
        return ((object)List[index]);
      }
      set
      {
        List[index] = value;
      }
    }

    public int AddElement(object element)
    {
      if (Contains(element) == false)
      {
        return List.Add(element);
      }
      return -1;
    }

    public void AddElementRange(object[] arrValue)
    {
      foreach (object obj in arrValue)
      {
        if (Contains(obj) == false)
        {
          List.Add(obj);
        }
      }
    }

    public void Union(Set setValue)
    {
      foreach (object obj in setValue)
      {
        if (Contains(obj) == false)
        {
          List.Add(obj);
        }
      }
    }

    public bool IsSubset(Set setValue)
    {
      foreach (object obj in setValue)
      {
        if (Contains(obj) == false)
        {
          return false;
        }
      }
      return true;
    }

    public bool IsProperSubset(Set setValue)
    {
      if (GetCardinality() > setValue.GetCardinality() && IsSubset(setValue) )
      {
        return true;
      }

      return false;

    }

    public void Subtract(Set setValue)
    {
      foreach (object obj in setValue)
      {
        if (Contains(obj) == true)
        {
          RemoveElement(obj);
        }
      }
    }

    public bool IsEqual(Set setValue)
    {
      if (GetCardinality() == setValue.GetCardinality() && IsSubset(setValue))
      {
        return true;
      }

      return false;

    }

    public bool IsEmpty()
    {
      return List.Count == 0 ? true : false;
    }

    public int GetCardinality()
    {
      return List.Count;
    }

    public bool ElementExist(object value)
    {
      return Contains(value);
    }

    public int IndexOf(object value)
    {
      return (List.IndexOf(value));
    }

    public bool RemoveElement(object value)
    {
      if (Contains(value) == true)
      {
        List.Remove(value);
        return true;
      }

      return false;

    }

    private bool Contains(object value)
    {
      // If value is not of type Int16, this will return false.
      return List.Contains(value);
    }
    
    protected override void OnInsert(int index, object value)
    {
      if (value == null)
      {
        throw new ArgumentException("Element cannot not be null.");
      }

      if (Contains(value) == true )
      {
        throw new ArgumentException("Element already exists in the set.", "value: " + value.ToString());
      }

      

    }

    public Array ToArray(Type type)
    {
      return base.InnerList.ToArray(type);
    }


  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mizan Rahman

Denmark Denmark
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 24 Jun 2009
Article Copyright 2008 by Mizan Rahman
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid