My first Python program -- a lexer

Paul McGuire ptmcg at austin.rr.com
Mon Nov 10 05:25:24 CET 2008


On Nov 9, 8:34 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Sun, 09 Nov 2008 23:33:30 +0100, Thomas Mlynarczyk
> <tho... at mlynarczyk-webdesign.de> 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 be used to compute line/column in case of an
> > error, so I wouldn't really need to store line/column with each token,
> > but only the offset. And provide a method to "convert" offset values
> > into line/column tuples.
>
>         Are you forcing the use of fixed length lines then?
>
>         Otherwise, by what algorithm will you convert:
>
> >>> data = """
>
> ... one
> ... two
> ... three
> ... four
> ... five
> ... supercalifragilisticexpialidocious
> ... seven
> ... eight
> ... nine""">>> ix = data.index("list")
> >>> ix
> 39

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...

-- Paul



More information about the Python-list mailing list