I want to code this but I am stuck
So suppose we have a grammar
S→x|LR|LT
T→SR
L→(
R→)
Here is how the list would look after each loop:
0 steps: [ S ]
1 step: [ x, LR, LT ]
2 steps: [ (R, L), (T, LSR ]
3 steps: [ (), (), (SR, (SR, LxR, LLRR, LLTR, LS) ]
and so on
Assume I want to check the string "(xx)" if it is in the grammar so I will do 2n-1 iteration which is 2x4-1=7 steps.
I am stuck as how to code to see the following:
Suppose I am on step 2. Now I want to expand LR. I loop over LR and I expand L to corrsponding RHS values which will be (R. This is done. Then I want to expand R in LR now I must use L and not ( so that I can achieve L). While looping how can I get L when my index moves to R?
Assume I am expanding S->LR the RHS rhs is a list of lists
for(int j=0;j<rhs.size();j++){//size of list
//size of every inside list such as {LR}
for(int k=0;k<rhs.get(j).length();k++){
//compare every variable with L and if matches right hand side RHS of L
//then move to R
}
My question
When expanding nth term how to add remaining right hand terms to current expansion and how to add left hand terms of the current expansion.
Example: I am expaning LR. If L then (R so i must add R with ( . Then when I got (R i must again get back L and expand R so I get L) ..so My final expansion of L will be(L and R)
Thanks
As you traverse a string and expand each character by its language rules, you create a new string by expanding in place. You don't transform the original string.
For instance, if you have LLTR, and you're expanding T, you can create a new string using substrings like
[LL] + expand(T) + [R]One way to do this is by maintaining a prefix, an index character, and a postfix. As you expand at the index, just prepend the prefix and append the postfix. You'll likely want to create a new list of from the
rhslist, rather than transformingrhsitself, otherwise you have to deal with maintaining the loop index over a list with a changing size.The following seems to work: