Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Analyze One Line Expression - Recursive Function for Nested Tokens

slimyslime777

New Coder
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.

Sample Input:

GVXqIFNy_zvaqvivvcSSDFFLCUAhbElBDAcZUfLKEKf3Fo42iTHIGqPLKj1FQz1r26wAL1b0cXMdjGc22_hBuWaQqXExVlNzACwHauDFaXJRvt8Hnqb8m5_iI__zXtUePPQt_Ghw


Expected Structure:
LeNyyYvTI8iNKf-FWx9I36DR3oSpvqJfkOF5zVhbF4eGklBMwGgnkfB512zyjxdBB_28O8-MxFkuCAScn87rwCmUPN-OtPAVFUUv_VVebVGMkrEUQynsrUrokRb3pKD8P6hc_-Oh


Rules:
By using this list.
XVJGoBl9FpP1cMpunzYOC7NmYzjmA4e_k_PHGd-ZfxczPtJMO5850ZG1HVFvg1ZzBDU97ficFkz4FbS6GO6bBABfMhq5nHzz2P08qVTIl_I8NWiyo-Aa-99gOP8226IdyQgpZ38B


Where:
6-ekAWKM98M1c9TG1fQtwUkvNHUXZRNRQnp8sk_Y_LQ9wwy27VYqBh5RBSR3Rf1hpG3y_JXyukro2K2SCiO-XFg8Cdo6xrlPh0SJRkf0il6Oz7oZquBq9huznwaaAsHSzhM-_Dms




PS:
I am very inefficient in implementing algorithms for recursion. This is what I've made so far but I already reached the confusion stage of my code. Different approach or optimization of the code below would be greatly appreciated.

Code:

[CODE lang="python" title="rules = {} rules['i'] = [] rules['ADD'] = ['i|ADD|SUB|MUL|DIV', 'i|ADD|SUB|MUL|DIV'] rules['SUB'] = ['i|ADD|SUB|MUL|DIV', 'i|ADD|SUB|MUL|DIV'] rules['MUL'] = ['i|ADD|SUB|MUL|DIV', 'i|ADD|SUB|MUL|DIV'] rules['DIV'] = ['i|ADD|SUB|MUL|DIV', 'i|ADD|SUB|MUL|DIV'] subject = ['ADD','1','1'] 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"]rules = {}
rules['i'] = []
rules['ADD'] = ['i|ADD|SUB|MUL|DIV', 'i|ADD|SUB|MUL|DIV']
rules['SUB'] = ['i|ADD|SUB|MUL|DIV', 'i|ADD|SUB|MUL|DIV']
rules['MUL'] = ['i|ADD|SUB|MUL|DIV', 'i|ADD|SUB|MUL|DIV']
rules['DIV'] = ['i|ADD|SUB|MUL|DIV', 'i|ADD|SUB|MUL|DIV']

subject = ['ADD','1','1']
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
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[/CODE]
 
Back
Top Bottom