How to access immediate character of string in python -
i'm working on small program removes unit production in given grammar. i've developed 1 logic correct me. problem i'm facing cannot access next immediate index of string stored in list in python.
gram=[line.rstrip('\n') line in open("grammar.txt","r+")] non_terminals=[] productions=[] item in range(len(gram)): non_terminals.append(gram[item][0]) print(non_terminals) temp=[] item in range(len(gram)): str in non_terminals: if gram[item][0] str: productions.append(gram[item].lstrip(str).lstrip('-'+'>')) temp='' item in range(len(productions)): str in range(len(productions[item])): nt in non_terminals: if productions[item][str] nt: if productions[item][str-1] '|' , (productions[item] [str+1] '|'): i=nt.index(nt) temp=productions[item].rstrip(nt) temp=temp+productions[i] print(productions) print(temp)
i'm getting error
if productions[item][str-1] '|' , (productions[item][str+1] '|'): indexerror: string index out of range
text file contains
s->s+t|t t->t*f|f f->(s)|a
can suggest reason i'm not able so.?
Comments
Post a Comment