انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Sequences of Declarations

Share |
الكلية كلية تكنولوجيا المعلومات     القسم قسم البرامجيات     المرحلة 3
أستاذ المادة نداء عبد المحسن عباس العطوان       11/03/2018 18:24:51
SDT for computing types and their widths is as below.
T ? B C {t=B.type; w=B.width ;}
B ? int {B.type=integer; B.width=4 ;}
B ? float {B.type=float; B.width=8 ;}
C ? ? {C.type=t; C.width=w ;}
C ? [num] C1 {array (num.value, C1.type);
C.width=num.value × C1.width ;}

Syntax-directed translation of array types is shown in the dependency graph below for the type declaration int [2][3].

Sequences of Declarations
The translation scheme below deals with a sequence of declarations of the form T id, where T generates a type. Before the first declaration is considered, offset is set to 0. As each new name x is seen, x is entered into the symbol table with its relative address set to the current value of offset, which is then incremented by the width of the type of x.
P ? {offset =0 ;}
D
D ?T id; {top.put (id.lexem, T.type, offset);
Offset = offset + T.width ;}
D1
D ? ?

The semantic action within the production D ? T id; D1 creates a symbol table entry by executing top.put (id.lexeme, T.type, offset). Here top denotes the current symbol table. The method top.put creates a symbol table entry for id.lexeme, with type T.type and relative address offset in its data area.
Fields in Records and Classes
Since records can essentially have a bunch of declarations inside, we only need add T ? RECORD {D} where the fields in this record type are specified by the sequence of declarations generated by D. For the semantics we need to push the environment and offset onto stacks since the namespace inside a record is distinct from that on the outside. The width of the record itself is the final value of (the inner) offset.
T ? record { { Env.push(top); top = new Env();
Stack.push(offset); offset = 0; }
D } { T.type = record(top); T.width = offset;
top = Env.pop(); offset = Stack.pop(); }

Let class Env implements symbol tables. The call Env.push (top) pushes the current symbol table denoted by top onto a stack. Variable top is then set to a new symbol table. Similarly, offset is pushed onto a stack called Stack. Variable offset is then set to 0. After the declarations in D have been translated, the symbol table top holds the types and relative addresses of the fields in this record. Further, offset gives the storage needed for all the fields. The second action sets T.type to record(top) and T.width to offset. Variables top and offset are then restored to their pushed values to complete the translation of this record type.

Addressing Array Elements

The idea is that you associate the base address with the array name. That is, the offset stored in the identifier table is the address of the first element of the array. The indices and the array bounds are used to compute the amount, often called the offset by which the address of the referenced element differs from the base address.

One Dimensional Array
For one dimensional array, this is especially easy: The address increment is the width of each element times the index (assuming indexes start at 0). So the address of A[i] is the base address of A plus i time the width of each element of A.
The width of each element is the width of what we have called the base type. Base is the relative address of the first location of array A, width is the width of each array element, low is the index of the first array element. A one-dimensional array A:

baseA low i width

baseA is the address of the first location of the array A,
width is the width of each array element.
low is the index of the first array element


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
download lecture file topic