Dec-22-2019, 10:58 AM
(This post was last modified: Dec-22-2019, 11:00 AM by Gribouillis.)
kingsman Wrote:So, how can I ensure that all the statements are extracted from the file?That's the whole problem of parsing. You need to describe the statements of the input language as accurately as possible.
If the structure of the code is that of a sequence of statements with one statement per line, you can create an expandable list of python objects containing the data read from the file. For example if the form of the storey statement is
Output:STOREY foo SPAM eggs HAM foo BAR qux
I would suggest to create a classclass Statement: def __init__(self, data): self.data = data def __repr__(self): return "{}({})".format(self.__class__.__name__, self.data) class Storey(Statement): pass statements = [] ... for line in File: data = line.split() if data[0] == 'STOREY': statements.append(Storey(data)) ...You would create such a class for every kind of statements and append instances to the
statements
list during the parsing step.At first, concentrate only on the parsing step. Once it is done, the rest will be relatively easy.