335 lines
40 KiB
HTML
335 lines
40 KiB
HTML
|
|
<!DOCTYPE html>
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<title>10. Full Grammar specification — Python 3.7.4 documentation</title>
|
|
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
|
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
|
|
|
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
|
<script type="text/javascript" src="../_static/jquery.js"></script>
|
|
<script type="text/javascript" src="../_static/underscore.js"></script>
|
|
<script type="text/javascript" src="../_static/doctools.js"></script>
|
|
<script type="text/javascript" src="../_static/language_data.js"></script>
|
|
|
|
<script type="text/javascript" src="../_static/sidebar.js"></script>
|
|
|
|
<link rel="search" type="application/opensearchdescription+xml"
|
|
title="Search within Python 3.7.4 documentation"
|
|
href="../_static/opensearch.xml"/>
|
|
<link rel="author" title="About these documents" href="../about.html" />
|
|
<link rel="index" title="Index" href="../genindex.html" />
|
|
<link rel="search" title="Search" href="../search.html" />
|
|
<link rel="copyright" title="Copyright" href="../copyright.html" />
|
|
<link rel="next" title="The Python Standard Library" href="../library/index.html" />
|
|
<link rel="prev" title="9. Top-level components" href="toplevel_components.html" />
|
|
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
|
<link rel="canonical" href="https://docs.python.org/3/reference/grammar.html" />
|
|
|
|
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
|
<script type="text/javascript" src="../_static/switchers.js"></script>
|
|
|
|
|
|
|
|
<style>
|
|
@media only screen {
|
|
table.full-width-table {
|
|
width: 100%;
|
|
}
|
|
}
|
|
</style>
|
|
|
|
|
|
</head><body>
|
|
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="right" >
|
|
<a href="../library/index.html" title="The Python Standard Library"
|
|
accesskey="N">next</a> |</li>
|
|
<li class="right" >
|
|
<a href="toplevel_components.html" title="9. Top-level components"
|
|
accesskey="P">previous</a> |</li>
|
|
<li><img src="../_static/py.png" alt=""
|
|
style="vertical-align: middle; margin-top: -1px"/></li>
|
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|
<li>
|
|
<span class="language_switcher_placeholder">en</span>
|
|
<span class="version_switcher_placeholder">3.7.4</span>
|
|
<a href="../index.html">Documentation </a> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="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="../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="#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 notranslate"><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://devguide.python.org/grammar/</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">decorators</span><span class="p">:</span> <span class="n">decorator</span><span class="o">+</span>
|
|
<span class="n">decorated</span><span class="p">:</span> <span class="n">decorators</span> <span class="p">(</span><span class="n">classdef</span> <span class="o">|</span> <span class="n">funcdef</span> <span class="o">|</span> <span class="n">async_funcdef</span><span class="p">)</span>
|
|
|
|
<span class="n">async_funcdef</span><span class="p">:</span> <span class="s1">'async'</span> <span class="n">funcdef</span>
|
|
<span class="n">funcdef</span><span class="p">:</span> <span class="s1">'def'</span> <span class="n">NAME</span> <span class="n">parameters</span> <span class="p">[</span><span class="s1">'->'</span> <span class="n">test</span><span class="p">]</span> <span class="s1">':'</span> <span class="n">suite</span>
|
|
|
|
<span class="n">parameters</span><span class="p">:</span> <span class="s1">'('</span> <span class="p">[</span><span class="n">typedargslist</span><span class="p">]</span> <span class="s1">')'</span>
|
|
<span class="n">typedargslist</span><span class="p">:</span> <span class="p">(</span><span class="n">tfpdef</span> <span class="p">[</span><span class="s1">'='</span> <span class="n">test</span><span class="p">]</span> <span class="p">(</span><span class="s1">','</span> <span class="n">tfpdef</span> <span class="p">[</span><span class="s1">'='</span> <span class="n">test</span><span class="p">])</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span> <span class="p">[</span>
|
|
<span class="s1">'*'</span> <span class="p">[</span><span class="n">tfpdef</span><span class="p">]</span> <span class="p">(</span><span class="s1">','</span> <span class="n">tfpdef</span> <span class="p">[</span><span class="s1">'='</span> <span class="n">test</span><span class="p">])</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span> <span class="p">[</span><span class="s1">'**'</span> <span class="n">tfpdef</span> <span class="p">[</span><span class="s1">','</span><span class="p">]]]</span>
|
|
<span class="o">|</span> <span class="s1">'**'</span> <span class="n">tfpdef</span> <span class="p">[</span><span class="s1">','</span><span class="p">]]]</span>
|
|
<span class="o">|</span> <span class="s1">'*'</span> <span class="p">[</span><span class="n">tfpdef</span><span class="p">]</span> <span class="p">(</span><span class="s1">','</span> <span class="n">tfpdef</span> <span class="p">[</span><span class="s1">'='</span> <span class="n">test</span><span class="p">])</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span> <span class="p">[</span><span class="s1">'**'</span> <span class="n">tfpdef</span> <span class="p">[</span><span class="s1">','</span><span class="p">]]]</span>
|
|
<span class="o">|</span> <span class="s1">'**'</span> <span class="n">tfpdef</span> <span class="p">[</span><span class="s1">','</span><span class="p">])</span>
|
|
<span class="n">tfpdef</span><span class="p">:</span> <span class="n">NAME</span> <span class="p">[</span><span class="s1">':'</span> <span class="n">test</span><span class="p">]</span>
|
|
<span class="n">varargslist</span><span class="p">:</span> <span class="p">(</span><span class="n">vfpdef</span> <span class="p">[</span><span class="s1">'='</span> <span class="n">test</span><span class="p">]</span> <span class="p">(</span><span class="s1">','</span> <span class="n">vfpdef</span> <span class="p">[</span><span class="s1">'='</span> <span class="n">test</span><span class="p">])</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span> <span class="p">[</span>
|
|
<span class="s1">'*'</span> <span class="p">[</span><span class="n">vfpdef</span><span class="p">]</span> <span class="p">(</span><span class="s1">','</span> <span class="n">vfpdef</span> <span class="p">[</span><span class="s1">'='</span> <span class="n">test</span><span class="p">])</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span> <span class="p">[</span><span class="s1">'**'</span> <span class="n">vfpdef</span> <span class="p">[</span><span class="s1">','</span><span class="p">]]]</span>
|
|
<span class="o">|</span> <span class="s1">'**'</span> <span class="n">vfpdef</span> <span class="p">[</span><span class="s1">','</span><span class="p">]]]</span>
|
|
<span class="o">|</span> <span class="s1">'*'</span> <span class="p">[</span><span class="n">vfpdef</span><span class="p">]</span> <span class="p">(</span><span class="s1">','</span> <span class="n">vfpdef</span> <span class="p">[</span><span class="s1">'='</span> <span class="n">test</span><span class="p">])</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span> <span class="p">[</span><span class="s1">'**'</span> <span class="n">vfpdef</span> <span class="p">[</span><span class="s1">','</span><span class="p">]]]</span>
|
|
<span class="o">|</span> <span class="s1">'**'</span> <span class="n">vfpdef</span> <span class="p">[</span><span class="s1">','</span><span class="p">]</span>
|
|
<span class="p">)</span>
|
|
<span class="n">vfpdef</span><span class="p">:</span> <span class="n">NAME</span>
|
|
|
|
<span class="n">stmt</span><span class="p">:</span> <span class="n">simple_stmt</span> <span class="o">|</span> <span class="n">compound_stmt</span>
|
|
<span class="n">simple_stmt</span><span class="p">:</span> <span class="n">small_stmt</span> <span class="p">(</span><span class="s1">';'</span> <span class="n">small_stmt</span><span class="p">)</span><span class="o">*</span> <span class="p">[</span><span class="s1">';'</span><span class="p">]</span> <span class="n">NEWLINE</span>
|
|
<span class="n">small_stmt</span><span class="p">:</span> <span class="p">(</span><span class="n">expr_stmt</span> <span class="o">|</span> <span class="n">del_stmt</span> <span class="o">|</span> <span class="n">pass_stmt</span> <span class="o">|</span> <span class="n">flow_stmt</span> <span class="o">|</span>
|
|
<span class="n">import_stmt</span> <span class="o">|</span> <span class="n">global_stmt</span> <span class="o">|</span> <span class="n">nonlocal_stmt</span> <span class="o">|</span> <span class="n">assert_stmt</span><span class="p">)</span>
|
|
<span class="n">expr_stmt</span><span class="p">:</span> <span class="n">testlist_star_expr</span> <span class="p">(</span><span class="n">annassign</span> <span class="o">|</span> <span class="n">augassign</span> <span class="p">(</span><span class="n">yield_expr</span><span class="o">|</span><span class="n">testlist</span><span class="p">)</span> <span class="o">|</span>
|
|
<span class="p">(</span><span class="s1">'='</span> <span class="p">(</span><span class="n">yield_expr</span><span class="o">|</span><span class="n">testlist_star_expr</span><span class="p">))</span><span class="o">*</span><span class="p">)</span>
|
|
<span class="n">annassign</span><span class="p">:</span> <span class="s1">':'</span> <span class="n">test</span> <span class="p">[</span><span class="s1">'='</span> <span class="n">test</span><span class="p">]</span>
|
|
<span class="n">testlist_star_expr</span><span class="p">:</span> <span class="p">(</span><span class="n">test</span><span class="o">|</span><span class="n">star_expr</span><span class="p">)</span> <span class="p">(</span><span class="s1">','</span> <span class="p">(</span><span class="n">test</span><span class="o">|</span><span class="n">star_expr</span><span class="p">))</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span><span class="p">]</span>
|
|
<span class="n">augassign</span><span class="p">:</span> <span class="p">(</span><span class="s1">'+='</span> <span class="o">|</span> <span class="s1">'-='</span> <span class="o">|</span> <span class="s1">'*='</span> <span class="o">|</span> <span class="s1">'@='</span> <span class="o">|</span> <span class="s1">'/='</span> <span class="o">|</span> <span class="s1">'%='</span> <span class="o">|</span> <span class="s1">'&='</span> <span class="o">|</span> <span class="s1">'|='</span> <span class="o">|</span> <span class="s1">'^='</span> <span class="o">|</span>
|
|
<span class="s1">'<<='</span> <span class="o">|</span> <span class="s1">'>>='</span> <span class="o">|</span> <span class="s1">'**='</span> <span class="o">|</span> <span class="s1">'//='</span><span class="p">)</span>
|
|
<span class="c1"># For normal and annotated assignments, additional restrictions enforced by the interpreter</span>
|
|
<span class="n">del_stmt</span><span class="p">:</span> <span class="s1">'del'</span> <span class="n">exprlist</span>
|
|
<span class="n">pass_stmt</span><span class="p">:</span> <span class="s1">'pass'</span>
|
|
<span class="n">flow_stmt</span><span class="p">:</span> <span class="n">break_stmt</span> <span class="o">|</span> <span class="n">continue_stmt</span> <span class="o">|</span> <span class="n">return_stmt</span> <span class="o">|</span> <span class="n">raise_stmt</span> <span class="o">|</span> <span class="n">yield_stmt</span>
|
|
<span class="n">break_stmt</span><span class="p">:</span> <span class="s1">'break'</span>
|
|
<span class="n">continue_stmt</span><span class="p">:</span> <span class="s1">'continue'</span>
|
|
<span class="n">return_stmt</span><span class="p">:</span> <span class="s1">'return'</span> <span class="p">[</span><span class="n">testlist</span><span class="p">]</span>
|
|
<span class="n">yield_stmt</span><span class="p">:</span> <span class="n">yield_expr</span>
|
|
<span class="n">raise_stmt</span><span class="p">:</span> <span class="s1">'raise'</span> <span class="p">[</span><span class="n">test</span> <span class="p">[</span><span class="s1">'from'</span> <span class="n">test</span><span class="p">]]</span>
|
|
<span class="n">import_stmt</span><span class="p">:</span> <span class="n">import_name</span> <span class="o">|</span> <span class="n">import_from</span>
|
|
<span class="n">import_name</span><span class="p">:</span> <span class="s1">'import'</span> <span class="n">dotted_as_names</span>
|
|
<span class="c1"># note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS</span>
|
|
<span class="n">import_from</span><span class="p">:</span> <span class="p">(</span><span class="s1">'from'</span> <span class="p">((</span><span class="s1">'.'</span> <span class="o">|</span> <span class="s1">'...'</span><span class="p">)</span><span class="o">*</span> <span class="n">dotted_name</span> <span class="o">|</span> <span class="p">(</span><span class="s1">'.'</span> <span class="o">|</span> <span class="s1">'...'</span><span class="p">)</span><span class="o">+</span><span class="p">)</span>
|
|
<span class="s1">'import'</span> <span class="p">(</span><span class="s1">'*'</span> <span class="o">|</span> <span class="s1">'('</span> <span class="n">import_as_names</span> <span class="s1">')'</span> <span class="o">|</span> <span class="n">import_as_names</span><span class="p">))</span>
|
|
<span class="n">import_as_name</span><span class="p">:</span> <span class="n">NAME</span> <span class="p">[</span><span class="s1">'as'</span> <span class="n">NAME</span><span class="p">]</span>
|
|
<span class="n">dotted_as_name</span><span class="p">:</span> <span class="n">dotted_name</span> <span class="p">[</span><span class="s1">'as'</span> <span class="n">NAME</span><span class="p">]</span>
|
|
<span class="n">import_as_names</span><span class="p">:</span> <span class="n">import_as_name</span> <span class="p">(</span><span class="s1">','</span> <span class="n">import_as_name</span><span class="p">)</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span><span class="p">]</span>
|
|
<span class="n">dotted_as_names</span><span class="p">:</span> <span class="n">dotted_as_name</span> <span class="p">(</span><span class="s1">','</span> <span class="n">dotted_as_name</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">dotted_name</span><span class="p">:</span> <span class="n">NAME</span> <span class="p">(</span><span class="s1">'.'</span> <span class="n">NAME</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">global_stmt</span><span class="p">:</span> <span class="s1">'global'</span> <span class="n">NAME</span> <span class="p">(</span><span class="s1">','</span> <span class="n">NAME</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">nonlocal_stmt</span><span class="p">:</span> <span class="s1">'nonlocal'</span> <span class="n">NAME</span> <span class="p">(</span><span class="s1">','</span> <span class="n">NAME</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">assert_stmt</span><span class="p">:</span> <span class="s1">'assert'</span> <span class="n">test</span> <span class="p">[</span><span class="s1">','</span> <span class="n">test</span><span class="p">]</span>
|
|
|
|
<span class="n">compound_stmt</span><span class="p">:</span> <span class="n">if_stmt</span> <span class="o">|</span> <span class="n">while_stmt</span> <span class="o">|</span> <span class="n">for_stmt</span> <span class="o">|</span> <span class="n">try_stmt</span> <span class="o">|</span> <span class="n">with_stmt</span> <span class="o">|</span> <span class="n">funcdef</span> <span class="o">|</span> <span class="n">classdef</span> <span class="o">|</span> <span class="n">decorated</span> <span class="o">|</span> <span class="n">async_stmt</span>
|
|
<span class="n">async_stmt</span><span class="p">:</span> <span class="s1">'async'</span> <span class="p">(</span><span class="n">funcdef</span> <span class="o">|</span> <span class="n">with_stmt</span> <span class="o">|</span> <span class="n">for_stmt</span><span class="p">)</span>
|
|
<span class="n">if_stmt</span><span class="p">:</span> <span class="s1">'if'</span> <span class="n">test</span> <span class="s1">':'</span> <span class="n">suite</span> <span class="p">(</span><span class="s1">'elif'</span> <span class="n">test</span> <span class="s1">':'</span> <span class="n">suite</span><span class="p">)</span><span class="o">*</span> <span class="p">[</span><span class="s1">'else'</span> <span class="s1">':'</span> <span class="n">suite</span><span class="p">]</span>
|
|
<span class="n">while_stmt</span><span class="p">:</span> <span class="s1">'while'</span> <span class="n">test</span> <span class="s1">':'</span> <span class="n">suite</span> <span class="p">[</span><span class="s1">'else'</span> <span class="s1">':'</span> <span class="n">suite</span><span class="p">]</span>
|
|
<span class="n">for_stmt</span><span class="p">:</span> <span class="s1">'for'</span> <span class="n">exprlist</span> <span class="s1">'in'</span> <span class="n">testlist</span> <span class="s1">':'</span> <span class="n">suite</span> <span class="p">[</span><span class="s1">'else'</span> <span class="s1">':'</span> <span class="n">suite</span><span class="p">]</span>
|
|
<span class="n">try_stmt</span><span class="p">:</span> <span class="p">(</span><span class="s1">'try'</span> <span class="s1">':'</span> <span class="n">suite</span>
|
|
<span class="p">((</span><span class="n">except_clause</span> <span class="s1">':'</span> <span class="n">suite</span><span class="p">)</span><span class="o">+</span>
|
|
<span class="p">[</span><span class="s1">'else'</span> <span class="s1">':'</span> <span class="n">suite</span><span class="p">]</span>
|
|
<span class="p">[</span><span class="s1">'finally'</span> <span class="s1">':'</span> <span class="n">suite</span><span class="p">]</span> <span class="o">|</span>
|
|
<span class="s1">'finally'</span> <span class="s1">':'</span> <span class="n">suite</span><span class="p">))</span>
|
|
<span class="n">with_stmt</span><span class="p">:</span> <span class="s1">'with'</span> <span class="n">with_item</span> <span class="p">(</span><span class="s1">','</span> <span class="n">with_item</span><span class="p">)</span><span class="o">*</span> <span class="s1">':'</span> <span class="n">suite</span>
|
|
<span class="n">with_item</span><span class="p">:</span> <span class="n">test</span> <span class="p">[</span><span class="s1">'as'</span> <span class="n">expr</span><span class="p">]</span>
|
|
<span class="c1"># NB compile.c makes sure that the default except clause is last</span>
|
|
<span class="n">except_clause</span><span class="p">:</span> <span class="s1">'except'</span> <span class="p">[</span><span class="n">test</span> <span class="p">[</span><span class="s1">'as'</span> <span class="n">NAME</span><span class="p">]]</span>
|
|
<span class="n">suite</span><span class="p">:</span> <span class="n">simple_stmt</span> <span class="o">|</span> <span class="n">NEWLINE</span> <span class="n">INDENT</span> <span class="n">stmt</span><span class="o">+</span> <span class="n">DEDENT</span>
|
|
|
|
<span class="n">test</span><span class="p">:</span> <span class="n">or_test</span> <span class="p">[</span><span class="s1">'if'</span> <span class="n">or_test</span> <span class="s1">'else'</span> <span class="n">test</span><span class="p">]</span> <span class="o">|</span> <span class="n">lambdef</span>
|
|
<span class="n">test_nocond</span><span class="p">:</span> <span class="n">or_test</span> <span class="o">|</span> <span class="n">lambdef_nocond</span>
|
|
<span class="n">lambdef</span><span class="p">:</span> <span class="s1">'lambda'</span> <span class="p">[</span><span class="n">varargslist</span><span class="p">]</span> <span class="s1">':'</span> <span class="n">test</span>
|
|
<span class="n">lambdef_nocond</span><span class="p">:</span> <span class="s1">'lambda'</span> <span class="p">[</span><span class="n">varargslist</span><span class="p">]</span> <span class="s1">':'</span> <span class="n">test_nocond</span>
|
|
<span class="n">or_test</span><span class="p">:</span> <span class="n">and_test</span> <span class="p">(</span><span class="s1">'or'</span> <span class="n">and_test</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">and_test</span><span class="p">:</span> <span class="n">not_test</span> <span class="p">(</span><span class="s1">'and'</span> <span class="n">not_test</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">not_test</span><span class="p">:</span> <span class="s1">'not'</span> <span class="n">not_test</span> <span class="o">|</span> <span class="n">comparison</span>
|
|
<span class="n">comparison</span><span class="p">:</span> <span class="n">expr</span> <span class="p">(</span><span class="n">comp_op</span> <span class="n">expr</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="c1"># <> isn't actually a valid comparison operator in Python. It's here for the</span>
|
|
<span class="c1"># sake of a __future__ import described in PEP 401 (which really works :-)</span>
|
|
<span class="n">comp_op</span><span class="p">:</span> <span class="s1">'<'</span><span class="o">|</span><span class="s1">'>'</span><span class="o">|</span><span class="s1">'=='</span><span class="o">|</span><span class="s1">'>='</span><span class="o">|</span><span class="s1">'<='</span><span class="o">|</span><span class="s1">'<>'</span><span class="o">|</span><span class="s1">'!='</span><span class="o">|</span><span class="s1">'in'</span><span class="o">|</span><span class="s1">'not'</span> <span class="s1">'in'</span><span class="o">|</span><span class="s1">'is'</span><span class="o">|</span><span class="s1">'is'</span> <span class="s1">'not'</span>
|
|
<span class="n">star_expr</span><span class="p">:</span> <span class="s1">'*'</span> <span class="n">expr</span>
|
|
<span class="n">expr</span><span class="p">:</span> <span class="n">xor_expr</span> <span class="p">(</span><span class="s1">'|'</span> <span class="n">xor_expr</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">xor_expr</span><span class="p">:</span> <span class="n">and_expr</span> <span class="p">(</span><span class="s1">'^'</span> <span class="n">and_expr</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">and_expr</span><span class="p">:</span> <span class="n">shift_expr</span> <span class="p">(</span><span class="s1">'&'</span> <span class="n">shift_expr</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">shift_expr</span><span class="p">:</span> <span class="n">arith_expr</span> <span class="p">((</span><span class="s1">'<<'</span><span class="o">|</span><span class="s1">'>>'</span><span class="p">)</span> <span class="n">arith_expr</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">arith_expr</span><span class="p">:</span> <span class="n">term</span> <span class="p">((</span><span class="s1">'+'</span><span class="o">|</span><span class="s1">'-'</span><span class="p">)</span> <span class="n">term</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">term</span><span class="p">:</span> <span class="n">factor</span> <span class="p">((</span><span class="s1">'*'</span><span class="o">|</span><span class="s1">'@'</span><span class="o">|</span><span class="s1">'/'</span><span class="o">|</span><span class="s1">'%'</span><span class="o">|</span><span class="s1">'//'</span><span class="p">)</span> <span class="n">factor</span><span class="p">)</span><span class="o">*</span>
|
|
<span class="n">factor</span><span class="p">:</span> <span class="p">(</span><span class="s1">'+'</span><span class="o">|</span><span class="s1">'-'</span><span class="o">|</span><span class="s1">'~'</span><span class="p">)</span> <span class="n">factor</span> <span class="o">|</span> <span class="n">power</span>
|
|
<span class="n">power</span><span class="p">:</span> <span class="n">atom_expr</span> <span class="p">[</span><span class="s1">'**'</span> <span class="n">factor</span><span class="p">]</span>
|
|
<span class="n">atom_expr</span><span class="p">:</span> <span class="p">[</span><span class="s1">'await'</span><span class="p">]</span> <span class="n">atom</span> <span class="n">trailer</span><span class="o">*</span>
|
|
<span class="n">atom</span><span class="p">:</span> <span class="p">(</span><span class="s1">'('</span> <span class="p">[</span><span class="n">yield_expr</span><span class="o">|</span><span class="n">testlist_comp</span><span class="p">]</span> <span class="s1">')'</span> <span class="o">|</span>
|
|
<span class="s1">'['</span> <span class="p">[</span><span class="n">testlist_comp</span><span class="p">]</span> <span class="s1">']'</span> <span class="o">|</span>
|
|
<span class="s1">'{'</span> <span class="p">[</span><span class="n">dictorsetmaker</span><span class="p">]</span> <span class="s1">'}'</span> <span class="o">|</span>
|
|
<span class="n">NAME</span> <span class="o">|</span> <span class="n">NUMBER</span> <span class="o">|</span> <span class="n">STRING</span><span class="o">+</span> <span class="o">|</span> <span class="s1">'...'</span> <span class="o">|</span> <span class="s1">'None'</span> <span class="o">|</span> <span class="s1">'True'</span> <span class="o">|</span> <span class="s1">'False'</span><span class="p">)</span>
|
|
<span class="n">testlist_comp</span><span class="p">:</span> <span class="p">(</span><span class="n">test</span><span class="o">|</span><span class="n">star_expr</span><span class="p">)</span> <span class="p">(</span> <span class="n">comp_for</span> <span class="o">|</span> <span class="p">(</span><span class="s1">','</span> <span class="p">(</span><span class="n">test</span><span class="o">|</span><span class="n">star_expr</span><span class="p">))</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span><span class="p">]</span> <span class="p">)</span>
|
|
<span class="n">trailer</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="o">|</span> <span class="s1">'['</span> <span class="n">subscriptlist</span> <span class="s1">']'</span> <span class="o">|</span> <span class="s1">'.'</span> <span class="n">NAME</span>
|
|
<span class="n">subscriptlist</span><span class="p">:</span> <span class="n">subscript</span> <span class="p">(</span><span class="s1">','</span> <span class="n">subscript</span><span class="p">)</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span><span class="p">]</span>
|
|
<span class="n">subscript</span><span class="p">:</span> <span class="n">test</span> <span class="o">|</span> <span class="p">[</span><span class="n">test</span><span class="p">]</span> <span class="s1">':'</span> <span class="p">[</span><span class="n">test</span><span class="p">]</span> <span class="p">[</span><span class="n">sliceop</span><span class="p">]</span>
|
|
<span class="n">sliceop</span><span class="p">:</span> <span class="s1">':'</span> <span class="p">[</span><span class="n">test</span><span class="p">]</span>
|
|
<span class="n">exprlist</span><span class="p">:</span> <span class="p">(</span><span class="n">expr</span><span class="o">|</span><span class="n">star_expr</span><span class="p">)</span> <span class="p">(</span><span class="s1">','</span> <span class="p">(</span><span class="n">expr</span><span class="o">|</span><span class="n">star_expr</span><span class="p">))</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span><span class="p">]</span>
|
|
<span class="n">testlist</span><span class="p">:</span> <span class="n">test</span> <span class="p">(</span><span class="s1">','</span> <span class="n">test</span><span class="p">)</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span><span class="p">]</span>
|
|
<span class="n">dictorsetmaker</span><span class="p">:</span> <span class="p">(</span> <span class="p">((</span><span class="n">test</span> <span class="s1">':'</span> <span class="n">test</span> <span class="o">|</span> <span class="s1">'**'</span> <span class="n">expr</span><span class="p">)</span>
|
|
<span class="p">(</span><span class="n">comp_for</span> <span class="o">|</span> <span class="p">(</span><span class="s1">','</span> <span class="p">(</span><span class="n">test</span> <span class="s1">':'</span> <span class="n">test</span> <span class="o">|</span> <span class="s1">'**'</span> <span class="n">expr</span><span class="p">))</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span><span class="p">]))</span> <span class="o">|</span>
|
|
<span class="p">((</span><span class="n">test</span> <span class="o">|</span> <span class="n">star_expr</span><span class="p">)</span>
|
|
<span class="p">(</span><span class="n">comp_for</span> <span class="o">|</span> <span class="p">(</span><span class="s1">','</span> <span class="p">(</span><span class="n">test</span> <span class="o">|</span> <span class="n">star_expr</span><span class="p">))</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span><span class="p">]))</span> <span class="p">)</span>
|
|
|
|
<span class="n">classdef</span><span class="p">:</span> <span class="s1">'class'</span> <span class="n">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="s1">':'</span> <span class="n">suite</span>
|
|
|
|
<span class="n">arglist</span><span class="p">:</span> <span class="n">argument</span> <span class="p">(</span><span class="s1">','</span> <span class="n">argument</span><span class="p">)</span><span class="o">*</span> <span class="p">[</span><span class="s1">','</span><span class="p">]</span>
|
|
|
|
<span class="c1"># The reason that keywords are test nodes instead of NAME is that using NAME</span>
|
|
<span class="c1"># results in an ambiguity. ast.c makes sure it's a NAME.</span>
|
|
<span class="c1"># "test '=' test" is really "keyword '=' test", but we have no such token.</span>
|
|
<span class="c1"># These need to be in a single rule to avoid grammar that is ambiguous</span>
|
|
<span class="c1"># to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,</span>
|
|
<span class="c1"># we explicitly match '*' here, too, to give it proper precedence.</span>
|
|
<span class="c1"># Illegal combinations and orderings are blocked in ast.c:</span>
|
|
<span class="c1"># multiple (test comp_for) arguments are blocked; keyword unpackings</span>
|
|
<span class="c1"># that precede iterable unpackings are blocked; etc.</span>
|
|
<span class="n">argument</span><span class="p">:</span> <span class="p">(</span> <span class="n">test</span> <span class="p">[</span><span class="n">comp_for</span><span class="p">]</span> <span class="o">|</span>
|
|
<span class="n">test</span> <span class="s1">'='</span> <span class="n">test</span> <span class="o">|</span>
|
|
<span class="s1">'**'</span> <span class="n">test</span> <span class="o">|</span>
|
|
<span class="s1">'*'</span> <span class="n">test</span> <span class="p">)</span>
|
|
|
|
<span class="n">comp_iter</span><span class="p">:</span> <span class="n">comp_for</span> <span class="o">|</span> <span class="n">comp_if</span>
|
|
<span class="n">sync_comp_for</span><span class="p">:</span> <span class="s1">'for'</span> <span class="n">exprlist</span> <span class="s1">'in'</span> <span class="n">or_test</span> <span class="p">[</span><span class="n">comp_iter</span><span class="p">]</span>
|
|
<span class="n">comp_for</span><span class="p">:</span> <span class="p">[</span><span class="s1">'async'</span><span class="p">]</span> <span class="n">sync_comp_for</span>
|
|
<span class="n">comp_if</span><span class="p">:</span> <span class="s1">'if'</span> <span class="n">test_nocond</span> <span class="p">[</span><span class="n">comp_iter</span><span class="p">]</span>
|
|
|
|
<span class="c1"># not used in grammar, but may appear in "node" passed from Parser to Compiler</span>
|
|
<span class="n">encoding_decl</span><span class="p">:</span> <span class="n">NAME</span>
|
|
|
|
<span class="n">yield_expr</span><span class="p">:</span> <span class="s1">'yield'</span> <span class="p">[</span><span class="n">yield_arg</span><span class="p">]</span>
|
|
<span class="n">yield_arg</span><span class="p">:</span> <span class="s1">'from'</span> <span class="n">test</span> <span class="o">|</span> <span class="n">testlist</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
|
<div class="sphinxsidebarwrapper">
|
|
<h4>Previous topic</h4>
|
|
<p class="topless"><a href="toplevel_components.html"
|
|
title="previous chapter">9. Top-level components</a></p>
|
|
<h4>Next topic</h4>
|
|
<p class="topless"><a href="../library/index.html"
|
|
title="next chapter">The Python Standard Library</a></p>
|
|
<div role="note" aria-label="source link">
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../bugs.html">Report a Bug</a></li>
|
|
<li>
|
|
<a href="https://github.com/python/cpython/blob/3.7/Doc/reference/grammar.rst"
|
|
rel="nofollow">Show Source
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="right" >
|
|
<a href="../library/index.html" title="The Python Standard Library"
|
|
>next</a> |</li>
|
|
<li class="right" >
|
|
<a href="toplevel_components.html" title="9. Top-level components"
|
|
>previous</a> |</li>
|
|
<li><img src="../_static/py.png" alt=""
|
|
style="vertical-align: middle; margin-top: -1px"/></li>
|
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|
<li>
|
|
<span class="language_switcher_placeholder">en</span>
|
|
<span class="version_switcher_placeholder">3.7.4</span>
|
|
<a href="../index.html">Documentation </a> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="index.html" >The Python Language Reference</a> »</li>
|
|
<li class="right">
|
|
|
|
|
|
<div class="inline-search" style="display: none" role="search">
|
|
<form class="inline-search" action="../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="footer">
|
|
© <a href="../copyright.html">Copyright</a> 2001-2019, Python Software Foundation.
|
|
<br />
|
|
The Python Software Foundation is a non-profit corporation.
|
|
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
|
<br />
|
|
Last updated on Jul 13, 2019.
|
|
<a href="../bugs.html">Found a bug</a>?
|
|
<br />
|
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
|
|
</div>
|
|
|
|
</body>
|
|
</html> |