eelco-visser-compiler-const.../lab/2.html

1060 lines
19 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Eelco Visser">
<meta name="generator" content="Jekyll v3.8.5">
<title>Lab 2: Disambiguation</title>
<!-- <base href="/2021"> -->
<!--link rel="canonical" href="https://getbootstrap.com/docs/4.3/examples/starter-template/"-->
<link rel="icon" href="../img/logo/pl_ico2_2B3_icon.ico" type="image/x-icon">
<!-- Bootstrap core CSS -->
<!--link href="https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
<!-- Custom styles for this template -->
<link href="../css/main.css" rel="stylesheet">
<link href="../css/borders-responsive.css" rel="stylesheet">
<link rel="stylesheet" href="../css/pl.css">
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="../index.html">
TU Delft | CS4200
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="../lectures/index.html" tabindex="-1" aria-disabled="true">Lectures</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="../homework/index.html" tabindex="-1" aria-disabled="true">Homework</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="../project/index.html" tabindex="-1" aria-disabled="true">Project</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="../news/index.html" tabindex="-1" aria-disabled="true">News</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="../blog/index.html" tabindex="-1" aria-disabled="true">Blog</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 col-xl-12">
<div class="mt-3 mb-3 pt-3 pb-3 text-dark border-top border-bottom border-grey">
<div class="text-dark font-weight-bold">
<h1>
Lab 2: Disambiguation
</h1>
</div>
<div>
</div>
<div>
Project
</div>
<div>
September 14, 2021
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-9 border-lg-right border-grey">
<p>In this lab you adapt and extend the syntax definition of the previous lab with disambiguation rules and layout constraints in order to ensure that the syntax definition is unambiguous.</p>
<h3 id="objectives">Objectives</h3>
<p>Adapt and extend the syntax definition that you developed in the previous lab such that it</p>
<ol>
<li>is an idiomatic SDF3 definition</li>
<li>realizes disambiguation using declarative disambiguation rules</li>
<li>defines layout constraints to enforce layout-sensitive syntax</li>
</ol>
<h3 id="disambiguation-rules">Disambiguation Rules</h3>
<p>Section 4.1 of the ChocoPy <a href="https://chocopy.org/chocopy_language_reference.pdf">reference manual</a> specifies the precedence and associativity rules of the language.
Translate this into associativity and priority rules in SDF3.</p>
<p>You need disambiguation constructs to disambiguate your syntax definition.
You can specify the associativity with attributes <code class="language-plaintext highlighter-rouge">left</code>, <code class="language-plaintext highlighter-rouge">right</code>, or <code class="language-plaintext highlighter-rouge">non-assoc</code> (See documentation).
These attributes are added to the end of a rule:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>context-free syntax
Exp.Constr1 = ... {left}
</code></pre></div></div>
<p>You can specify precedence in a <code class="language-plaintext highlighter-rouge">context-free priorities</code> section.
In this section, you order groups of rules:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>context-free priorities
{
Exp.Constr1
Exp.Constr2
} &gt; { ... } &gt; ...
</code></pre></div></div>
<p>You can also define the associativity of a group:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>context-free priorities
{ left:
Exp.Constr1
Exp.Constr2
} &gt; { ... } &gt; ...
</code></pre></div></div>
<p>Finally you should also consider specifying a <code class="language-plaintext highlighter-rouge">bracket</code> rule for disambiguation of expressions.</p>
<h3 id="layout-constraints">Layout Constraints</h3>
<p>The syntax of ChocoPy is layout-sensitive, meaning that indentation and newlines are significant.
To enable layout-sensitive parsing in Spoofax 3, we need to make some changes to the <code class="language-plaintext highlighter-rouge">spoofaxc.cfg</code> file.
In the <code class="language-plaintext highlighter-rouge">spoofaxc.cfg</code> file, replace:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sdf3 {}
</code></pre></div></div>
<p>with:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sdf3 {
parse-table-generator {
layout-sensitive = true
}
}
</code></pre></div></div>
<p>and replace:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>parser {
default-start-symbol = sort Start
}
</code></pre></div></div>
<p>with:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>parser {
default-start-symbol = sort Start
variant = jsglr2 {
preset = LayoutSensitive
}
}
</code></pre></div></div>
<p>In the ChocoPy grammar in Figure 3 in the <a href="https://chocopy.org/chocopy_language_reference.pdf">reference manual</a>, layout-sensitive parsing is defined using <code class="language-plaintext highlighter-rouge">NEWLINE</code>, <code class="language-plaintext highlighter-rouge">INDENT</code>, and <code class="language-plaintext highlighter-rouge">DEDENT</code> tokens.
Section 3.1 describes the meaning of these tokens.</p>
<p>Translate these tokens into <em>layout constraints</em> and <em>layout declarations</em> as presented in <a href="../lecture/3.html">Lecture 3</a> and in the papers cited below.</p>
<p>Additional documentation on layout constraints can also be found in the <a href="https://www.spoofax.dev/references/syntax/layout-sensitivity/">Spoofax 2 documentation</a>. <strong>Please note that this documentation is for Spoofax 2, and may therefore not fully correspond to Spoofax 3 behavior.</strong></p>
<h3 id="cexpr-and-expr"><code class="language-plaintext highlighter-rouge">CExpr</code> and <code class="language-plaintext highlighter-rouge">Expr</code></h3>
<p>The ChocoPy reference manual uses two separate expression sorts in its grammar, in order to keep compatibility with Python and to disambiguate. SDF3 has disambiguation features that makes this separation of sorts unneccessary. As a result, we expect you to implement a grammar that only uses a single expression sort. For more information on grading, see <a href="../milestone/1.html">milestone 1</a>.</p>
<p>One particularity is that ChocoPy does not allow any binary operators that feature expressions such as negation on the right-hand side. Indeed, <code class="language-plaintext highlighter-rouge">True == not False</code> is invalid syntax in ChocoPy. In order to emulate this restriction using a flattened grammar in SDF3, you can use the following priority directive syntax:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>context-free priorities
A.CA &lt;n&gt; .&gt; B.CB
</code></pre></div></div>
<p>This restriction states that the <code class="language-plaintext highlighter-rouge">n</code>-th (0-indexed) child of <code class="language-plaintext highlighter-rouge">A.CA</code> cannot be a term of type <code class="language-plaintext highlighter-rouge">B.CB</code>. For example:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>context-free grammar
Expr.Eq = &lt;&lt;Expr&gt; == &lt;Expr&gt;&gt;
Expr.Not = &lt;not &lt;Expr&gt;&gt;
context-free priorities
Expr.Eq &lt;2&gt; .&gt; Expr.Not
</code></pre></div></div>
<p>This will reject a <code class="language-plaintext highlighter-rouge">not</code> expression from appearing on the right-hand side of an equality expression.</p>
<p>Using this syntax and other SDF3 disambiguation features discussed in the lectures, you should be able to implement a ChocoPy grammar in SDF3 that adheres to the reference manual while only using a single sort for expressions.</p>
<h3 id="references">References</h3>
<ul class="list-group list-group-flush pb-3">
<li class="list-group-item pl-lg-0 border-0 pb-0">
<div class="bd-callout border-primary pl-3">
<div>
<a name="AmorimSEV18"></a>
<a class="font-weight-bold text-dark" target="_blank" href="https://doi.org/10.1145/3276604.3276607">Declarative specification of indentation rules: a tooling perspective on parsing and pretty-printing layout-sensitive languages</a>
</div>
<div class="font-weight-light text-secondary" style="font-size:90%;">
<a class="text-secondary" href="https://www.linkedin.com/profile/view?id=136481548">Luis Eduardo de Souza Amorim</a>, <a class="text-secondary" href="http://michael.steindorfer.name">Michael J. Steindorfer</a>, <a class="text-secondary" href="http://www.informatik.uni-marburg.de/~seba/">Sebastian Erdweg</a>, <a class="text-secondary" href="http://eelcovisser.org">Eelco Visser</a>.
</div>
<div class="font-weight-light text-secondary" style="font-size:90%;">
SLE 2018
[<a class="text-secondary" href="https://doi.org/10.1145/3276604.3276607" target="_blank">doi</a>, <a class="text-secondary" target="_blank" href="https://researchr.org/publication/AmorimSEV18/bibtex">bib</a>, <a href="https://researchr.org/publication/AmorimSEV18" class="text-secondary" target="_blank">researchr</a>, <a class="text-secondary" data-toggle="collapse" href="2.html#AbstractAmorimSEV18" role="button" aria-expanded="false" aria-controls="AbstractAmorimSEV18"">abstract</a>]
<!-- AmorimSEV18 -->
</div>
<div class="collapse mt-1 mb-2" id="AbstractAmorimSEV18">
<div class="">
In layout-sensitive languages, the indentation of an expression or statement can influence how a program is parsed. While some of these languages (e.g., Haskell and Python) have been widely adopted, there is little support for software language engineers in building tools for layout-sensitive languages. As a result, parsers, pretty-printers, program analyses, and refactoring tools often need to be handwritten, which decreases the maintainability and extensibility of these tools. Even state-of-the-art language workbenches have little support for layout-sensitive languages, restricting the development and prototyping of such languages. In this paper, we introduce a novel approach to declarative specification of layout-sensitive languages using layout declarations. Layout declarations are high-level specifications of indentation rules that abstract from low-level technicalities. We show how to derive an efficient layout-sensitive generalized parser and a corresponding pretty-printer automatically from a language specification with layout declarations. We validate our approach in a case-study using a syntax definition for the Haskell programming language, investigating the performance of the generated parser and the correctness of the generated pretty-printer against 22191 Haskell files.
</div>
</div>
</div>
</li>
<li class="list-group-item pl-lg-0 border-0 pb-0">
<div class="bd-callout border-primary pl-3">
<div>
<a name="ErdwegRKO12"></a>
<a class="font-weight-bold text-dark" target="_blank" href="http://dx.doi.org/10.1007/978-3-642-36089-3_14">Layout-Sensitive Generalized Parsing</a>
</div>
<div class="font-weight-light text-secondary" style="font-size:90%;">
<a class="text-secondary" href="http://www.informatik.uni-marburg.de/~seba/">Sebastian Erdweg</a>, <a class="text-secondary" href="https://researchr.org/alias/tillmann-rendel">Tillmann Rendel</a>, <a class="text-secondary" href="https://researchr.org/alias/christian-k%C3%A4stner">Christian Kästner</a>, <a class="text-secondary" href="https://researchr.org/alias/klaus-ostermann">Klaus Ostermann</a>.
</div>
<div class="font-weight-light text-secondary" style="font-size:90%;">
SLE 2012
[<a class="text-secondary" href="http://dx.doi.org/10.1007/978-3-642-36089-3_14" target="_blank">doi</a>, <a class="text-secondary" target="_blank" href="https://researchr.org/publication/ErdwegRKO12/bibtex">bib</a>, <a href="https://researchr.org/publication/ErdwegRKO12" class="text-secondary" target="_blank">researchr</a>]
<!-- ErdwegRKO12 -->
</div>
</div>
</li>
<li class="list-group-item pl-lg-0 border-0 pb-0">
<div class="bd-callout border-primary pl-3">
<div>
<a name="ErdwegRRO12"></a>
<a class="font-weight-bold text-dark" target="_blank" href="http://doi.acm.org/10.1145/2364506.2364526">Layout-sensitive language extensibility with SugarHaskell</a>
</div>
<div class="font-weight-light text-secondary" style="font-size:90%;">
<a class="text-secondary" href="http://www.informatik.uni-marburg.de/~seba/">Sebastian Erdweg</a>, <a class="text-secondary" href="https://researchr.org/alias/felix-rieger">Felix Rieger</a>, <a class="text-secondary" href="https://researchr.org/alias/tillmann-rendel">Tillmann Rendel</a>, <a class="text-secondary" href="https://researchr.org/alias/klaus-ostermann">Klaus Ostermann</a>.
</div>
<div class="font-weight-light text-secondary" style="font-size:90%;">
haskell 2012
[<a class="text-secondary" href="http://doi.acm.org/10.1145/2364506.2364526" target="_blank">doi</a>, <a class="text-secondary" target="_blank" href="https://researchr.org/publication/ErdwegRRO12/bibtex">bib</a>, <a href="https://researchr.org/publication/ErdwegRRO12" class="text-secondary" target="_blank">researchr</a>]
<!-- ErdwegRRO12 -->
</div>
</div>
</li>
</ul>
</div>
<div class="col-sm-12 col-md-12 col-lg-3 col-xl-3 " >
<div class="sticky-top top70 d-none d-lg-block d-xl-block">
<div class="pb4 mb3 border-bottom border-grey ">
<nav>
<ul class="pagination justify-content-left">
<li class="page-item">
<a class="page-link" href="../milestone/0.html">
&laquo;
</a>
</li>
<li class="page-item">
<a class="page-link" href="../project/index.html">
^
</a>
</li>
<li class="page-item">
<a class="page-link" href="3a.html">&raquo;</a>
</li>
</ul>
</nav>
</div>
<ul id="my_toc" class="toc list-group list-group-flush d-none d-lg-block d-xl-block p-0 ml-0 mt-3">
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="2.html#objectives">Objectives</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="2.html#disambiguation-rules">Disambiguation Rules</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="2.html#layout-constraints">Layout Constraints</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="2.html#cexpr-and-expr">CExpr and Expr</a></li>
<li class="list-group-item pl-0 ml-0 border-0 pl-0 pt-0 pb-1 pr-0 m-0 mr-3"><a href="2.html#references">References</a></li>
</ul>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="border-top border-bottom border-grey mt-3 pt-3">
<nav>
<ul class="pagination justify-content-center">
<li class="page-item">
<a class="page-link" href="../milestone/0.html">
Previous
</a>
</li>
<li class="page-item">
<a class="page-link" href="3a.html">Next</a>
</li>
<li class="page-item">
<a class="page-link" href="../project/index.html">
Index
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>