Basic kinds of declarations Constant declarations( value bindings Associate values to names, is single assignment const int size=199 Type declarations Bind names to newly constructed types and may also create aliases for existing named types Type names are usually used in conjunction with a type equivalence algorithm to perform type checking of a program Struct entr ichar * name; Int count struct Entry * next
Basic kinds of declarations • Constant declarations (Value bindings) – Associate values to names, is single assignment. – const int SIZE = 199 ; • Type declarations – Bind names to newly constructed types and may also create aliases for existing named types – Type names are usually used in conjunction with a type equivalence algorithm to perform type checking of a program Struct Entry {char * name; int count; struct Entry *next ;};
Variable declarations Bind names to data types Integer a, b[ 100 An attribute of variables related to scope that is also implicitly or explicitly bound is the allocation of memory for the declared variable and the duration during execution of the allocation Lifetime or extent of the declaration) int count( void) (static int counter =0 return ++counter int count(ⅴol Extern int counter =0 return ++counter
• Variable declarations – Bind names to data types, Integer a, b[100] – An attribute of variables related to scope that is also implicitly or explicitly bound is the allocation of memory for the declared variable and the duration during execution of the allocation (lifetime or extent of the declaration) int count(void) {static int counter = 0; return ++counter;} int count(void) {extern int counter = 0; return ++counter;}
Procedure/function declarations Include explicit and implicit declarations Definition and declaration The strategies Use one symbol table to hold the names from all the different kinds of declarations Use a different symbol table for each kind of declaration
• Procedure/function declarations – Include explicit and implicit declarations • Definition and Declaration • The strategies: – Use one symbol table to hold the names from all the different kinds of declarations. – Use a different symbol table for each kind of declaration
6.3.3 Scope Rules and block Structure
6.3.3 Scope Rules and Block Structure
TTwo rules Declaration before use Without this rule one-pass compilation is impossible The most closely nested rule for block structure Block: any construct that can contain declarations, such as procedure/function declarations a language is block structured 1) If it permits the nesting of blocks inside other blocks (2)If the scope of declarations in a block are limited to that block and other block contained in that block
Two rules: • Declaration before use – Without this rule one-pass compilation is impossible • The most closely nested rule for block structure – Block: any construct that can contain declarations, such as procedure/function declarations. – A language is block structured: (1) If it permits the nesting of blocks inside other blocks; (2) If the scope of declarations in a block are limited to that block and other block contained in that block