Docsity

Decaf Programming Language: Scope and Type Checking Rules, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
Decaf Semantic Specification
3/04/08
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.
pf2

Partial preview of the text

Download Decaf Programming Language: Scope and Type Checking Rules and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

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.