My first Python program -- a lexer

Paul McGuire ptmcg at austin.rr.com
Mon Nov 10 09:07:25 EST 2008


On Nov 10, 7:29 am, Thomas Mlynarczyk <tho... at mlynarczyk-webdesign.de>
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 keeping track of the start of every line...
>
> Yes, I was thinking of something like this. As long as the line/column
> are only needed in case of an error (i.e. at most once per script run),
> I consider this more performant than keeping track of line/column for
> every token.
>
> Greetings,
> Thomas
>
> --
> Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
> (Coluche)

Just be sure to account for tabs when computing the column, which this
simple-minded algorithm does not do.

-- Paul



More information about the Python-list mailing list