Re: [Python-Dev] this python string literals documentation couldn't explain me: single quote presence inside double quoted string and viceversa. Can Anyone explain me?

I asked this question here, http://stackoverflow.com/questions/16435233/this-python-string-literals-docu..., . I was advised to ask here On Wed, May 8, 2013 at 4:56 PM, Alok Nayak <aloknayak29@gmail.com> wrote:
This python string literals documentation<http://docs.python.org/2/reference/lexical_analysis.html#string-literals>couldn't explain: single quote presence inside double quoted string and viceversa.
I think both double quoted string and single quoted string need to be defined differently for representing the 'stringliteral' lexical definition.
shortstringchar ::= <any source character except "\" or newline or the quote>
here in this definition 'the quote' isn't specific whether single (') or double (").
-- Alok Nayak Gwalior, India
-- Alok Nayak Gwalior, India

On 08/05/13 21:31, Alok Nayak wrote:
I asked this question here, http://stackoverflow.com/questions/16435233/this-python-string-literals-docu..., . I was advised to ask here
They were wrong. It is not relevant here, since it is not a question about development of Python. But I will answer your question anyway. The relevant parts of the documentation are: shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"' shortstringitem ::= shortstringchar | escapeseq shortstringchar ::= <any source character except "\" or newline or the quote> So let's look at a string: 'a"b' This is a shortstring, made up of single-quote followed by three shortstringitems, followed by single-quote. All three shortstring items are shortstringchar, not escapeseq: a is a source character, not including "\" or newline or single-quote " is a source character, not including "\" or newline or single-quote b is a source character, not including "\" or newline or single-quote [...]
shortstringchar ::= <any source character except "\" or newline or the quote>
here in this definition 'the quote' isn't specific whether single (') or double (").
Correct. You are expected to understand that it means either single-quote or double-quote according to context. This is documentation aimed at a human reader who should be able to use human reasoning skills to understand what "the quote" means, it is not the literal BNF grammar used by the compiler to compile Python's parser. For brevity and simplicity, some definitions may be simplified. -- Steven

Thanks for you answer sir, I was thinking its regular expressions(automata) not BNF grammer. Aint I right ? And I thought even if it is for human reading, if we write literal grammer ( regular expression, in my view) using documentation, we would end up with python not allowing strings like"python's rule" and ' python"the master" ' Here I changed the defination of string literal for explaining my thinking, inserted short-singlequoted-stringitem, short-sq-tstringchar, short-doublequoted-stringitem and short-dq-stringchar . I think we should use this in documentation stringliteral ::= [stringprefix](shortstring | longstring) stringprefix ::= "r" | "u" | "ur" | "R" | "U" | "UR" | "Ur" | "uR" | "b" | "B" | "br" | "Br" | "bR" | "BR" shortstring ::= "'" short-singlequoted-stringitem* "'" | '"' short-doublequoted-stringitem* '"' longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""' short-singlequoted-stringitem ::= short-sq-stringchar | escapeseq short-doublequoted-stringitem ::= short-dq-stringchar | escapeseq longstringitem ::= longstringchar | escapeseq short-sq-tstringchar ::= <any source character except "\" or newline or single-quote> short-dq-stringchar ::= <any source character except "\" or newline or double-quote> longstringchar ::= <any source character except "\"> escapeseq ::= "\" <any ASCII character>
participants (2)
-
Alok Nayak
-
Steven D'Aprano