Reading: Chapter 3 of the text covers parsing. Read Sections 3.1, 3.2, and 3.5. We may return to the sections on top-down parsing (3.3) and bottom-up parsing (3.4) later in the course.
In this assignment you will add syntax to the Augusta ANTLR grammar to support several new language features.
Start by updating your clone of the Augusta project:
The repository contains (failing) tests for the new syntax forms described below. Adjust the grammar (in augusta.g4) so all tests pass.
There are several new declaration forms needed. These include:
Enumeration Types
type Some_Name is (Value1, Value2, Value3);
Here Some_Name and the enumerators (e.g., Value1) are all identifiers There must be at least one enumerator.
Type Declarations
type Some_Name is new Integer; type Some_Name is range 1 .. 10;
The name of the type being declared is an identifier. In the first form the name of the "parent type" is also an identifier. In the second form, the range is given by two integer literals (and not general expressions).
Subtype Declarations
subtype Some_Name is Integer range 1 .. 10;
The name of the subtype being declared is an identifier. The name of the type from which it is derived is also an identifier. The range is given by two integer literals (and not general expressions). Note that in Ada subtype ranges can be general expressions that are evaluated dynamically. That is much more complicated to implement, so we won't support it. Ada also allows subtypes of enumeration types in which case the range is a range of enumerators. We won't support that feature.
There are several new expression forms needed. These include:
Logical Operators
X and Y X or Y not X
The precedence of the and operator and the or operator should be lower than the relational operators so that expressions like X < Y and Y < Z are parsed as (X < Y) and (Y < Z). The and operator should have higher precedence than the or operator so that expressions like A and B or C are parsed as (A and B) or C. Ada requires that expressions with mixed logical operators must be parenthesized, but we won't implement that rule here. It can be embedded in the grammar, but it makes the expression grammar less elegant. Instead, if we implement it at all, we'll do so in the semantic analyzer.
The not operator should be treated as another kind of unary operator. This means it has higher precedence than the other logical operators so expressions like not A and B are parsed as (not A) and B.
There are several new statement forms needed. These include:
Return Statements
return Some_Expression;
The expression is optional.
Declare Statements
declare X : Integer; begin ... end;
Between "declare" and "begin" there can be zero or more declarations. Between "begin" and "end" there can be one or more statements (including other declare statements).
Submit your augusta.g4 file to Canvas
Last Revised: 2025-02-26
© Copyright 2025 by Peter Chapin <peter.chapin@vermontstate.edu>