Lab 1(c): Syntax Definition

Project
September 07, 2021

In this lab, you define the concrete and abstract syntax of ChocoPy in Spoofax. From this definition, you generate an Eclipse editor, which provides syntax checking, error recovery, and syntax highlighting.

In this lab, focus on translating the grammar in Figure 3 of the ChocoPy Reference Manual to an idiomatic context-free syntax definition with constructors. In the next lab, you extend this definition with disambiguation rules for associativity, priority, and layout constraints.

Objectives

Develop a syntax definition for ChocoPy in SDF3 and generate an Eclipse editor from it. The definition should include:

  1. The start symbol Start
  2. A lexical syntax definition for all ChocoPy lexemes
  3. A context-free syntax definition ChocoPy complying with the grammar in Figure 3 of the reference manual
  4. Constructor names for context-free syntax productions

Syntax Definition

You should define your syntax in SDF3. You can start by opening the file src/start.sdf3 in your chocopy project. You can also split your syntax definition over several modules in src/ and import modules into module start.

module start
imports lex
imports expressions
imports statements
// etc.
context-free start symbols Start

For more information on how to write SDF3 syntax definitions, you can also review the old SDF3 reference manual and the new SDF3 reference.

Note that both of these are written for Spoofax 2, and that not all information may still be correct in Spoofax 3!

Context-free Syntax

Start with the context-free syntax of the language. Use the context-free grammar in the ChocoPy Language Reference Manual as a reference.

We recommend to use template productions for your context-free syntax definition, since they help when generating artifacts other than just the parser. When you use template productions, you need to make sure that templates are correctly tokenised. Otherwise, the parser would reject layout in certain positions, for example between [ and ] in an array type. Check the SDF3 documentation for details. In case you want to use < or > as symbols inside a template, you can use alternate template brackets [...].

Lexical Syntax

Continue with the lexical syntax definition including identifiers, integer, and simple layout.

First, define lexical syntax rules:

lexical syntax
  ID     = ...
  INT    = ...
  LAYOUT = ...

Second, define follow restrictions to ensure longest match:

lexical restrictions
  ID -/- ...
  INT -/- ...
context-free restrictions
 LAYOUT? -/- ...

Finally, use rejection rules to rule out reserved words.

lexical syntax
  ID = ... {reject}

Testing Your Syntax Definition

You now can check your tests.

It is important to get your grammar working at this stage. Do not move on if you have issues here, since there is a high chance that these issues influence your other tests as well. If you experience weird behaviour on your tests or a large amount of ambiguity, this is most likely caused by an erroneous definition of LAYOUT.

Finally, you should add lexical syntax rules for comments to your syntax definition. Start with single line comments. Do not forget to define follow restrictions.