Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Symbol Table.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 Languages and Compilers (SProg og Oversættere) Code Generation.
1 Compiler Construction Intermediate Code Generation.
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch. 3: Compilation Spring 2007 Marco Valtorta.
1 Languages and Compilers (SProg og Oversættere) Lecture 2 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch. 3: Compilation Spring 2008 Marco Valtorta.
Tutorial 6 & 7 Symbol Table
1 Languages and Compilers (SProg og Oversættere) Lecture 6 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.1 Spring 2010 Marco Valtorta
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
CCSA 221 Programming in C CHAPTER 2 SOME FUNDAMENTALS 1 ALHANOUF ALAMR.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Imperative Programming
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Compilation (Chapter 3) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
COP4020 Programming Languages
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Compiler Construction
7-1 7 Contextual analysis  Aspects of contextual analysis  Scope checking  Type checking  Case study: Fun contextual analyser  Representing types.
Chapter 8 - Control II: Procedures and Environments
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
Introduction (Chapter 1) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Language Translation A programming language processor is any system that manipulates programs expressed in a PL A source program in some source language.
1 Languages and Compilers (SProg og Oversættere) Lecture 6 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Review (Chapter 9) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Bernd Fischer RW713: Compiler and Software Language Engineering.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
1 Languages and Compilers (SProg og Oversættere) Semantic Analysis.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 9 Symbol Table and Attributed Grammars
Chapter 7: User-Defined Functions II
Type Checking, and Scopes
Semantic Analysis with Emphasis on Name Analysis
Languages and Compilers (SProg og Oversættere)
CS 536 / Fall 2017 Introduction to programming languages and compilers
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Introduction to Data Structure
Course Overview PART I: overview material PART II: inside a compiler
Course Overview PART I: overview material PART II: inside a compiler
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Course Overview PART I: overview material PART II: inside a compiler
Presentation transcript:

Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside a compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion 8Interpretation 9Review

Contextual Analysis (Chapter 5) 2 The “Phases” of a Compiler Syntax Analysis Contextual Analysis Code Generation Source Program Abstract Syntax Tree Decorated Abstract Syntax Tree Object Code Error Reports

Contextual Analysis (Chapter 5) 3 Multi Pass Compiler Compiler Driver Syntactic Analyzer calls Contextual AnalyzerCode Generator calls Dependency diagram of a typical Multi Pass Compiler: A multi pass compiler makes several passes over the program. The output of a preceding phase is stored in a data structure and used by subsequent phases. input Source Text output AST input output Decorated AST input output Object Code This chapter

Contextual Analysis (Chapter 5) 4 Recap: Contextual Constraints Syntax rules alone are not enough to specify the format of well-formed programs. Example 1: let const m~2 in putint(m + x) Example 2: let const m~2 ; var n:Boolean in begin n := m<4; n := n+1 end Undefined! Scope Rules Type error! Type Rules

Contextual Analysis (Chapter 5) 5 Contextual Analysis For a “typical” programming language, we have two kinds of contextual constraints that will be verified at compile time: –Scope Rules => Identification –Type Rules => Type checking What do we mean by a “typical” programming language? Do there exist programming languages that are not typical, for which scope and/or type rules are not verified at compile time?

Contextual Analysis (Chapter 5) 6 Recap: Contextual Analysis -> Decorated AST Program LetCommand SequentialDeclaration n Ident SimpleT VarDecl SimpleT VarDecl Integer c Charc‘&’ nn +1 Ident OpChar.LitInt.Lit SimpleV Char.Expr SimpleV VNameExpInt.Expr AssignCommand BinaryExpr SequentialCommand AssignCommand :char :int result of identification :type result of type checking Annotations: :int SimpleV

Contextual Analysis (Chapter 5) 7 Identification Table The identification table (also often called symbol table) is a dictionary-style data structure that somehow stores identifier names and relates each identifier to its corresponding attributes. Typical operations: –Empty the table –Add an entry (Identifier -> Attribute) –Find an entry for an identifier

Contextual Analysis (Chapter 5) 8 Identification Table The organization of the identification table depends on the programming language. Different kinds of “block structure” in languages: –Monolithic block structure: e.g. BASIC, COBOL (single block) –Flat block structure: e.g. Fortran (partition program into blocks) –Nested block structure: e.g. C, C++, Java, Algol, Pascal, Scheme, … (as in modern “block-structured” programming languages, each block might contain other blocks) block = an area of text in the program that corresponds to some kind of boundary for the visibility of identifiers. block structure = the textual relationship between blocks in a program.

Contextual Analysis (Chapter 5) 9 Different kinds of Block Structure... a picture MonolithicFlatNested

Contextual Analysis (Chapter 5) 10 Monolithic Block Structure A language exhibits monolithic block structure if the only block is the entire program. => Every identifier is visible throughout the entire program Very simple scope rules: No identifier may be declared more than once For every applied occurrence of an identifier I there must be a corresponding declaration. Monolithic

Contextual Analysis (Chapter 5) 11 Flat Block Structure A language exhibits flat block structure if the program can be subdivided into several disjoint blocks There are two scope levels: global or local. Typical scope rules: a globally defined identifier may be redefined locally several local definitions of a single identifier may occur in different blocks (but not in the same block) For every applied occurrence of an identifier there must be either a local declaration within the same block or a global declaration. Flat

Contextual Analysis (Chapter 5) 12 Nested Block Structure A language exhibits nested block structure if blocks may be nested one within another (typically with no upper bound on the level of nesting that is allowed). There can be any number of scope levels (depending on the level of nesting of blocks). Typical scope rules: no identifier may be declared more than once within the same block (at the same level). for any applied occurrence there must be a corresponding declaration, either within the same block or in a block in which it is nested. Nested

