Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Implement a symbol table module for the language P0 described in the picture below: As such, the following procedures must be included: procedure NewObj(var obj:
Implement a symbol table module for the language P0 described in the picture below:
As such, the following procedures must be included:
procedure NewObj(var obj: Object; cls: Class);
procedure OpenScope;
procedure CloseScope;
procedure find (var obj: Object);
You can implement this in a language of your choosing but you must provide a testing program called "test" which runs test cases you have created. An associated script file called "script" must build and run your program. Your program must build and run on a Linux server.
Symbol Table and Nesting d Closing Symbol Table Insertion an We represent the symbol table as a linked list of records. For Due to the nested structure of languages, there can be multiple entries for one identifier in the symbol table .Two additional operations of the symbol table are needed a new scope const N 10: lifetime var x: T: y boolean; SCO result in follewing entries in the symbol table, shewing only the relevent record fields Here we don't bother deallocating the entries properly after closing a scope. procedure OpenScope; var s: Object; begin new ()cls:HeadClass; sdsctopScope; end procedure CloseScope: ome-w begin x 5: p: write(x) The list elweys contains hsoder element. The list is terminated by used as a tentinel for a search. . guard, which Symbol Table and Nesting begin topScope: topScope dsc The symbol table becomes a stack of linked lists. For example, the symbol table at x a7 is, showing only the relevant ficlds: Symbol Table Lookup Symbol Table Insertion Whenever we insert a new identifier, we check if the identifier is alreody present and issue an errar message in that case .Each occurrence of an identi ifier requires a search of the table, starting at the innermost scope and continuing in the next outer scope if the identificr is not found there. A scntinel is used for simplifying the termination condition: topScope ex dsc var universe: Object: (points to the outermost scope) procedure find (var obj begin Objc topScope: Object guard: Ob procedure NewObj (var obj: Object: cls: Class): dsc while true do begin :: 10pScope; guard ^.name :: id: (set sentinel for search) begin x next while x^.next ^.norn o id do x := ^.next: if x" next ; guard then while x^.norme id do :: x.next: rf x o guard then begin obj :2x; exit end; For each variable, we record its level, starting at level zero, the module level. new (n), nname id n elcls; n.nextguard; begin obj:x Mark ('undef') exit end else begin objnext, Mark (mult def') end Entries for Data Types- Standard Identifiers .Symbol Table Types Specification of types in programming kanguoges serves for type checking programs at compile time, and Standard identifiers can b thought of as being declared outside . the module level: - standard constants: truc, falsc standard types: intager, boolean -stendard procedures: read, write, writeln They are entered at parser initializotion in the 'universe' level, eg. g the amount of memory needed by variables. Obi Desc = record cls: Class (either Head, Var, Par, Const, Fld, Typ. Proc, SProc) lev: integer:if classVar, then level of nesting with 0 being The type information has to be entered in the symbol table. Since var a: aay [1 10] of integer we cannot simply have a reference to the declaration of the type, but have to introduce a new deta structure in the symbol table outermost level: variables on outermost (module) level are accessed by absolute addressing: nested variables are accessed by relative addressing) next: Object: (next object in list of objects of the same level; dsc: Object: (if class Head, points to the list of objects at the tp: Typ: if elass Var, Par, Const, Fld, the type of the name: Ident: if elassVar, Par, Const, Fld, Typ, Proe, the name last element in list points to 'guard') next leve objecth of the object, podded with blanks) Form (Bcol, Int, Arry, Rerd) frm: Form: fields: Objeet. [for records) ower, Ien, siza: integer for arrayszein bytes) Entries for Date Types Symbol Table Types val: integer if class Const, then value of the integer or boolean 147 type R record fg: integer end; Table constant, where false and 1: true; if class Typ, then 1boolean and 2 integer: if class Sproc, then 1- read, 2 write, a: array [1 10) of integer results in following symbol toble, showing only the relevent entriestyPe writeln (HeadClass, (head of lists of objects of a scope) VarClass,local variable, global variable, or value parameter) if elass Fld, then offset of that field if class Var, then relative address of the variable reference (negative number for proper variables and positive ConstClass, (integer or boolean constant; includes true, false) FldClass, frecord field) TypClass, type includes integer, boolean) ProcClass, (user-defined procedure) SProcClass, (standard procedure: read, write, writeln: if class -Proc, then -1 when parsing local declarations ize-8 fielda of that procedure, and address of the procedure otherwise (forward calls are not allowed); e 40 if class Par, then relative address of the reference to distinction between uscr-defined and standard procedure-needed for code generation: no code for the dure call is generated for standard p rather RD, WRD instruction is generated inline.) r-Int Symbol Table and Nesting d Closing Symbol Table Insertion an We represent the symbol table as a linked list of records. For Due to the nested structure of languages, there can be multiple entries for one identifier in the symbol table .Two additional operations of the symbol table are needed a new scope const N 10: lifetime var x: T: y boolean; SCO result in follewing entries in the symbol table, shewing only the relevent record fields Here we don't bother deallocating the entries properly after closing a scope. procedure OpenScope; var s: Object; begin new ()cls:HeadClass; sdsctopScope; end procedure CloseScope: ome-w begin x 5: p: write(x) The list elweys contains hsoder element. The list is terminated by used as a tentinel for a search. . guard, which Symbol Table and Nesting begin topScope: topScope dsc The symbol table becomes a stack of linked lists. For example, the symbol table at x a7 is, showing only the relevant ficlds: Symbol Table Lookup Symbol Table Insertion Whenever we insert a new identifier, we check if the identifier is alreody present and issue an errar message in that case .Each occurrence of an identi ifier requires a search of the table, starting at the innermost scope and continuing in the next outer scope if the identificr is not found there. A scntinel is used for simplifying the termination condition: topScope ex dsc var universe: Object: (points to the outermost scope) procedure find (var obj begin Objc topScope: Object guard: Ob procedure NewObj (var obj: Object: cls: Class): dsc while true do begin :: 10pScope; guard ^.name :: id: (set sentinel for search) begin x next while x^.next ^.norn o id do x := ^.next: if x" next ; guard then while x^.norme id do :: x.next: rf x o guard then begin obj :2x; exit end; For each variable, we record its level, starting at level zero, the module level. new (n), nname id n elcls; n.nextguard; begin obj:x Mark ('undef') exit end else begin objnext, Mark (mult def') end Entries for Data Types- Standard Identifiers .Symbol Table Types Specification of types in programming kanguoges serves for type checking programs at compile time, and Standard identifiers can b thought of as being declared outside . the module level: - standard constants: truc, falsc standard types: intager, boolean -stendard procedures: read, write, writeln They are entered at parser initializotion in the 'universe' level, eg. g the amount of memory needed by variables. Obi Desc = record cls: Class (either Head, Var, Par, Const, Fld, Typ. Proc, SProc) lev: integer:if classVar, then level of nesting with 0 being The type information has to be entered in the symbol table. Since var a: aay [1 10] of integer we cannot simply have a reference to the declaration of the type, but have to introduce a new deta structure in the symbol table outermost level: variables on outermost (module) level are accessed by absolute addressing: nested variables are accessed by relative addressing) next: Object: (next object in list of objects of the same level; dsc: Object: (if class Head, points to the list of objects at the tp: Typ: if elass Var, Par, Const, Fld, the type of the name: Ident: if elassVar, Par, Const, Fld, Typ, Proe, the name last element in list points to 'guard') next leve objecth of the object, podded with blanks) Form (Bcol, Int, Arry, Rerd) frm: Form: fields: Objeet. [for records) ower, Ien, siza: integer for arrayszein bytes) Entries for Date Types Symbol Table Types val: integer if class Const, then value of the integer or boolean 147 type R record fg: integer end; Table constant, where false and 1: true; if class Typ, then 1boolean and 2 integer: if class Sproc, then 1- read, 2 write, a: array [1 10) of integer results in following symbol toble, showing only the relevent entriestyPe writeln (HeadClass, (head of lists of objects of a scope) VarClass,local variable, global variable, or value parameter) if elass Fld, then offset of that field if class Var, then relative address of the variable reference (negative number for proper variables and positive ConstClass, (integer or boolean constant; includes true, false) FldClass, frecord field) TypClass, type includes integer, boolean) ProcClass, (user-defined procedure) SProcClass, (standard procedure: read, write, writeln: if class -Proc, then -1 when parsing local declarations ize-8 fielda of that procedure, and address of the procedure otherwise (forward calls are not allowed); e 40 if class Par, then relative address of the reference to distinction between uscr-defined and standard procedure-needed for code generation: no code for the dure call is generated for standard p rather RD, WRD instruction is generated inline.) r-Int
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started