0

I've a problem which puzzles me, and I can't figure it out how to do this in C#.

Let's say, I have this formula: ((true or false) and (false or true))

This is just a sample formula, but it could be much more complex like this: ((((true or ((((true or false) and (false or ((true or false) and (false or ((((true or false) and (false or ((true or false) and (false or true)))) or not ((true or false) and (false or ((true or false) and (false or true)))))))))) or not ((true or false) and (false or ((true or false) and (false or true))))))) and (false or ((true or false) and (false or ((((true or false) and (false or ((true or false) and (false or true)))) or not ((true or false) and (false or ((true or false) and (false or true)))))))))) or not ((true or false) and (false or ((true or false) and (false or true)))))) etc.

I'd like to break it into three parts

  1. (true or false)
  2. and
  3. (false or true)

In an other way, I'd like to break this formula, to its two partial-formula and its operand. Right now, I have two binary operator (or,and) and one unary operator (not).

How can I solve this problem? Could you help me on this? I've tried regular expressions, with Grouping Constructs, but I'm not an expert of regular expressions.

Thanks, Otto

giotto
  • 39
  • 4
  • This is posibly not a use of Regular Expression, but Parsers. – user3021830 Feb 20 '15 at 10:40
  • Array is not a best structure for storing such a data since it is actually *expression tree*, not a flat list. Also regular expressions is not a best tool for parsing such expressions, and such a parsing itself can be complicated task, so probably you should look to the some lexical analyzer. – Andrey Korneyev Feb 20 '15 at 10:43
  • as far as I know, regular expressions can be used for this, if you use Grouping Constructs for regex in .Net. – giotto Feb 20 '15 at 11:34

2 Answers2

1

This problem has simply solution. Reverse Polish Notation (RPN) Convert you string to RNP, and then execute it.

Andrey Ganin
  • 349
  • 1
  • 10
0

Otto, without knowing why you want to do this I don't know whether or not an array of strings would be the best approach or not; the problem is, if you just have an array of "(true OR false)" etc, you lose the structure of the original expression. My solution uses integers starting from 0 as placeholders to show where the strings were extracted from the expression. Hope this helps! Greg

using System;
using System.Collections.Generic;

namespace ParseParentheses
{
    class Program
    {
        static List<string> wordList = new List<string>();
        static int wordIndex = -1;

    static void Main(string[] args)
    {
        GetChunks("(true AND (true OR false)) OR ((true AND false) OR false)");
        // Now we know how many items we have, convert the List to an array,
        // as this is what the solution specified.
        string[] words = wordList.ToArray();
        for (int i = 0; i < words.Length; i++)
        {
            Console.WriteLine(i + ":\t" + words[i]);
        }
    }

    private static void GetChunks(string text)
    {
        int start;
        int end = text.IndexOf(')');
        if (end > -1)
        {
            start = text.LastIndexOf('(', end - 1);
            if (start == -1)
            {
                throw new ArgumentException("Mismatched parentheses", text);
            }
            wordList.Add(text.Substring(start, end - start + 1));
            wordIndex++;
            text = text.Substring(0, start)
                + wordIndex.ToString()
                + text.Substring(end + 1);

            GetChunks(text);
        }
        else
        {
        // no more ) in text, just add what's left to the List.
            wordList.Add(text);
        }
    }
  }
}
IglooGreg
  • 147
  • 5