Re: My first Python program -- a lexer

2008-11-24 Thread Arnaud Delobelle
Thomas Mlynarczyk <[EMAIL PROTECTED]> writes: > John Machin schrieb: > >> *IF* you need to access the regex associated with a token in O(1) >> time, a dict is indicated. > > O(1) - Does that mean `mydict[mykey]` takes the same amount of time, > no matter if mydict has 10 or 10 entries? How

Re: My first Python program -- a lexer

2008-11-24 Thread Steve Holden
Thomas Mlynarczyk wrote: > John Machin schrieb: > >> *IF* you need to access the regex associated with a token in O(1) >> time, a dict is indicated. > > O(1) - Does that mean `mydict[mykey]` takes the same amount of time, no > matter if mydict has 10 or 10 entries? How does this magic wor

Re: My first Python program -- a lexer

2008-11-24 Thread Thomas Mlynarczyk
John Machin schrieb: *IF* you need to access the regex associated with a token in O(1) time, a dict is indicated. O(1) - Does that mean `mydict[mykey]` takes the same amount of time, no matter if mydict has 10 or 10 entries? How does this magic work? O(log n) I would understand, but

Re: My first Python program -- a lexer

2008-11-23 Thread John Machin
On Nov 12, 2:54 am, Thomas Mlynarczyk <[EMAIL PROTECTED]> wrote: > John Machin schrieb: > > > You are getting closer. A better analogy is that using a dict is like > > transporting passengers along an autobahn in an aeroplane or > > helicopter that never leaves the ground. > > It is not a bad idea

Re: My first Python program -- a lexer

2008-11-12 Thread Thomas Mlynarczyk
Steve Holden schrieb: Suppose I use the dict and I want to access the regex associatetd with the token named "tokenname" (that is, no iteration, but a single access). I could simple write tokendict["tokenname"]. But with the list of tuples, I can't think of an equally easy way to do that. But th

Re: My first Python program -- a lexer

2008-11-11 Thread Steve Holden
Thomas Mlynarczyk wrote: > John Machin schrieb: > >> You are getting closer. A better analogy is that using a dict is like >> transporting passengers along an autobahn in an aeroplane or >> helicopter that never leaves the ground. > > It is not a bad idea to transport passengers in an airplane, b

Re: My first Python program -- a lexer

2008-11-11 Thread Thomas Mlynarczyk
John Machin schrieb: You are getting closer. A better analogy is that using a dict is like transporting passengers along an autobahn in an aeroplane or helicopter that never leaves the ground. It is not a bad idea to transport passengers in an airplane, but then the airplane should not follow

Re: My first Python program -- a lexer

2008-11-10 Thread John Machin
On Nov 11, 8:35 am, Thomas Mlynarczyk <[EMAIL PROTECTED]> wrote: > [Using dict] > > > No, not at all. The point is that you were not *using* any of the > > mapping functionality of the dict object, only ancillary methods like > > iteritems -- hence, you should not have been using a dict at all. >

Re: My first Python program -- a lexer

2008-11-10 Thread Thomas Mlynarczyk
John Machin schrieb: Single-character tokens like "<" may be more efficiently handled by doing a dict lookup after failing to find a match in the list of (name, regex) tuples. Yes, I will keep that in mind. For the time being, I will use only regexes to keep the code simpler. Later, or when t

Re: My first Python program -- a lexer

2008-11-10 Thread John Machin
On Nov 11, 12:26 am, Thomas Mlynarczyk <[EMAIL PROTECTED] webdesign.de> wrote: > John Machin schrieb: > > >> On the other hand: If all my tokens are "mutually exclusive" then, > > But they won't *always* be mutually exclusive (another example is > > relational operators (< vs <=, > vs >=)) and AFAI

Re: My first Python program -- a lexer

2008-11-10 Thread Thomas Mlynarczyk
Paul McGuire schrieb: Just be sure to account for tabs when computing the column, which this simple-minded algorithm does not do. Another thing I had not thought of -- thanks for the hint. Greetings, Thomas -- Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison! (Coluche)

Re: My first Python program -- a lexer

2008-11-10 Thread Steve Holden
Some pratt wrote: > BLAST YOUR AD [...] and curse yours -- http://mail.python.org/mailman/listinfo/python-list

Re: My first Python program -- a lexer

2008-11-10 Thread Paul McGuire
On Nov 10, 7:29 am, Thomas Mlynarczyk <[EMAIL PROTECTED]> wrote: > Paul McGuire schrieb: > > > loc = data.index("list") > > print data[:loc].count("\n")-1 > > print loc-data[:loc].rindex("\n")-1 > > > prints 5,14 > > > I'm sure it's non-optimal, but it *is* an algorithm that does not > > require ke

