<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0130)https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/201820192150306000201/Python%203.6.1%20BNF.htm -->
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>10. Full Grammar specification — Python 3.6.1 documentation</title>
<link rel="stylesheet" href="./10. Full Grammar specification — Python 3.6.1 documentation_files/pydoctheme.css" type="text/css">
<link rel="stylesheet" href="./10. Full Grammar specification — Python 3.6.1 documentation_files/pygments.css" type="text/css">
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '3.6.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="./10. Full Grammar specification — Python 3.6.1 documentation_files/jquery.js.download"></script>
<script type="text/javascript" src="./10. Full Grammar specification — Python 3.6.1 documentation_files/underscore.js.download"></script>
<script type="text/javascript" src="./10. Full Grammar specification — Python 3.6.1 documentation_files/doctools.js.download"></script>
<script type="text/javascript" src="./10. Full Grammar specification — Python 3.6.1 documentation_files/sidebar.js.download"></script>
<link rel="search" type="application/opensearchdescription+xml" title="Search within Python 3.6.1 documentation" href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/_static/opensearch.xml">
<link rel="author" title="About these documents" href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/about.html">
<link rel="copyright" title="Copyright" href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/copyright.html">
<link rel="top" title="Python 3.6.1 documentation" href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/contents.html">
<link rel="up" title="The Python Language Reference" href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/201820192150306000201/index.html">
<link rel="next" title="The Python Standard Library" href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/library/index.html">
<link rel="prev" title="9. Top-level components" href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/201820192150306000201/toplevel_components.html">
<link rel="shortcut icon" type="image/png" href="./10. Full Grammar specification — Python 3.6.1 documentation_files/py.png">
<link rel="canonical" href="https://docs.python.org/3/reference/grammar.html">
<script type="text/javascript" src="./10. Full Grammar specification — Python 3.6.1 documentation_files/copybutton.js.download"></script>
<script type="text/javascript" src="./10. Full Grammar specification — Python 3.6.1 documentation_files/version_switch.js.download"></script>
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/genindex.html" title="General Index" accesskey="I">index</a></li>
<li class="right">
<a href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/py-modindex.html" title="Python Module Index">modules</a> |</li>
<li class="right">
<a href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/library/index.html" title="The Python Standard Library" accesskey="N">next</a> |</li>
<li class="right">
<a href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/201820192150306000201/toplevel_components.html" title="9. Top-level components" accesskey="P">previous</a> |</li>
<li><img src="./10. Full Grammar specification — Python 3.6.1 documentation_files/py.png" alt="" style="vertical-align: middle; margin-top: -1px"></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="version_switcher_placeholder">3.6.1</span>
<a href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/201820192150306000201/index.html" accesskey="U">The Python Language Reference</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/search.html" method="get">
<input placeholder="Quick search" type="text" name="q">
<input type="submit" value="Go">
<input type="hidden" name="check_keywords" value="yes">
<input type="hidden" name="area" value="default">
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="full-grammar-specification">
<h1>10. Full Grammar specification<a class="headerlink" href="https://elearning.szu.edu.cn/bbcswebdav/pid-394481-dt-content-rid-2786637_1/courses/201820192150306000201/Python%203.6.1%20BNF.htm#full-grammar-specification" title="Permalink to this headline">¶</a></h1>
<p>This is the full Python grammar, as it is read by the parser generator and used
to parse Python source files:</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="c1"># Grammar for Python</span>
<span class="c1"># NOTE WELL: You should also follow all the steps listed at</span>
<span class="c1"># https://docs.python.org/devguide/grammar.html</span>
<span class="c1"># Start symbols for the grammar:</span>
<span class="c1"># single_input is a single interactive statement;</span>
<span class="c1"># file_input is a module or sequence of commands read from an input file;</span>
<span class="c1"># eval_input is the input for the eval() functions.</span>
<span class="c1"># NB: compound_stmt in single_input is followed by extra NEWLINE!</span>
<span class="n">single_input</span><span class="p">:</span> <span class="n">NEWLINE</span> <span class="o">|</span> <span class="n">simple_stmt</span> <span class="o">|</span> <span class="n">compound_stmt</span> <span class="n">NEWLINE</span>
<span class="n">file_input</span><span class="p">:</span> <span class="p">(</span><span class="n">NEWLINE</span> <span class="o">|</span> <span class="n">stmt</span><span class="p">)</span><span class="o">*</span> <span class="n">ENDMARKER</span>
<span class="n">eval_input</span><span class="p">:</span> <span class="n">testlist</span> <span class="n">NEWLINE</span><span class="o">*</span> <span class="n">ENDMARKER</span>
<span class="n">decorator</span><span class="p">:</span> <span class="s1">'@'</span> <span class="n">dotted_name</span> <span class="p">[</span> <span class="s1">'('</span> <span class="p">[</span><span class="n">arglist</span><span class="p">]</span> <span class="s1">')'</span> <span class="p">]</span> <span class="n">NEWLINE</span>
<span class="n">de