

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Decaf Semantic Specification 3/04/ Scope Rules A Decaf program consists of a single class declaration that may contain a number of variable declarations followed by method declarations. Method declarations can only occur at this top level. Variable declarations can also occur in any block, which may be the body of a method or a block can be introduced where any statement can occur. Any block introduces a new level of scope where variables of the same name can mask a more global variable. All identifiers must be declared When entering a scope, all declarations in that scope are immediately visible. Identifiers with a scope must be unique, i.e. cannot have two variables or functions with the same name – no overloading of function names Declarations in a global scope are accessible within a local scope unless they are masked by another use of the identifier (no closed scopes from private or other modifiers) In a function declaration, formal parameters are at the same scope level as the local variables of its block Recursive functions are allowed (related to second bullet) Type Checking Rules Expression evaluation: Arithmetic operators only work on integers, so there is no mixed type arithmetic and conversions to support. Relational and equality operators take integers as arguments and result in a Boolean. Conditional operators work only on Booleans. Method invocation: The number and types of the actual and formal parameters must match (these will only be int and bool). If the return type of the method is void, then the method call must be used as a statement. If the return type of the method is bool or int, then the method call can be used in the appropriately typed arithmetic expression. Assignment: The type of the identifier used as the lvalue must be the same as the expression on the right. Control structures: Expressions in the condition portion of if, while and for statements must have type bool. In a for statement, the assignments must be of type int. The type of the value in a return statement must be the same as the return type of the method.
Other Compile-time Rules The Decaf program must contain one method called main which takes no parameters and returns type void. A break statement can only appear in a loop. Methods that return void have a return statements with an empty expression.