Download presentation
Presentation is loading. Please wait.
Published byLiv Henriksen Modified over 6 years ago
1
DECLARATION Chuen-Liang Chen Department of Computer Science
and Information Engineering National Taiwan University Taipei, TAIWAN
2
Introduction variable declaration + type declaration
creating symbol table, but not generating code attribute -- internal representation of declaration related data structures 1. symbol tables 2. attribute records 3. type description records 1, 2 & 3 are linked together, and form a complete symbol table 4. semantic records used by declaration semantic routines semantic information is stored in 4 as well as 2 and 3
3
Relevant grammar (1/2) <variable_decl> ® <id_list> : <type> ; <type_decl> ® type <id> is <type_def> ; <id_list> ® <id> <id_list_tail> <id_list_tail> ® , <id> <id_list_tail> <id_list_tail> ® l <id> ® IDENTIFIER <type> ® <type_name> <type> ® <type_def> <type_name> ® <id> <type_def> ® <record_type_def> <type_def> ® <array_type_def> <record_type_def> ® record <comp_list> end <comp_list> ® <comp_decl> <comp_list_tail> <comp_list_tail> ® <comp_decl> <comp_list_tail> <comp_list_tail> ® l <comp_decl> ® <id_list> : <type_name> ; <array_type_def> ® array ( <static_range> ) of <type> <static_range> ® INTL .. INTL
4
Relevant grammar (2/2) example declarations I , J : Integer ;
type R is record X , Y : Integer ; end ; ARecord : R ; A1 : array ( ) of R ; A2 : array ( ) of array ( ) of Integer ;
5
Type description record
enum type_form { INTEGERTYPE, FLOATTYPE, STRINGTYPE, RECORDTYPE, ARRAYTYPE, ERRORTYPE }; typedef unsigned long address_range; struct range { long lower, upper; }; typedef struct type_des { enum type_form form; address_range size; union { /* form == INTEGERTYPE, FLOATTYPE, STRINGTYPE, ERRORTYPE */ /* empty variant */ /* form == RECORDTYPE */ struct { symbol_table fields; attributes *field_list; } ; /* form == ARRAYTYPE */ struct range bounds; struct type_des *element_type; } type_descriptor;
6
Attribute record #include "symtab.h" typedef short boolean;
struct address { short var_level; address_range var_offset; boolean indirect; } ; enum id_class { VARIABLE, TYPENAME, FIELD }; typedef struct attributes { enum id_class class; id_entry id; /* id_entry is defined in symtab.h */ struct type_des *id_type; union { /* class == VARIABLE */ struct address var_address; /* class == TYPENAME */ /* empty variant */ /* class == FIELD */ struct { address_range field_offset; struct attributes *next_field; } ; } ; } attributes;
7
Relevant grammar (2/2) example declarations I , J : Integer ;
type R is record X , Y : Integer ; end ; ARecord : R ; A1 : array ( ) of R ; A2 : array ( ) of array ( ) of Integer ;
8
Example symbol table (1/3)
ST1 name attrs float integer I J A1 cls id id_tp TypeName A2 cls id id_tp TypeName TD1 frm sz IntegerType 1 TD2 frm sz FloatType 2 var_addr 1 var_lvl var_off indirect F A4 cls id id_tp Variable var_addr 1 var_lvl var_off indirect F A3 cls id id_tp Variable
9
Example symbol table (2/3)
ST1 name attrs R ARecord ST2 name Y X attrs A7 cls id id_tp TypeName TD3 frm sz RecordType 2 fld list var_addr 1 var_lvl var_off indirect 2 F A8 cls id id_tp Variable A5 cls id id_tp Field f_off next Ù A6 cls id id_tp Field f_off next 1 TD1
10
Example symbol table (3/3)
ST1 name attrs A1 A2 var_addr 1 var_lvl var_off indirect 4 F A9 cls id id_tp Variable TD4 frm sz ArrayType 20 bounds lower upper 1 10 e_tp TD3 TD1 var_addr 1 var_lvl var_off indirect 24 F A10 cls id id_tp Variable TD6 frm sz ArrayType 15 bounds lower upper 4 6 e_tp TD5 frm sz ArrayType 5 bounds lower upper 8 12 e_tp
11
Semantic record (1/2) <variable_decl> ® <id_list> : <type> ; <type_decl> ® type <id> is <type_def> ; <id_list> ® <id> <id_list_tail> <id_list_tail> ® , <id> <id_list_tail> <id_list_tail> ® l <id> ® IDENTIFIER <type> ® <type_name> <type> ® <type_def> <type_name> ® <id> <type_def> ® <record_type_def> <type_def> ® <array_type_def> <record_type_def> ® record <comp_list> end <comp_list> ® <comp_decl> <comp_list_tail> <comp_list_tail> ® <comp_decl> <comp_list_tail> <comp_list_tail> ® l <comp_decl> ® <id_list> : <type_name> ; <array_type_def> ® array ( <static_range> ) of <type> <static_range> ® INTL .. INTL
12
Semantic record (2/2) struct id { string id; };
struct one_id { struct id name; struct one_id *next_id; } ; struct type_ref { type_descriptor *object_type; }; struct record_def { type_descriptor *this_type; address_range next_offset; } ; struct range { long lower, upper; }; enum semantic_record_kind { ID, IDLIST,TYPEREF, RECORDDEF, RANGE, ERROR }; struct semantic_record { enum semantic_record_kind record_kind; union { struct id id; /* ID */ struct one_id *first_id; /* IDLIST */ struct type_ref type_ref; /* TYPEREF */ struct record_def record_def; /* RECORDDEF */ struct range range; /* RANGE */ /* empty variant */ /* ERROR */ } ;
13
Initial symbol table with pre-defined type descriptor ST1 name attrs
float integer offset = 0 A1 cls id id_tp TypeName A2 cls id id_tp TypeName TD1 frm sz IntegerType 1 TD2 frm sz FloatType 2
14
Pre-defined type variable declaration (1/6)
semantic record -- struct id { string id; }; struct one_id { struct id name; struct one_id *next_id; } ; struct type_ref { type_descriptor *object_type; }; struct semantic_record { enum semantic_record_kind record_kind; union { struct id id; /* ID */ struct one_id *first_id; /* IDLIST */ struct type_ref type_ref; /* TYPEREF */ } ; } ; relevant grammar with action symbols -- <variable_decl> ® <id_list> : <type> #var_decl($1,$3) ; <id_list> ® <id> #start_id_list($1,$2) <id_list_tail> #cp($2,$$) <id_list_tail> ® , <id> #next_id($$,$2,$3) <id_list_tail> #cp($3,$$) <id_list_tail> ® l <id> ® IDENTIFIER #process_id($$) <type> ® <type_name> #cp($1,$$) <type_name> ® <id> #type_reference($1,$$)
15
Pre-defined type variable declaration (2/6)
example -- I, J : integer ; <vdl> <il>6 : #vd <t> ; ID #pi <ilt>2,5 #c($2,$$) <tn> #c($1,$$) <id>3 #ni <ilt>4 <id>7 #tr #c($3,$$) l <id>1 #si , 1 knd Id id I 2 knd IdList f_id 3 knd Id id J 4,5,6 knd IdList f_id 7 knd Id id integer I Ù J I Ù
16
Pre-defined type variable declaration (3/6)
/* <type_name> ® <id> #type_reference($1,$$) */ type_reference (<id>) => <type_name> { Find <id>.id.id in the symbol table if (it is there && its attrs.class == TYPENAME) <type_name> ¬ (struct type_ref) { .object_type = its attrs.id_type }; else <type_name>.record_kind = ERROR; }
17
Initial symbol table with pre-defined type descriptor ST1 name attrs
float integer offset = 0 A1 cls id id_tp TypeName A2 cls TypeName TD1 frm sz IntegerType 1 id id_tp TD2 frm FloatType sz 2
18
Pre-defined type variable declaration (4/6)
example -- I, J : integer ; <vdl> <il>6 : #vd <t>9 ; ID #pi <ilt>2,5 #c($2,$$) <tn>8 #c($1,$$) <id>3 #ni <ilt>4 <id>7 #tr #c($3,$$) l <id>1 #si , 7 knd Id id integer 1 knd Id id I 2 knd IdList f_id 3 knd Id id J 4,5,6 knd IdList f_id 8,9 TypeRef knd type_ref o_tp TD1 I Ù J I Ù
19
Pre-defined type variable declaration (5/6)
/* <variable_decl> ® <id_list> : <type> #var_decl($1,$3) ; */ var_decl (<id_list>, <type>) { for (each identifier in <id_list>) { Call enter() to put the identifier in the current scope of the symbol table if (it is already there) { ... } Allocate storage (size = <type>.type_ref.object_type.size) for the variable, recording its offset in the local variable offset The following expression describes the attribute record to be create for the variable: (attributes) { .class = VARIABLE; .id = the id_entry returned by enter(); .id_type = <type>.type_ref.object_type; .var_address = (struct address) { .var_level = current_nesting_level; .var_offset = offset; .indirect = FALSE; }
20
Pre-defined type variable declaration (6/6)
ST1 name attrs float integer I J A1 cls id id_tp TypeName A2 cls id id_tp TypeName TD1 frm sz IntegerType 1 TD2 frm sz FloatType 2 var_addr 1 var_lvl var_off indirect F A4 cls id id_tp Variable var_addr 1 var_lvl var_off indirect F A3 cls id id_tp Variable offset = 2
21
Record type declaration (1/8)
semantic record -- struct record_def { type_descriptor *this_type; address_range next_offset; } ; struct semantic_record { enum semantic_record_kind record_kind; union { . . . struct record_def record_def; /* RECORDDEF */ } ; relevant grammar with action symbols -- <type_decl> ® type <id> is <type_def> #type_decl($2,$4) ; <type_def> ® <record_type_def> #cp($1,$$) <record_type_def> ® record #start_record($2) <comp_list> #end_record($2,$$) end <comp_list> ® #cp($$,$1) <comp_decl> #cp($1,$2) <comp_list_tail> #cp($2,$$) <comp_list_tail> ® #cp($$,$1) <comp_decl> #cp($1,$2) <comp_list_tail> #cp($2,$$) <comp_list_tail> ® l <comp_decl> ® <id_list> : <type_name> #field_decl($1,$3,$$) ;
22
Record type declaration (2/8)
/* <record_type_def> ® record #start_record($2) <comp_list> #end_record($2,$$) end */ start_record(void) => <comp_list> { Create a type descriptor, T, as follows: (type_descriptor) { .form = RECORDTYPE; .size = 0; .fields = create(); .field_list = NULL; } <comp_list> ¬ ( struct record_def) { .this_type = &T; .next_offset = 0; } ; }
23
Record type declaration (3/8)
example -- type R is record X , Y : integer ; end ; <tdl> <id>1 is #td <td> ; type <rtd> #c($1,$$) <cl>2 #er #c($$,$1) end #sr record <cd>3 #c($1,$2) <clt> #c($2,$$) l #fd <tn>5 : <il>4 ST2 name attrs TD3 frm sz RecordType fld list Ù 4 knd IdList f_id Y X Ù 2,3 knd RecordDef record_def this_tp nxt_off 1 knd Id id R 5 TypeRef knd type_ref o_tp TD1
24
Record type declaration (4/8)
/* <comp_decl> ® <id_list> : <type_name> #field_decl($1,$3,$$) ; */ field_decl(<comp_decl>, <id_list>, <type_name>) => <comp_decl> { for (each identifier in <id_list>) { Enter it in the symbol table pointed by <comp_decl>.record_def.this_type.fields if (it is already there) { ... } The following expression describes the attribute record to be created for the field: (attributes) { .class = FIELD; .id = the id_entry returned by enter(); .id_type = <type_name>.type_ref.object_type; .field_offset = <comp_decl>.record_def.next_offset; .next_field = NULL; } /* Allocate space for the field with the record. */ <comp_decl>.record_def.next_offset += <type_name>.type_ref.object_type.size Add this attribute record to the end of the list referenced by <comp_decl>.record_def.this_type.field_list
25
Record type declaration (5/8)
example -- type R is record X , Y : integer ; end ; <tdl> <id> is #td <td> ; type <rtd> #c($1,$$) <cl>8 #er #c($$,$1) end #sr record <cd>6 #c($1,$2) <clt>7 #c($2,$$) l #fd <tn>5 : <il>4 ST2 name Y X attrs TD3 frm sz RecordType fld list A5 cls id id_tp Field f_off next Ù A6 cls id id_tp Field f_off next 1 4 knd IdList f_id 5 TypeRef knd type_ref o_tp TD1 6,7,8 knd RecordDef record_def this_tp nxt_off 2 Y X Ù TD1
26
Record type declaration (6/8)
/* <record_type_def> ® record #start_record($2) <comp_list> #end_record($2,$$) end */ end_record (<comp_list>) => <record_type_def> { type_descriptor *T; T ¬ <comp_list>.record_def.this_type; T.size ¬ <comp_list>.record_def.next_offset; <record_type_def> ¬ (struct type_ref) { .object_type = T; } ; } /* <type_decl> ® type <id> is <type_def> #type_decl($2,$4) ; */ type_decl (<id>, <type_def>) The identifier is entered into the symbol table with the following associated attributes record: (attributes) { .class = TYPENAME; .id = the id_entry returned by enter(); .id_type = <type_def>.type_ref.object_type; };
27
Record type declaration (7/8)
example -- type R is record X , Y : integer ; end ; <tdl> ST2 name Y X attrs TD3 frm sz RecordType 2 fld list type <id> is <td>10 #td ; <rtd>9 #c($1,$$) record #sr <cl> #er end #c($$,$1) <cd> #c($1,$2) <clt> #c($2,$$) A5 cls id id_tp Field f_off next Ù A6 cls id id_tp Field f_off next 1 <il> : <tn> #fd ; l 9,10 TypeRef knd type_ref o_tp TD1
28
Record type declaration (8/8)
ST1 name attrs R ST2 name Y X attrs A7 cls id id_tp TypeName TD3 frm sz RecordType 2 fld list A5 cls id id_tp Field f_off next Ù A6 cls id id_tp Field f_off next 1 TD1 offset = 2
29
User-defined type variable declaration (1/2)
example -- ARecord : R ; 1 knd IdList f_id <vdl> <il>1 : #vd <t>4 ; <tn>3 #c($1,$$) <id>2 #tr #pi ID ARecord Ù 2 knd Id id R 3,4 TypeRef knd type_ref o_tp TD3
30
User-defined type variable declaration (2/2)
ST1 name attrs R ARecord ST2 name Y X attrs A7 cls id id_tp TypeName TD3 frm sz RecordType 2 fld list var_addr 1 var_lvl var_off indirect 2 F A8 cls id id_tp Variable A5 cls id id_tp Field f_off next Ù A6 cls id id_tp Field f_off next 1 TD1 offset = 4
31
Static array declaration (1/7)
semantic record -- struct range { long lower, upper; }; struct semantic_record { enum semantic_record_kind record_kind; union { . . . struct range range; /* RANGE */ } ; relevant grammar with action symbols -- <type> ® <type_def> #cp($1,$$) <type_def> ® <array_type_def> #cp($1,$$) <array_type_def> ® array ( <static_range> ) of <type> #array_def($3,$6,$$) <static_range> ® INTL #lower_bound($1,$$) .. INTL #upper_bound($3,$$)
32
Static array declaration (2/7)
/* <static_range> ® INTL #lower_bound($1,$$) .. INTL #upper_bound($3,$$) */ upper_bound(<static_range>, INTL) => <static_range> { <static_range>.range.upper ¬ the value of INTL if (<static_range>.range.upper < <static_range>.range.lower) { ... } } /* <array_type_def> ® array ( <static_range> ) of <type> #array_def($3,$6,$$) */ array_def (<static_range>, <type>) => <array_type_def> Create a new type descriptor for an array type: T ¬ (type_descriptor) { .form = ARRAYTYPE; .element_type = <type>.type_ref.object_type; .bounds = <static_range>.range; .size = .element_type->size * (<static_range>.range.upper - <static_range>.range.lower + 1); } <array_type_def> ¬ (struct type_ref){ .object_type = T; }
33
Static array declaration (3/7)
example -- A1 : array ( ) of R ; <vdl> <il>1 : #vd <t>7 ; <td>6 #c($1,$$) <atd>5 ) <sr>2,3 ( array of <t>4 #ad .. #lb INT #ub TD4 frm sz ArrayType 20 bounds lower upper 1 10 e_tp TD3 1 knd IdList f_id 2 knd Range range lower upper 1 3 knd Range range lower upper 10 1 4 TypeRef knd type_ref o_tp TD3 5,6,7 TypeRef knd type_ref o_tp A1 Ù
34
Static array declaration (4/7)
name attrs A1 var_addr 1 var_lvl var_off indirect 4 F A9 cls id id_tp Variable TD4 frm sz ArrayType 20 bounds lower upper 1 10 e_tp TD3 offset = 24
35
Static array declaration (5/7)
example -- A2 : array ( ) of array ( ) of integer ; <vdl> <il>1 : #vd <t> ; <td> #c($1,$$) <atd> ) <sr>2 ( array of <t>7 #ad .. #lb INT #ub <td>6 <atd>5 <sr>3 <t>4 1 knd IdList f_id 2 knd Range range lower upper 6 4 A2 Ù 5,6,7 TypeRef knd type_ref o_tp 3 knd Range range lower upper 12 8 TD5 frm sz ArrayType 5 bounds lower upper 8 12 e_tp TD1 4 TypeRef knd type_ref o_tp TD1
36
Static array declaration (6/7)
example -- A2 : array ( ) of array ( ) of integer ; <vdl> <il>1 : #vd <t>10 ; <td>9 #c($1,$$) <atd>8 ) <sr>2 ( array of <t>7 #ad .. #lb INT #ub <td> <atd> <sr> <t> 1 knd IdList f_id 2 knd Range range lower upper 6 4 A2 Ù 8,9,10 TypeRef knd type_ref o_tp 5,6,7 TypeRef knd type_ref o_tp TD6 frm sz ArrayType 15 bounds lower upper 4 6 e_tp TD5 frm sz ArrayType 5 bounds lower upper 8 12 e_tp TD1
37
Static array declaration (7/7)
name attrs A1 A2 var_addr 1 var_lvl var_off indirect 4 F A9 cls id id_tp Variable TD4 frm sz ArrayType 20 bounds lower upper 1 10 e_tp TD3 TD1 var_addr 1 var_lvl var_off indirect 24 F A10 cls id id_tp Variable TD6 frm sz ArrayType 15 bounds lower upper 4 6 e_tp TD5 frm sz ArrayType 5 bounds lower upper 8 12 e_tp offset = 39
38
Relevant grammar with action symbols
<variable_decl> ® <id_list> : <type> #var_decl($1,$3) ; <type_decl> ® type <id> is <type_def> #type_decl($2,$4) ; <id_list> ® <id> #start_id_list($1,$2) <id_list_tail> #cp($2,$$) <id_list_tail> ® , <id> #next_id($$,$2,$3) <id_list_tail> #cp($3,$$) <id_list_tail> ® l <id> ® IDENTIFIER #process_id($$) <type> ® <type_name> #cp($1,$$) <type> ® <type_def> #cp($1,$$) <type_name> ® <id> #type_reference($1,$$) <type_def> ® <record_type_def> #cp($1,$$) <type_def> ® <array_type_def> #cp($1,$$) <record_type_def> ® record #start_record($2) <comp_list> #end_record($2,$$) end <comp_list> ® #cp($$,$1) <comp_decl> #cp($1,$2) <comp_list_tail> #cp($2,$$) <comp_list_tail> ® #cp($$,$1) <comp_decl> #cp($1,$2) <comp_list_tail> #cp($2,$$) <comp_list_tail> ® l <comp_decl> ® <id_list> : <type_name> #field_decl($1,$3,$$) ; <array_type_def> ® array ( <static_range> ) of <type> #array_def($3,$6,$$) <static_range> ® INTL #lower_bound($1,$$) .. INTL #upper_bound($3,$$)
39
Advanced features variable and constant declarations enumeration types
subtypes dynamic array types variant records pointer types packages QUIZ: how?
40
QUIZ QUIZ: Term Project
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.