r'\' - python parser bug?

Fredrik Lundh fredrik at pythonware.com
Wed May 26 07:25:30 EDT 2004


Fuzzyman wrote:

> Wrong, surely ?

nope.

> >>> print '\\'
> \

the parser sees the first backslash, skips the second backslash,
sees the end quote, and passes everything between the quotes
to the next compiler stage.

> >>> print r'\\'
> \\

the parser sees the first backslash, skips the second backslash,
sees the end quote, and passes everything between the quotes
to the next compiler stage.

> >>> print r'c:\subdir\'
> SyntaxError: EOL while scanning single-quoted string

the parser sees the first backslash, skips the "s", and moves on.
the parser then sees the second backslash, skips the end quote,
and stumbles upon an EOL.  syntax error (grammar violation).

> > When the parser sees a backslash inside
> > a string literal, it always skips the next character.
>
> In the above example the parser *only* skips the next character if it
> is at the end of the string... surely illogical.

you're confusing the string literal syntax (which is what the parser deals
with) with the contents of the resulting string object (which is created by
a later compiler stage).  read the grammar (it's in the language reference)
and try again.

</F>







More information about the Python-list mailing list