[antlr-interest] Python.g string with triple quote

Andrew Dalke dalke at dalkescientific.com
Wed Jan 2 15:41:08 PST 2008


On Jan 1, 2008, at 11:05 AM, Fõrat KŸŸk wrote:
> and also python shell behaves like that.
>
> Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
> [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> var = """hello"""
>>>> var = """"hello"""
>>>> var = """hello""""
>   File "<stdin>", line 1
>     var = """hello""""
>                      ^
> SyntaxError: EOL while scanning single-quoted string
>
>
> is there a way to fix this? or is this normal?

This is the proper (meaning, documented and expected) behavior for  
Python.

Python does string concatenation, so successive strings are joined.   
The C implementation does this at the tokenization level and the  
parser never even sees it.  For example,

   a = "Monty Python's " "Flying Circus"
becomes
   a = "Monty Python's Flying Circus"

Similarly,

   a = """My hovercraft """ "is" ''' full of eels.'''
                                 ^------------------^ triple quoted  
string
                                                      (using single  
quotes)
       ^------------------^ triple quoted string (using double quotes)
                            ^--^ single quoted string (using single  
quotes)


is identical to

   a = "My hovercraft is full of eels."

What you're seeing here is a triple quoted string followed by the  
start of a single quoted string, and hence the error message saying  
the Python lexer cannot find the close quote for a single-quoted  
string before the end of the line.


				Andrew
				dalke at dalkescientific.com

[1]  ..hovercraft.. - http://www.youtube.com/watch?v=p_ve37gVwxw



More information about the antlr-interest mailing list