Compilers Topi 4 The ol Tle nd Blok Struture PART II Mik O Donnell: mihel.odonnell@um.es Alfonso Orteg: lfonso.orteg@um.es Topi 2: Blok Struture 2 1
ol tles with lok strutures Blok Struture Progrmming lnguges with lok strutures: Those lnguges in whih some identifiers only exist inside some setions of the ode, nd not in others. For instne, Algol, PL/I, C, Jv, Psl Exmple of progrm with loks: { int, d; { flot d, f; L1: { int i, h; L2: { int ; 3 ol tles with lok strutures Blok Struture Sopes ssoited with eh line of ode: Every line of ode is inside set of loks or sopes. Eh identifier is only vlid within its ssoited loks. Current sope: the sope defined y the deepest lok of the urrent line of ode. Open sopes: All the sopes tht inlude line of ode (diretly, or through other inner sopes) re sid to e open with respet to tht line. Closed sopes: All the sopes tht do not inlude line of ode (diretly, or indiretly) re sid to e losed with respet to tht line. Sope = Amito o loque 4 2
ol tles with lok strutures Blok Struture Exmple { int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; Sope 2 Sope 3 Sope 1 5 ol tles with lok strutures Conepts Rules to determine whih identifiers re tive: An identifier is tive if: It is defined in the urrent sope, or It is defined in sope whih ontins the urrent one. The vrile is only vlid AFTER the point of delrtion A vrile delrtion pplies for ny lok whih is emedded within the lok where the vrile is defined, nd strts fter the delrtion. Whenever n identifier is delred oth in this sope, nd in prent sope, then the delrtion in the urrent sope is tken. The nmes of the rguments of proedure re lol to tht proedure, nd they re not essile from outside. The sme identifier nnot e defined twie t the sme level. 6 3
Topi 3: Use of ol Tles with Blok Struture 7 ol tles with lok strutures In the following, we re ssuming tht we re operting within syntti nlysis of the progrm (nd sometimes semnti nlysis) This is euse sopes (lok struture) nnot e identified t the time of lexil nlysis, whih only sees one token t time, not how these tokens relte to eh other. 8 4
ol tles with lok strutures In lnguges without soping, single symol tle suffies: An identifier lwys refers to the sme vrile (or proedure) However, when emedded sopes re possile, the informtion ssoited with n identifier is prtiulr to the sopes it is delred within E.g., fred might refer to string in one lok nd n int in nother We thus need to store nd retrieve informtion out symols reltive to the set of sopes tht the urrent line is within. Two solutions: A distint symol tle for eh sope, giving informtion only out identifiers visile in tht sope. A single symol tle, whih detils, for eh identifier, whih sopes it ours in, nd its ttriutes in tht sope (e.g. its dttype) 9 ol tles with lok strutures One symol tle per sope One symol tle per sope : the si pproh: Eh lok is given seprte symol tle. When line of ode delres vrile (e.g., in the rgument list of proedure delrtion, or in n expliit type delrtion), n entry is reted in the urrent sope s symol tle. When line of ode refers to vrile, the ompiler first tries to retrieve the identifier s reord from the urrent symol tle. If not found, the symol tle of the prent sope is tried This is repeted until n entry is found in n enlosing sope. If none found, then the vrile is referened without delrtion report error (ssuming type delrtion is required). 10 5
{ int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope Blok 2 Blok 3 Blok 1 11 { int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope Blok 2 Blok 3 Blok 1 Event: enter new lok Ation: Crete ol Tle 12 6
{ int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope Blok 2 Blok 3 Blok 1 Event: enter new lok Ation: Crete ol Tle 13 { int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope Blok 2 Blok 3 Blok 1 Event: reognise type de Ation: Add to urrent symol tle No No No 14 7
{ int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope Blok 2 Blok 3 Blok 1 Event: vrile ssignment Query: Found in urrent ST? Ation: Mrk s initited No No 15 { int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope Blok 2 Blok 3 Blok 1 Event: vrile ssignment Query: Found in urrent ST? Ation: Mrk s initited No 16 8
{ int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope Blok 2 Blok 3 Blok 1 Event: vrile ssignment Query: Found in urrent ST? Ation: Mrk s initited 17 { int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope Blok 2 Blok 3 Blok 1 Event: Enter new lok Ation: Crete new Tle ST2 18 9
{ int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope Blok 2 Blok 3 Blok 1 Event: reognise type de Ation: Add to urrent symol tle ST2 d No No 19 { int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope Blok 2 Blok 3 Blok 1 Event: vrile ssignment () Query: Found in urrent ST? Ation: Mrk s initited ST2 d No 20 10
{ int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope Blok 2 Blok 3 Blok 1 ST2 Event: vrile referene to Query: Found in urrent ST? NO Query: Found in prent ST? YES Query: Initited in prent ST? YES d No 21 ol tles with lok strutures One symol tle per sope Note: the previous exmple demonstrtes semnti heking When n identifier is found: Vrile Delrtion: i) Return error if not first delrtion of vrile in urrent sope ii) Else, dd entry for vrile in urrent symol tle Vrile Assignment: i) Find the first sope in whih the vrile is delred ii) Return error if not delred in sope iii) Set the STATE to initited Vrile Referene: i) Find the first sope in whih the vrile is delred ii) Return error if the vrile is not delred nd initited 22 11
ol tles with lok strutures One symol tle per sope : single pss One pss ompilers: Sometimes the ompiler mkes single pss over the input token strem, nd sometimes two or more psses re mde. If single pss, then one sope is exited, the symol tle of tht sope will not e referened gin. The symol tle of losed sope n thus e lered from memory (for memory effiieny). Therefore, in one-pss ompilers, the list of open sopes n e implemented s LIFO stk. 23 { int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope : single pss Blok 2 Blok 3 Blok 1 ST2 d No 24 12
{ int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope : single pss Blok 2 Blok 3 Blok 1 ST2 d 25 { int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope : single pss Blok 2 Blok 3 Blok 1 ST2 d 26 13
{ int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope : single pss Blok 2 Blok 3 Blok 1 27 { int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope : single pss Blok 2 Blok 3 Blok 1 ST3 28 14
Insertion lgorithm: ol tles with lok strutures One symol tle per sope: single pss sttus insert(id key, SopeTle T) { if exists(key, urrent_sope(t) return FAIL; insert_key( key, urrent_sope(t) ); 29 ol tles with lok strutures One tle per sope, single pss Serh lgorithm: Hsh_entry serh(id key, SopeTle T) { HshTle symtle; symtle = urrent_sope(t); while (not empty_hsh_tle(symtle)) { If exists(key, symtle) return symtle; symtle=next_tle(symtle,t); return NULL; 30 15
ol tles with lok strutures One tle per sope: multiple psses Multiple Pss Compilers: However, if the ompiler mkes multiple psses, then eh sope s symol tle needs to e preserved for lter user. One nnot use LIFO stk. One solution: Dt struture to reord struture of sopes (e.g., n ojet for eh sope, with pointer to its prent sope) Eh sope ojet lso hs pointer to the symol tle for tht sope. 31 ol tles with lok strutures One symol tle per sope: multiple psses { int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; Blok 2 Blok 3 Blok 1 ST2 d 32 16
{ int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope : multiple psses Blok 2 Blok 3 Blok 1 ST2 d 33 { int,, ; = 0; = 0; = 0; { int, d; = + 1; d = ; { string ; int ; print ; ol tles with lok strutures One symol tle per sope : multiple psses Blok 2 Blok 3 Blok 1 ST2 d ST3 34 17
ol tles with lok strutures One symol tle per sope : multiple psses Multiple Pss Compilers: Note tht the set of symol tles is not stored s stk. Better to use tree struture, eh symol tle pointing to the symol tle for the sope tht ontins it. Open sopes = urrent sope plus ll prent sopes k to the root. ST2 ST4 ST3 ST4 35 Topi 4: Deling with Blok Struture with Single ol Tle 36 18
ol tles with lok strutures Just one symol tle An lterntive is to use just single hsh tle. All the identifiers will e inserted in the sme tle. Eh identifier will hve informtion out the sope to whih it elongs. Possile implementtion: Using An rry for the dt out the loks. Other for the identifiers. It my e just one struture tht uses the end for the identifiers of the open loks, nd the eginning for the ids. of the losed ones. On the other hnd, there might e two: one for the open sopes nd one for the losed sopes. Eh lok will ontin the following informtion: Blok numer. Numer of the prent lok. Numer of identifiers in this lok. Pointer to the informtion of the identifiers in this lok. 37 ol tles with lok strutures Multiple sopes, Just one symol tle The prolem: Eh hsh tle requires memory overhed Thus using hsh tle for eh sope is memory ineffiient We my e required to serh in multiple symol tles, Thus less effiient for speed s well Solution: Use single hsh tle for ll sopes Less memory overhed (perhps) Only one serh required. 38 19
ol tles with lok strutures Multiple sopes, Just one symol tle Solution 1: Eh sope hs unique numer 1. Strt sope_id = 0 (for glol sope) 2. For eh new sope, inrement sope_id When entering n identifier into the symol tle, use identifier,sope_id s the key. To find the symol tle entry for n identifier 1. Append the urrent sope_id to the identifier nd look for tht 2. If tht fils, try the prent sope, et. until it is found BUT still my require multiple serhes! 39 ol tles with lok strutures Multiple sopes, Just one symol tle Solution 2: identifier returns linked list of usges of identifier ol tle: Key = identifier Vlue = linked list of pointers to reords, one for eh usge of the identifier. Sope_id Dt pointer A 12 11 ol dt 40 20
ol tles with lok strutures Multiple sopes, Just one symol tle If single pss ompiler, 1. Eh new insertion of n identifier is t front of the linked list 2. A seprte struture keeps trk of ll identifiers defined in the urrent sope 3. When exiting the sope, one uses the list in (2) to delete the frontmost element in eh linked list. A 12 11 41 ol tles with lok strutures Multiple sopes, Just one symol tle If single pss ompiler, 1. Eh new insertion of n identifier is t front of the linked list 2. A seprte struture keeps trk of ll identifiers defined in the urrent sope 3. When exiting the sope, one uses the list in (2) to delete the frontmost element in eh linked list. A 11 42 21
ol tles with lok strutures Multiple sopes, Just one symol tle Using this pproh: ll entries on the symol tle re in open sopes (potentilly ville within the urrent lok of ode) The front item on the linked list is lwys the pproprite one in the urrent sope (it ws either defined in the urrent sope, or in prent sope of the urrent sope) A 11 43 Summry of ol Tles 1. ol tle for lnguges without lok struture 2. ol tle for lnguges with lok struture 1. One Tle per sope: single pss 2. One Tle per sope: Multiple Pss 3. One Tle for ll sopes 44 22
Pst Exm Questions 45 During the semnti nlysis, the tle of symols is used to verify tht the identifiers re used in n pproprite wy. Wht semnti verifitions re rried out using the tle of symols when: ) se delr un vrile (por ejemplo, int v;) ) se le sign vlor un vrile (por ejemplo, v = 7;) ) se he refereni un vrile (por ejemplo, w = v * 8;) 46 23
PAST EXAM QUESTIONS 1. Ddo el siguiente progrm { // Líne 1 int x,y; // Líne 2 { // Líne 3 int z; // Líne 4 { // Líne 5 int p; // Líne 6 // Líne 7 int q; // Líne 8 { // Líne 9 int r; // Líne 10 r = z + 3; // Líne 11 // Líne 12 int s; // Líne 13 // Líne 14 { // Líne 15 int t; // Líne 16 // Líne 17 // Líne 18 () Mostrr el ontenido de l tl de símolos después de ompilr d un de ls línes mrds on suponiendo que se utiliz un ompildor de un psd y un tl de símolos por loque. Los loques deen nomrrse utilizndo l notión Bn, donde n es un numero distinto pr d loque. Pr d identifidor solo dee inluirse su nomre. () Al ompilr l instruión de l líne 8 uánts operiones de úsqued se llevrán o y en qué orden? Espeifir pr d un de ells el motivo de l úsqued y qué loque orresponde l tl en l que se reliz l úsqued. () Al ompilr l instruión de l líne 11 uánts operiones de úsqued se llevrán o y en qué orden? Espeifir pr d un de ells el motivo de l úsqued y qué loque orresponde l tl en l que se reliz l úsqued. 47 1. Se dese relizr un ompildor de un psd pr el lenguje ASPLE que está estudindo en ls prátis y que, por lo tnto, dmite funiones. Pr su tl de símolos se v utilizr un tl hsh pr d ámito. Considere los siguientes progrms ASPLE. Pr d uno de ellos se le pide: egin int ; int ; funtion int FRr(int ; int ) egin int ; # (1) input ; return ++ end # (2) input ; input ; output FRr(,) end egin int ; int ; funtion int FRr(int ) egin int ; # (1) input ; return ++ end # (2) input ; input ; output FRr() end (1) Indir explíitmente los errores de ompilión, en el so de que se produzn. Explique revemente l us de los mismos. (2) Si los progrms son orretos, muestre el ontenido de ls tls de símolos (omplets) en los puntos mrdos on (1) y (2). Explique revemente el ontenido de ls tls. 48 24