Click here to Skip to main content
14,699,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

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 diffrent here and the examples can be 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 comparasion 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 are diffrent from each other we return true like the follwoing example:
x = y + a[i] + 10;
so false will be return on something similar to this:
x = x + a[i];
x = x + a[i] + 10;
note: the letters could be diffrent. 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];
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:
So I want to compare
a [i] with b [i]
and compare
a [i] with c [i]

What I have tried:

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];
Updated 6-Oct-20 10:03am

1 solution

It sounds like there is not simple rule that you are following so you are almost at the point of needing a different rule for each string. Using a text parser could possibly simplify things for you in that you get get a set of tokens and relationships. Take a look at some of the parsing articles by[^] to see if they could help.
HishamMohammedA 6-Oct-20 15:23pm
I already receive the string using antlr getText() method but I was facing a problem when I tried to take each node because I don't know how to go deep in the tree.
Richard MacCutchan 6-Oct-20 15:30pm
Sorry, that becomes a different question, you did not mention antlr.
HishamMohammedA 6-Oct-20 15:33pm
it is still the same question, nothing change but all I was saying is that I already take the strings using antlr and I don't know how to fully utilize it that's why I am trying with strings now.
Richard MacCutchan 6-Oct-20 15:34pm
OK but you need to define your rules first.
HishamMohammedA 6-Oct-20 15:50pm
Can you elaborate please, because I didn't understand
Richard MacCutchan 6-Oct-20 16:06pm
Sorry but I don't understand what you are trying to do. You are splitting the statements into separate tokens but it is not clear what you are trying to compare.

You state:

So I want to compare
a [i] with b [i]
and compare
a [i] with c [i]

But what does that mean? In any simple terms both answers are false.
HishamMohammedA 6-Oct-20 16:22pm
I am sorry Richard, excuse my English maybe my explanation wasn't good enough.
let's say I have this statement:
a[i] = b[i] + c[i];
now I have split them into two-parts
the left side part -> a[i]
and the right side part -> b[i] + c[i]
I know when I compare between them it will be false, but I don't know how to split the second part of the string (right side) into two string like:
I am trying to do dependency analysis and in dependency analysis
if we have statement like this:a[i] = b[i] + c[i] there is no dependency so the test return true here but in this example:
a[i] = b[i] + a[i+1]; the dependency analysis should return false, why, because when we compare
a[i] with b[i] no depencency but when we compare a[i] with a[i+1] there is loop carried depencency
Richard MacCutchan 7-Oct-20 4:01am
Sorry, but that is something that I have not worked on. I suggest you open a new question with a better title and more details of exactly what you are trying to do. The title of this question asks if there is a way to compare strings, which really is not what this is about.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900