New submission from Xinmeng Xia <xi...@smail.nju.edu.cn>:

In full grammar specification of Python 3.6 official documentation (Python 3.6 
official documentation: https://docs.python.org/3.6/reference/grammar.html ), 
we can find a very clear definition on the grammar about the usage of 'break'. 
According to the definition, we can find the following derivation, which 
indicates the keyword 'break' can appear in the block of if statement without 
being nested into a loop block:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Start symbols for the grammar:
#       single_input is a single interactive statement;

single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE

compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef 
| classdef | decorated | async_stmt

if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]

suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT

simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE

small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | 
global_stmt | nonlocal_stmt | assert_stmt)

flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt

break_stmt: 'break'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

However, the implementation of the Python parser requires the 'break' can only 
be embedded into a loop statement.
See the following example:

Example A(without loop):
>>> compile("if True:break",'','exec')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "", line 1
SyntaxError: 'break' outside loop

Example B(with a loop):
>>> compile("while True:\n\tif True:break",'','exec')
<code object <module> at 0x7f5f4de90b70, file "", line 1>

Similar problems exist between if-statement and keywords: 'continue', 'yield', 
'return', 'nonlocal' in Python 3.6 and later versions.

----------
assignee: docs@python
components: Documentation
messages: 380502
nosy: docs@python, xxm
priority: normal
severity: normal
status: open
title: The grammar specification is inconsistent with the implementation of 
Python parser.
type: compile error
versions: Python 3.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42284>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to