For a method I'm creating, I want to take in a word that is found on the end of a line, and I then want to append the word found to the left of it (at the start of the line up to a space character) to an array.
Here is my code so far:
def ruleElements(factor):
# Creates list of RHS and LHS rule elements
results = []
# If RHS factor is found in grammar, append corresponding LHS.
for line in grammarFile:
start = line.find(0)
end = line.find(' ', start)
if factor in line:
results.append(line[start:end])
return results
So far the outputted array comes up empty all the time. Not sure where my logic is wrong.
A line in the grammarFile looks like, for example:
VP -> V NP
NP -> N
VP -> V PP
I want to take the part on the right-side of -> as an input and append the left-side to an array to be used in other parts of the program.