A simple single line, triple-quoted comment is giving syntax error. Why?

Marko Rauhamaa marko at pacujo.net
Fri Apr 3 03:13:11 EDT 2015


Chris Angelico <rosuav at gmail.com>:

> I know that it started in response to my statement that string literal
> concatenation wasn't an expression as such, but I have no idea what
> either side of the current debate is, nor how it affects my
> statement's validity.

This is what I have gathered:

 - A Python expression cannot directly follow another expression. A
   connector is required by the syntax.

 - That's untrue:
    * "abc"  is an expression
    * "def"  is an expression
    * "abc" "def"  is an expression
   QED

 - Merging "abc" "def" is a matter of lexical analysis.

 - No, it's right there in the syntax definition.

 - Well, ok, however, the parse tree for "abc" "def" is not

   expr:
     expr:
       atom:
         string: "abc"
     expr:
       atom:
         string: "def"

   Rather, the correct parse tree is:

   expr:
     expr:
       atom:
         string: "abc"
         string: "def"

   Thus, a Python expression still is not directly following another
   expression.


My own take: all sides are correct. Thomas is most correct and is being
obnoxious about it.

 1. A lexical analyzer *could* take care of concatenating string
    literals. It would have to be smart enough to handle intervening
    comments. On the other hand, the lexical analyzer is just a part of
    the parser; the line between them often is blurred.

 2. The counterexample "abc" "def" *does* demonstrate that expressions
    can at times follow each other immediately. It is a nice point even
    if not all that consequential.

    Somewhat analogously:
     * ord  is an expression
     * ("a")  is an expression
     * ord("a")  is an expression

 3. Arguing about definitions is silly. Is 0 a natural number? Is 1 a
    prime number?


Marko



More information about the Python-list mailing list