9.0.0.1

6 — Class: Syntax🔗

Due Thursday, 16 October 2025, 11:59:59pm

Purpose to build a model of the syntax of a language with a simple class system

Warning This assignment is one of the two largest this semester. Getting started early and working systematically—that is, following the design recipes of Fundamentals I and II—will pay off.

Delivery Deliver your solutions in a directory called 6 within your assigned GitHub repository. The directory should contain the following items:

  • the executables xclass as specified below;

  • the sub-directory 6/Tests/ with the required integration test; and

  • an optional sub-directory called 6/Other/ for all auxiliary files.

The directory 6/ may also contain a Makefile (see Make) in case you need us to build your executable. An executable should not be checked into a repository.

Programming Task Using your favorite programming language, you are to create a syntax model of the Class language, an extension of Core with a simple class system. This task requires (1) the extension of the parser; (2) new validity-checking passes; and (3) an adaptation of the existing validity checker.

ExampleDD is an Example whose Names also contain the following keywords: if0, while0, block, def, =, /, +, ==, class, method, isa, new, >.

See figure 8 for the grammar of the Class language.

The implementation of the parser should adapt your solution to 4 — Core: Validity.

The adaptation of the validity checker must confirm that every occurrence of a variable in an expression or statement comes with a matching declaration or method parameter in its context. The checker consumes an error-free AST of a Class program, walks the tree, and replaces all variable occurrence that violate the validity constraint so that an IDE would be able to notify a Class programmer in a suitable manner.

In addition, the validity checker must ensure that some additional conditions hold:
  1. No two class definitions should have the same ClassName.

  2. In a single class, no two methods and no two fields should have the same name, respectively.

  3. Within a method, no two parameters should have the same name.

  4. The occurrence of every ClassName in a Statement or an Expression must refer to a defined class.

Note Following tradition, the variable this denotes the target object of a method call—by default and implicitly. That is, if a programmer wishes to define a variable this inside of a method, doing so will “shadow” the default binding.

Hint Since the validity checker for Class performs distinct tasks, the principles of Fundamentals 1 and 2 dictate that your implementation composes several helpers—also called passes in the context of programming languages.

  Program     ::= (Class^* Declaration^* Statement^* Expression)

  

  Class       ::= (class ClassName (FieldName^*) Method^*)

  

  Method      ::= (method MethodName (Parameter^*)

                    Declaration^*

                    Statement^*

                    Expression)

  

  Declaration ::= (def Variable Expression)

  

  Statement   ::= (Variable = Expression)

                | (if0 Expression Block Block)

                | (while0 Expression Block)

  

                | (Variable --> FieldName = Expression)

  

  Block       ::= Statement

                | (block Declaration^* Statement^+)

  

  Expression  ::= GoodNumber

                | Variable

                | (Variable + Variable)

                | (Variable / Variable)

                | (Variable == Variable)

  

                | (new ClassName (Variable^*))

                | (Variable --> FieldName)

                | (Variable --> MethodName (Variable^*))

                | (Variable isa ClassName)

  

  The set of ClassNames is the same as the set of Variables.

  

  The set of FieldNamess is the same as the set of Variables.

  

  The set of MethodNames is the same as the set of Variables.

  

  The set of Parameters is the same as the set of Variables.

  

  The set of Variables consists of all Names, minus keywords.

  

  The set of GoodNumbers comprises all inexact numbers

  (doubles) between -1000.0 and +1000.0, inclusive.

Figure 8: The concrete syntax of the Class language

The xclass program reads one ExampleDD from STDIN, constructs the AST, performs the validity checks, and writes one of the following strings to STDOUT: The outcomes are listed in the order in which they may occur for a given program.

  • "parser error" if the given ExampleDD is not an element of the Class grammar;

  • "duplicate class name" if in the well-formed Class program two of the class definitions come with the same name;

  • "duplicate method, field, or parameter name" if in the well-formed Class program one class definition one method definition, or one Shape comes with (at least) two fields, methods, or parameters that have the same name;

  • "undeclared variable error" if in the well-formed Class program one of the variable references comes without declaration or one reference to a ClassName (in a statement or an expression) comes without a (possibly imported) class definition;

  • "belongs" if the given ExampleDD is a well-formed and valid Class program.

Testing Task Create 10 integration tests for xclass.

A test always consists of inputs, expected output, and an automated procedure for comparing the actual output with the expected one. — For this course, an integration test consists of a pair of files: n-in.ss, the input file, and n-out.ss, the expected output file, where n is an integer between 0 and the requested number of tests (exclusive). The comparison procedure is (string or numeric) equality on the content of the output file and the actual output. — Constraint No test file may exceed the size limit of 5Kb.

Readings Structured Data and Abstraction (Validity)