Re: My first Python program -- a lexer

2008-11-10 Thread Thomas Mlynarczyk
Paul McGuire schrieb: loc = data.index("list") print data[:loc].count("\n")-1 print loc-data[:loc].rindex("\n")-1 prints 5,14 I'm sure it's non-optimal, but it *is* an algorithm that does not require keeping track of the start of every line... Yes, I was thinking of something like this. As l

Re: My first Python program -- a lexer

2008-11-10 Thread Thomas Mlynarczyk
John Machin schrieb: On the other hand: If all my tokens are "mutually exclusive" then, But they won't *always* be mutually exclusive (another example is relational operators (< vs <=, > vs >=)) and AFAICT there is nothing useful that the lexer can do with an assumption/guess/input that they

Re: My first Python program -- a lexer

2008-11-10 Thread Thomas Mlynarczyk
Robert Lehmann schrieb: You don't have to introduce a `next` method to your Lexer class. You could just transform your `tokenize` method into a generator by replacing ``self.result.append`` with `yield`. It gives you the just in time part for free while not picking your algorithm into tiny unr

Re: My first Python program -- a lexer

2008-11-10 Thread Robert Lehmann
On Sun, 09 Nov 2008 15:53:01 +0100, Thomas Mlynarczyk wrote: > Arnaud Delobelle schrieb: > >> Adding to John's comments, I wouldn't have source as a member of the >> Lexer object but as an argument of the tokenise() method (which I would >> make public). The tokenise method would return what you

Re: My first Python program -- a lexer

2008-11-09 Thread Paul McGuire
On Nov 9, 8:34 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Sun, 09 Nov 2008 23:33:30 +0100, Thomas Mlynarczyk > <[EMAIL PROTECTED]> declaimed the following in > comp.lang.python: > > > > > Of course. For the actual message I would use at least the line number. > > Still, the offset could

Re: My first Python program -- a lexer

2008-11-09 Thread John Machin
On Nov 10, 9:33 am, Thomas Mlynarczyk <[EMAIL PROTECTED]> wrote: > John Machin schrieb: > > >>> dict.iter() will return its results in essentially random > >>> order. > > A list of somethings does seem indicated. > > On the other hand: If all my tokens are "mutually exclusive" then, But they won't

Re: My first Python program -- a lexer

2008-11-09 Thread Thomas Mlynarczyk
John Machin schrieb: [...] You have TWO problems: (1) Reporting the error location as (offset from the start of the file) instead of (line number, column position) would get you an express induction into the User Interface Hall of Shame. Of course. For the actual message I would use at least

Re: My first Python program -- a lexer

2008-11-09 Thread John Machin
On Nov 10, 1:39 am, Thomas Mlynarczyk <[EMAIL PROTECTED]> wrote: > John Machin schrieb: > > > Be consistent with your punctuation style. I'd suggest *not* having a > > space after ( and before ), as in the previous line. Read > >http://www.python.org/dev/peps/pep-0008/ > > What were the reasons for

Re: My first Python program -- a lexer

2008-11-09 Thread Thomas Mlynarczyk
Arnaud Delobelle schrieb: Adding to John's comments, I wouldn't have source as a member of the Lexer object but as an argument of the tokenise() method (which I would make public). The tokenise method would return what you currently call self.result. So it would be used like this. mylexer =

Re: My first Python program -- a lexer

2008-11-09 Thread Thomas Mlynarczyk
John Machin schrieb: Be consistent with your punctuation style. I'd suggest *not* having a space after ( and before ), as in the previous line. Read http://www.python.org/dev/peps/pep-0008/ What were the reasons for preferring (foo) over ( foo )? This PEP gives recommendations for coding styl

Re: My first Python program -- a lexer

2008-11-08 Thread Arnaud Delobelle
Thomas Mlynarczyk <[EMAIL PROTECTED]> writes: > Hello, > > I started to write a lexer in Python -- my first attempt to do > something useful with Python (rather than trying out snippets from > tutorials). It is not complete yet, but I would like some feedback -- > I'm a Python newbie and it seems

Re: My first Python program -- a lexer

2008-11-08 Thread John Machin
On Nov 9, 7:55 am, Thomas Mlynarczyk <[EMAIL PROTECTED]> wrote: > Hello, > > I started to write a lexer in Python -- my first attempt to do something > useful with Python (rather than trying out snippets from tutorials). It > is not complete yet, but I would like some feedback -- I'm a Python > new

My first Python program -- a lexer

2008-11-08 Thread Thomas Mlynarczyk
Hello, I started to write a lexer in Python -- my first attempt to do something useful with Python (rather than trying out snippets from tutorials). It is not complete yet, but I would like some feedback -- I'm a Python newbie and it seems that, with Python, there is always a simpler and bett