Compilers. Topic 4. The Symbol Table and Block Structure PART II. Mick O Donnell: Alfonso Ortega:

Similar documents
CS 241 Week 4 Tutorial Solutions

Type Checking. Roadmap (Where are we?) Last lecture Context-sensitive analysis. This lecture Type checking. Symbol tables

CS553 Lecture Introduction to Data-flow Analysis 1

INTEGRATED WORKFLOW ART DIRECTOR

CS453 INTRODUCTION TO DATAFLOW ANALYSIS

From Dependencies to Evaluation Strategies

Introduction to Algebra

CS201 Discussion 10 DRAWTREE + TRIES

Table-driven look-ahead lexical analysis

box Boxes and Arrows 3 true 7.59 'X' An object is drawn as a box that contains its data members, for example:

Fig.25: the Role of LEX

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

Error Numbers of the Standard Function Block

Midterm Exam CSC October 2001

Duality in linear interval equations

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

Reducing a DFA to a Minimal DFA

Distributed Systems Principles and Paradigms. Chapter 11: Distributed File Systems

Containers: Queue and List

Lexical Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Midterm 2 Sample solution

CMPUT101 Introduction to Computing - Summer 2002

Lecture 13: Graphs I: Breadth First Search

V = set of vertices (vertex / node) E = set of edges (v, w) (v, w in V)

cisc1110 fall 2010 lecture VI.2 call by value function parameters another call by value example:

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

Lexical analysis, scanners. Construction of a scanner

Distance vector protocol

LINX MATRIX SWITCHERS FIRMWARE UPDATE INSTRUCTIONS FIRMWARE VERSION

COMP 423 lecture 11 Jan. 28, 2008

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Lesson 4.4. Euler Circuits and Paths. Explore This

Distributed Systems Principles and Paradigms

COMPUTER EDUCATION TECHNIQUES, INC. (WEBLOGIC_SVR_ADM ) SA:

Discussion 1 Recap. COP4600 Discussion 2 OS concepts, System call, and Assignment 1. Questions. Questions. Outline. Outline 10/24/2010

Pattern Matching. Pattern Matching. Pattern Matching. Review of Regular Expressions

Problem Final Exam Set 2 Solutions

Paradigm 5. Data Structure. Suffix trees. What is a suffix tree? Suffix tree. Simple applications. Simple applications. Algorithms

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards

10.2 Graph Terminology and Special Types of Graphs

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

Package Contents. Wireless-G USB Network Adapter with SpeedBooster USB Cable Setup CD-ROM with User Guide (English only) Quick Installation

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

c s ha2 c s Half Adder Figure 2: Full Adder Block Diagram

McAfee Web Gateway

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

COSC 6374 Parallel Computation. Non-blocking Collective Operations. Edgar Gabriel Fall Overview

10.5 Graphing Quadratic Functions

COMP108 Algorithmic Foundations

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

WORKSHOP 9 HEX MESH USING SWEEP VECTOR

What are suffix trees?

Computational geometry

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

Symbol Table management

Product of polynomials. Introduction to Programming (in C++) Numerical algorithms. Product of polynomials. Product of polynomials

CS 551 Computer Graphics. Hidden Surface Elimination. Z-Buffering. Basic idea: Hidden Surface Removal

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

WORKSHOP 19 GLOBAL/LOCAL MODELING USING FEM FIELDS

Troubleshooting. Verify the Cisco Prime Collaboration Provisioning Installation (for Advanced or Standard Mode), page

Advanced Programming Handout 5. Enter Okasaki. Persistent vs. Ephemeral. Functional Queues. Simple Example. Persistent vs.

To access your mailbox from inside your organization. For assistance, call:

YOU ARE: AND THIS IS:

Shared Memory Architectures. Programming and Synchronization. Today s Outline. Page 1. Message passing review Cosmic Cube discussion

Dr. D.M. Akbar Hussain

Agenda & Reading. Class Exercise. COMPSCI 105 SS 2012 Principles of Computer Science. Arrays

Calculus Differentiation

Width and Bounding Box of Imprecise Points

Registering as a HPE Reseller. Quick Reference Guide for new Partners in Asia Pacific

Parallelization Optimization of System-Level Specification

6.045J/18.400J: Automata, Computability and Complexity. Quiz 2: Solutions. Please write your name in the upper corner of each page.

Reducing Costs with Duck Typing. Structural

Compilers. Chapter 4: Syntactic Analyser. 3 er course Spring Term. Precedence grammars. Precedence grammars

Greedy Algorithm. Algorithm Fall Semester

Lists in Lisp and Scheme

UT1553B BCRT True Dual-port Memory Interface

CSE 401 Compilers. Agenda. Lecture 4: Implemen:ng Scanners Michael Ringenburg Winter 2013

Agilent Mass Hunter Software

Smart Output Field Installation for M-Series and L-Series Converter

Scope, Functions, and Storage Management

Algorithm Design (5) Text Search

CS 430 Spring Mike Lam, Professor. Parsing

Agilent G3314AA BioConfirm Software

2 Computing all Intersections of a Set of Segments Line Segment Intersection

Data sharing in OpenMP

Introduction To Files In Pascal

Union-Find Problem. Using Arrays And Chains. A Set As A Tree. Result Of A Find Operation

Compression Outline :Algorithms in the Real World. Lempel-Ziv Algorithms. LZ77: Sliding Window Lempel-Ziv

PARALLEL AND DISTRIBUTED COMPUTING

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

CSc 453 Compilers and Systems Software. 6 : Top-Down Parsing I

COSC 6374 Parallel Computation. Dense Matrix Operations

Compilers Spring 2013 PRACTICE Midterm Exam

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

Transcription:

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