UK CS 541 - Programming Assignment 4 CSX Type Checker

Unformatted text preview:

CS 536 — Spring 2014Programming Assignment 4CSX Type CheckerWrite member functions and classes that implement a type checker for CSX programs. Your main program calls your CSX parser. If the parse succeeds, it calls the type checker. The CSX source program to be compiled is named on the compiler’s command line. Your program writes error messages to standard output. A skeleton for the type checker module may be found in the directory ~raphael/-courses/cs541/public/proj4/startup.The Type Checker The type checker is an AST member function, operating on the abstract syntax tree built by the CSX parser. The type checker produces an error message for each scoping and type error in the program represented by the AST and returns a Boolean value indicating whether the AST has any type or scoping errors. The scope rules of CSX are similar to those of C++ and Java. A program consists of a single named class. All members within the class (fields and methods) are static (in the Java sense). All members must have distinct names. Local declarations (within a method or statement block) override any global declaration, but any one identifier may be declared only once in any particular scope. Parameters of a method are local declarations within that method’s body. All identifiers, whether class members, or local declarations, must be declared before they are used. The last member declaration must be a void method named main with no parameters. As in Java, execution commences with this method.Your type checker must print an error message if the CSX program uses an undeclared identifier or if it redeclares an identifier within the same class, method body, or statement block. (The class name is external to all other scopes; it never conflicts with any other declaration.)An identifier may denote a class name, a label, a field (either a variable or a constant), a method, a parameter of a method, or a local variable or a local constant. Local variables and constants, fields, functions (methods that return a value) and parameters may be of type int, bool, or char. Variables (fields or locals) and parameters may be arrays of int, bool, or char values. The type and scope rules of the CSX language require the following: • Arithmetic operators may be applied to int or char values; the result is of type int.• Logical operators (&&, ||, and !) may be applied only to bool values; the result is of type bool. • Relational operators (==, <, >, !=, <=, >=) may be applied only to a pair of arithmeticvalues (int or char) or to a pair of bool values; the result is of type bool. • Relational operators can be applied to bool values; by definition, false is less than true. • The scope of a field declared in the CSX class comprises all fields and methods that follow it; forward references to fields not yet declared are not allowed.• The scope of a method comprises its own body and all methods that follow it. Recursive calls are allowed, but calls to methods not yet declared are not allowed.• The scope of a local variable or constant declared in a method or block comprises all fields and statements that follow it in the method or block; forward references to locals not yet declared are not allowed.• A formal parameter of a method is considered local to the body of the method.• An identifier may only be declared once within a class, method or block. However, an identifier already declared outside a method or block may be redefined locally.• The type of a constant is the type of the expression that defines the constant’s value.• The type of a control expression (in an if or while construct) must be bool. • int, bool and char values, char arrays, and string literals may be actual parameters in print statements. • Only int and char values may be actual parameters in read statements. • The types of an assignment statement’s left- and right-hand sides must be identical. Entire arrays may be assigned if they are have the same size and component type. A string literal may be assigned to a char array if both contain the same number of characters.• The size of an array parameter is not known at compile time. Hence all size restrictions involving the assignment of array parameters are enforced at run time.• The types of an actual parameter and its corresponding formal parameter must be identical. • Arrays may be passed as parameters. • Assignment to constant identifiers (fields or locals) is invalid. • Only identifiers denoting procedures (methods with a void result type) may be called in statements. • Only identifiers denoting functions (methods with a non-void result type) may be called in expressions. The type of a function call is the result type of the function. • return statements with an expression may only appear in functions. The expression returned by a return statement must have the same type as the function within which it appears. • return statements without an expression may only appear in procedures (void result type). • If necessary, an implicit return statement is assumed at the end of a procedure (but not a function). • Any expression (including variables, constants and literals) of type int, char or bool may be type-cast to an int, char or bool value. These are the only type casts allowed.• An identifier that labels a while statement is a local declaration in the scope immediately containing the while statement. No other declaration of the identifier in the same scope is allowed. Labels on while statements are an extra-credit option.• An identifier referenced in a break or continue statement (which you may implement for extra credit) must denote a label (on a while statement). Moreover, the break or continue statement must appear within the body of the while statement that is selected by the label.• A void method of no parameters named main must be the last method declared in the class that constitutes a CSX program.• The size of an array (in a declaration) must be greater than zero.• Only expressions of type int or char may be used to index arrays. To prevent one type error from causing multiple error messages, you may assume that the result of an arithmetic operation is always int and that the result of a logical or relational operation is always bool, even when an operand is type-incorrect. For example, the following expression should


View Full Document

UK CS 541 - Programming Assignment 4 CSX Type Checker

Download Programming Assignment 4 CSX Type Checker
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming Assignment 4 CSX Type Checker and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming Assignment 4 CSX Type Checker 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?