Click here to Skip to main content
15,888,157 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];
or
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];
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:
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];
Posted
Updated 6-Oct-20 9: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 https://www.codeproject.com/Articles/code-witch#Article[^] to see if they could help.
 
Share this answer
 
Comments
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

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