1

maybe my question doesn't match with the content of the message but I hope I explained my problem good.

I have two scenarios

First scenario:

If I recive Strings like this:

x = x + 10;x = x + a [i]; x = x + a [i] +10;

note: the letters can be different here and the examples can differ too. I separated the side before the = sign from the side after the = sign So I've got ArrayList where the elements look like this:

  • Index 0 -> x
  • Index 1 -> x + 10
  • Index 2 -> x
  • Index 3 -> a [i]
  • Index 4 -> x
  • Index 5 -> x + a [i] +10

I want to make a comparison now between the sentences, so I will make a comparison between Index 0 and Index 1 and between Index 2, Index 3, and so on.

Let's name the side before equals (the left side) to str1 and after the equal (the right side) to str2

Is there a possibility to make a comparison between the two sides and said if the str1 is variable and str2 two is the name of the same variable and array come after the name of the variable like the following example:

x = x + a[i] * 10;

we return false. But if the name of the variables is different from each other we return true like the following example:

x = y + a[i] + 10;

so false will be return on something similar to this:

x = x + a[i];

or

x = x + a[i] + 10;

note: the letters could be different. I mean we could have something like this:

sum = sum + a[i]; and so on.

The second scenario:

Note: Letters and numbers can be changed.

if I have string like the following:

a [i] = b [i] + c [i];

or

a [i] = b [i + 1] * c [i] +10;

I was able to separate the side before = and the side after = in the same way as I mentioned before, and it looks like this:

  • Index 0 -> a [i]
  • Index 1 -> b [i] + c [i]
  • Index 2 -> a [i]
  • Index 3 -> b [i + 1] * c [i] +10

the question here is:

Can I separate the left side from each other as well? I mean, I want the first sentence to be such

  • a [i] by itself
  • b [i] by itself
  • c [i] by itself

same things about the second example because I want to compare like this: a [i] with b [i]

and compare a [i] with c [i]

What I have tried:

I split the string using ";" then I separate between the two sides as I mentioned above and I used charAT but I don't know how to find out about the rest of the statement like in this example: x = x + a[i]; it will return 1 because x = x but I don't know how to check for the rest of the string, and How I separate the string more like this example: a[i] = b[i] + c[i+1];

Thank you in advance

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Hisham
  • 11
  • 3
  • You seem to be asking several questions here. The title asks about *comparing*, but the text seems more focused on *splitting* or *parsing*. Please narrow your question to just one question, and remove as much as you can that isn't absolutely necessary to describe your problem. – Bohemian Oct 06 '20 at 22:28
  • Thank you for editing my question, and sorry for the confusion about the question I was trying to explain it and describe my problem so the one who read can understand what I do need exactly. and yes my main concern is comparison but I can't do it because I don't know how to split the parts to do proper comparison. – Hisham Oct 06 '20 at 22:37
  • When you say: `Index 3 -> a [i]`, I assume that is a typo, and you meant to write `Index 3 -> x + a[i]`? If so, please edit, if not, hmm, perhaps explain a bit more about the algorithm :) – rzwitserloot Oct 06 '20 at 22:52
  • @Hisham if that’s the case, please ask separate questions - one question for each concept/problem. You can either close this question and ask two new ones, or modify this one to narrow it to just one idea and ask another separate question. – Bohemian Oct 06 '20 at 23:24
  • @rzwitserloot yes you right it is a typo and is supposed to be the same thing that you wrote – Hisham Oct 07 '20 at 14:30

1 Answers1

0

It sounds like you want to properly parse a 'calculator' string. Let's say this is your string:

x = x + a[i] * (10 + 2)

You'd want to parse this into an abstract syntax tree:

   -- = --
  /       \
 x       - + -
        /     \
     -- * --   x
    /       \
   +        []
  / \      /  \
 10  2    a    i

Note how, for example, this syntax tree has processed things so that a * takes precedence (we do a[i] * (10 + 2) first, and only then do we add that end result to x, which is not the same thing as doing x + a[i] first, and then multiplying that. After all, 1 + 2 * 3 is 9 if you just read left-to-right, but 7 if you apply standard aritmetic precedence rules. I assume you want those.

Also, this example includes parentheses, another common tool.

Once you have such a tree, resolving the entire thing is very simple using a recursive algorithm, as is doing analyses such as what you appear to be asking for.

So, your question really boils down to:

How do I translate such a string into one of these AST thingies?

And the answer to THAT is: Well, it's more complicated than you think. But it IS a solved problem - there are a ton of parser generators for java available. Search the web for 'turn calculator string into tree parser java' which for example gets you to this SO answer.

Yes, trees are somewhat complex constructs, but, as I said, that's because the concept is just fundamentally a bit complicated, what with precedence and all that.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • so I have to use AST, can regex solve this type of problem? – Hisham Oct 07 '20 at 14:33
  • I received the string using antlr because this string will be inside for loop, so I was able to locate for loops in the input file and get the string as text then I do the analysis in another class. it was difficult and complicated to follow the AST as you mentioned in your answer that's why I tried to analyze the string without using AST – Hisham Oct 07 '20 at 14:45
  • also, the string will be received from the user, so I wouldn't know how they can be, and it can be different from the above but it will have the same concept that I mentioned in the examples previously. excuse my English but I am trying my best to explain the issue I am facing correctly. – Hisham Oct 07 '20 at 15:46
  • Regular expression cannot do this; calculation expressions like this are not regular. If you're already using ANTLR, upgrade your parser definition to be able to parse this down to the individual nodes instead of the entire string at once. If the inputs can be even more varied than what you explained, you DEFINITELY need a full blown parser. – rzwitserloot Oct 07 '20 at 16:06