CIS-4050/6050 Homework #2: Augusta Type Checking

Due: Friday, February 15, 2019

Reading: In Chapter 4 read through Section 4.2 on type systems.

In this assignment you will add some functionality to the current Augusta language and flesh out the type checker (part of semantic analysis) to include some non-trivial checks. Follow the checklist below:

  1. Modify the grammar in Ada.g4 so that it includes support for if statements. See the grammar in the Ada Reference Manual for ideas. The notation in the Ada Reference Manual uses {...} to denote "zero or more" and [...] to denote "optional." ANTLR's syntax for these things is different. Note that if statements have a semicolon at the end. Note also that in Ada a "sequence of statements" must have at least one statement.

  2. Add support for the relational operator tokens to the bottom of the Ada.g4 file. THIS HAS ALREADY BEEN DONE!

  3. Add the Boolean literal tokens "true" and "false" to the list of allowed tokens. Be sure they are recognized in a case insensitive manner. See how the reserved words are handled. Technically Ada does not reserve true and false since all the built-in types are treated as if they were part of a special library package named Standard. However, for purposes of this assignment we'll treat true and false as reserved words.

  4. Enhance the expression grammar to support the relational operators. All of the relational operators have the same precedence (in Ada) and left-to-right associativity. Their precedence is lower than that of the additive operators. I recommend using a non-terminal named "relational_expression."

  5. Modify SemanticAnalyzer.scala so that it properly handles declarations that use either type "Integer" or "Boolean" (it may already do this; check that).

  6. Update the type checking in SemanticAnalyzer.scala so the following rules are checked (some of the rules might already be implemented):

    1. The type of the expression used to control conditional branches must be Boolean.
    2. The type of a numeric literal is Integer.
    3. The type of a boolean literal is Boolean.
    4. Both operands of all binary operators must have the same type.
    5. The left and right sides of an assignment must have the same type.
    6. Arithmetic operations cannot be done on Boolean operands. This includes the unary operators.
    7. The operands of the relational operators <, <=, >, >= must be Integer. The operands of = and /= can be any type (as long as they match as required above).
    8. The result of any arithmetic operation is Integer
    9. The result of any relational operation is Boolean

    You should also apply "reasonable" error recovery strategies. For example, an expression such as: X + True should be flagged as an error, but given the overall type of, for example, "Integer" so the compiler can continue its analysis in a (hopefully) useful way.

  7. Design one (or more) demonstration files that illustrate these new features. Ideally your demonstration should include bad code as well as correct code to verify that the compiler properly detects and diagnoses errors.

Note that errors should be reported by calling the reportError method in the reporter object. However, you may find the helper method typeError to be useful. It just calls reportError and uses a given token to define the location of the error.

Submit your modified files in a zip archive to Moodle.

Last Revised: 2019-02-07
© Copyright 2019 by Peter C. Chapin <>