Declarations Presented by Sangeetha R ( BP150501) Sacred Heart College Tirupattur
Declarations Declaration in a procedure is examined, can layout storage for name local to the procedure. For each local name, we create a symbol-table entry with information like the type and the relative address of the storage for the name. The relative address consists of an offset from the base of the static area or the field for local data in an activation record.
In this case, a global variable, say offset, can keep of the next available relative address. The procedure enter(name, type, offset) creates a symbol-table entry for name, gives it type and relative address offset in its data area. nonterminals P generates a sequence of declarations of the form id:T . Before the first declarations is considered, offset is set to . Name is entered to symbol table with offset equal to the current value of offset, and offset is incremented by the width of the data object denoted by that name .
Computing the type and relative address of declared names P-> {offset:=0} D D->D:D D->id:T {enter(id.name, T.type, offset); offset:=offset+T.Width} T->Integer {T.type:=integer, T.width:=4} T->real {T.type:=integer, T.width:=8} T->array[num]ofT1 {T.type:=array(num.val,T1.type); T.width:=num.val*T1.Width} T->T1 {T.type:=pointer(T1.type); T.width:=4}
Non terminal generating E, called Marker nonterminals can be used to rewrite productions so that all actions appear at the ends of right sides. P- > M D M- >E {offset:=0}
Keeping track of scope information This will be illustrated by adding semantic rules to the followed language: P- >D D- >D:D|id:T|proc id;D;S The nesting structure of the procedure can be deduced from the links between the symbol tables; The symbol tables for procedures readarray, exchange, and quicksort point back to that for the containing procedure sort, consisting of the entire program. Since partition is declared within quicksort, its table points to that of quicksort.
Symbol table for nested procedures
Semantic rules m ktable(previous ) The argument previous points to a previously created symbol table, presumably that for the enclosing procedure. e nter(table , name, type, offset) Creates a new entry for name in the symbol table pointed to by table. again enter places type and relative address offset in fields within the entry.
addwidth (table, width) records the cumulative width of the all the entries in table in the header associated with this symbol table. e nterproc (table , name, newtable) creates a new entry for procedure name in the symbol table pointed to by table. The argument newtable points to the symbol table for this procedure name.
Fields Names in records T- >record L D end { T.type:=record(top(tblptr)); {T.width:=top(offset) L->E { t:=mktable(nil); L- >E {t:=mktable(nil); push(t , tblptr); push(0,offset )} Setting up a symbol table for field names in a record
After the keyword record is seen, the action associated with the marker L creates a new table for the field names. A pointer to this symbol table is pushed onto stack tblptr and relative address is pushed onto stack offset. The action for D->id:T This pointer will be used in the next section to recover the names, types, and widths of the fields in the record from T.type .