# Analyze One Line Expression - Recursive Function for Nested Tokens

I’m on the verge of a looping confusion. My goal is to create a python recursive function that can analyze nested tokens (in any pattern). The function can point out the wrong token index and displays the expected rules. If there’s no error, it will display the structure (dictionary type) and the calculated answer of the expression.

Details:

Code:

``````rules = {}
rules['i'] = []

for_branching = {}  # subject with parent-child indexes
branches = {}
token_index = -1
depth = -1
solved_indexes = []
success = True queue = {}

for s_x in range(0, len(subject)):
s = subject[s_x]
s_is_int = True if s.isnumeric() else False
token_symbol = 'i' if s_is_int is True else s
rule_found = True if token_symbol in rules else False
if rule_found is True:
r = rules[s]
if s_x+len(r) <= len(subject):
r_x = -1
for x1 in range(s_x+1, s_x+len(r)+1):
r_x += 1
s_is_int2 = True if subject[x1].isnumeric() else False
token_symbol2 = 'i' if s_is_int2 is True else subject[x1]
if token_symbol2 in r[r_x]:
if token_symbol2 in rules:
queue[x1] = rules[token_symbol]
else:
success = False
else:
success = False
print('ERROR: token # '+str(x1)+' expects \''+r[r_x]+'\'')
break

if success is False:
break
else:
print('the token expects '+str(len(r))+' token/s')
break

if success is False:
break
``````

to make it recursive you need a function that calls itself

Yes it is the whole code but it is incomplete. I haven’t enclosed it to a function yet because I’m stuck with the logic.

you can’t make it recursive without a function that calls itself

can you explain your logic here?

I can try to take a look later

the details are in here https://docs.google.com/document/d/1p3aT-0ByvdzJV72W3R4ysxSFeY4WgWkIX31bspi4iu0/edit#

PS: I’ve update the link with two examples.

please explain what your code does to me, and where it goes wrong

I am not sure of my code. It can only handle one nest of expression. My needed help is to make my code capable of handling nested expressions like `['ADD', 'SUB', '3', '2', 'MUL', '6', 'DIV', '10', '2']`

The expected output must be:

``````STRUCTURE:
{
'SUB': {
'3': {},
'2': {},
},
'MUL': {
'6': {},
'DIV': {
'10': {},
'2': {}
}
}
}
}