project 10, check for more

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

project 10, check for more

dmoeller
I am teaching myself python while I do this project. I was wondering how to check for more data in the file? I am making a method hasMore() that will check if jackFile has more data to be translated.
Reply | Threaded
Open this post in threaded view
|

Re: project 10, check for more

cadet1620
Administrator
I found HasMoreTokens() to be a problem, particularly handling Jack files that end with whitespace after the final "}", so I did away with it.

Instead, I have Advance() return True if it found a token and False if it encountered EOF.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: project 10, check for more

dmoeller
How does you advance work? I dont understand how to check for more data in python.
Reply | Threaded
Open this post in threaded view
|

Re: project 10, check for more

cadet1620
Administrator
dmoeller wrote
How does you advance work? I dont understand how to check for more data in python.
My tokenizer uses
    raw_line = file.readline()
to read lines out of the file, and parses tokens from the lines.  (This makes it easier to deal with string constants containing /* and // )

file.readline() returns an empty string when it is at EOF.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: project 10, check for more

dmoeller
but then you still have to be able to check if raw_line hasMore() for more info on the line. I assume this is read as a string? if so how do i separate it into individual words?
Reply | Threaded
Open this post in threaded view
|

Re: project 10, check for more

cadet1620
Administrator
After determining that raw_line is is not empty, I clean it up a bit, copy it to line and call _Parse().
        while True:
            if len(self.line) == 0:
                # Read next line.
                self.raw_line = self.file.readline()
                if len(self.raw_line) == 0:
                    return False
                # Remove trailing whitespace.
                self.raw_line = self.raw_line.rstrip()
                self.lineNumber = self.lineNumber + 1
                # Convert tabs to spaces and remove leading whitespace.
                self.line = self.raw_line.replace('\t', ' ').strip()

            # Tokens are removed from self.line as they are parsed.
            self._Parse()
            if self.tokenType == None:  # This happens if a line ends in a comment.
                continue
            return True
Parse() parses the next token on self.line and removes the token from self.line. When self.line becomes empty, it is time to read a new line from the file.

(Note that the two-stage line reading is so that I can print the entire source line in an error message if there is a parsing problem midway through the line.)

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: project 10, check for more

dmoeller
so I could use a variation of your code, but could i use this for my while?

while self.line != '':

(those are two ' not a ")
Reply | Threaded
Open this post in threaded view
|

Re: project 10, check for more

dmoeller
nevermind, I see the logic of the while loop now.

I have another question though. Since im still kinda new to python and I don't understand self. how do i pass self.line to another function? like when you call self.Parse(), how does Parse() take self.line?
Reply | Threaded
Open this post in threaded view
|

Re: project 10, check for more

cadet1620
Administrator
In reply to this post by dmoeller
Yes, assuming line was initialized with something:

    line = None
    while line != '':
        ...

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: project 10, check for more

cadet1620
Administrator
In reply to this post by dmoeller
dmoeller wrote
I have another question though. Since im still kinda new to python and I don't understand self. how do i pass self.line to another function? like when you call self.Parse(), how does Parse() take self.line?
"self" is Python's version of C++/Java's "this".  

_Parse is another member function of Tokenizer:
    def _Parse(self):

When Advance() calls self._Parse(), _Parse() automatically gets the same "self" as Advance() had.  _Parse() is changing the Tokenizer's instance variable when it changes self.line

(The leading "_" in the function name indicates that the function is intended to be private, which is not a supported concept in Python.)

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: project 10, check for more

dmoeller
my Parse() function I names writeTokens(). So if i cal self.writeTokens, it will automatically pass it the raw_line and whenever i type self in writeTokens, i.e self.tokenType, then it is the same as raw_line.tokenType?