Contextual Analysis (Chapter 5) 13 Identification Table For a typical programming language, i.e. a statically scoped language with nested block structure, we can visualize the structure of all scopes within a program as a kind of tree. Global A B A1 A2 A3 Global AB A1A2A3 = “direction” of identifier lookup Lookup path for an applied occurrence in A3 At any one time (in analyzing the program) only a single path on the tree is accessible. => We don’t necessarily need to keep the whole “scope” tree in memory all the time.

Contextual Analysis (Chapter 5) 14 Identification Table public class IdentificationTable { /** Add an entry to the identification table, associating identifier id with attribute attr at the current level */ public void enter(String id, Attribute attr) {... } /** Retrieve a previously added entry. Returns null when no entry for this identifier is found */ public Attribute retrieve(String id) {... } /** Add a new deepest nesting level to the identification table */ public void openScope( ) {... } /** Remove the deepest scope level from the table, and delete all entries associated with it */ public void closeScope( ) {... }... public class IdentificationTable { /** Add an entry to the identification table, associating identifier id with attribute attr at the current level */ public void enter(String id, Attribute attr) {... } /** Retrieve a previously added entry. Returns null when no entry for this identifier is found */ public Attribute retrieve(String id) {... } /** Add a new deepest nesting level to the identification table */ public void openScope( ) {... } /** Remove the deepest scope level from the table, and delete all entries associated with it */ public void closeScope( ) {... }...

Contextual Analysis (Chapter 5) 15 Identification Table: Example LevelIdentAttr 1a(1) 1b(2) LevelIdentAttr 1a(1) 1b(2) 2b(3) 2c(4) LevelIdentAttr 1a(1) 1b(2) 2d(5) 2e(6) LevelIdentAttr 1a(1) 1b(2) 2d(5) 2e(6) 3x(7) let var a: Integer; var b: Boolean in begin... let var b: Integer; var c: Boolean in begin... end... let var d: Boolean; var e: Integer in begin let const x~3 in... end

Contextual Analysis (Chapter 5) 16 Attributes public void enter(String id, Attribute attr) {... } public Attribute retrieve(String id) {... } public void enter(String id, Attribute attr) {... } public Attribute retrieve(String id) {... } What are these attributes? (Or in other words: What information do we need to store about identifiers?) To understand what information needs to be stored, we must first understand what the information will be used for! Checking Scope Rules Checking Type Rules What information is required by each of these two sub-phases and where does it come from?

Contextual Analysis (Chapter 5) 17 Attributes Example 1: let const m~2 in putint(m + x) Example 2: let const m~2 ; var n:Boolean in begin n := m<4; n := n+1 end Undefined! Scope Rules Type error! Type Rules The ID table needs to provide information needed for Checking Scope Rules Checking Type Rules

Contextual Analysis (Chapter 5) 18 Attributes The ID table needs to provide information needed for Checking Scope Rules Checking Type Rules To check scope rules, all we need to know is whether or not a corresponding declaration exists. To check type rules, we need to be able to find the type of each applied occurrence. One possible solution is to enter type information into the ID table. ==> Attribute = type information. This may be sufficient for simple languages, but not for more complex languages.

Contextual Analysis (Chapter 5) 19 Attributes: Example 1: Mini-Triangle attributes Mini Triangle is very simple: there are only two kinds of declarations single-Declaration ::= const Identifier ~ Expression | var Identifier : Type-denoter single-Declaration ::= const Identifier ~ Expression | var Identifier : Type-denoter... and only two types of values: BOOL or INT public class Attribute { public static final byte CONST = 0, VAR = 1, // two kinds of declaration BOOL = 0, INT = 1; // two types byte kind; // either CONST or VAR byte type; // either BOOL or INT } public class Attribute { public static final byte CONST = 0, VAR = 1, // two kinds of declaration BOOL = 0, INT = 1; // two types byte kind; // either CONST or VAR byte type; // either BOOL or INT }

Contextual Analysis (Chapter 5) 20 Attributes: Example 2: Triangle attributes Triangle is more complex than Mini Triangle => more kinds of declarations and types public abstract class Attribute {... } public class ConstAttribute extends Attribute {...} public class VarAttribute extends Attribute {...} public class ProcAttribute extends Attribute {...} public class FuncAttribute extends Attribute {...} public class TypeAttribute extends Attribute {...} public abstract class Type {...} public class BoolType extends Type {...} public class CharType extends Type {...} public class IntType extends Type {...} public class ArrayType extends Type {...} public class RecordType extends Type {...} public abstract class Attribute {... } public class ConstAttribute extends Attribute {...} public class VarAttribute extends Attribute {...} public class ProcAttribute extends Attribute {...} public class FuncAttribute extends Attribute {...} public class TypeAttribute extends Attribute {...} public abstract class Type {...} public class BoolType extends Type {...} public class CharType extends Type {...} public class IntType extends Type {...} public class ArrayType extends Type {...} public class RecordType extends Type {...}

Contextual Analysis (Chapter 5) 21 Attributes: Pointers to Declaration AST’s Mini Triangle is very simple, but in a more realistic language the attributes can be quite complicated (many different kinds of identifiers and many different types of values) => The implementation of “attributes” can become much more complex and tedious. Observation: The declarations of identifiers provide the necessary information for attributes. => For some languages, a practical way to represent attributes is simply as pointers to the AST-subtree of the actual declaration of an identifier.

Contextual Analysis (Chapter 5) 22 Attributes as pointers to Declaration AST’s Program LetCommand Ident VarDecl xint Ident SequentialDecl VarDecl abool Ident LetCommand VarDecl yint Ident Id table LevelIdentAttr 1 x 1 a 2 y