7-1 7 Contextual analysis  Aspects of contextual analysis  Scope checking  Type checking  Case study: Fun contextual analyser  Representing types.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Symbol Table.
8 VM code generation Aspects of code generation Address allocation
Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
1 Compiler Construction Intermediate Code Generation.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Elaboration or: Semantic Analysis Compiler Baojian Hua
Compiler Construction
6-1 6 Syntactic analysis  Aspects of syntactic analysis  Tokens  Lexer  Parser  Applications of syntactic analysis  Compiler generation tool ANTLR.
The Symbol Table Lecture 13 Wed, Feb 23, The Symbol Table When identifiers are found, they will be entered into a symbol table, which will hold.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Tutorial 6 & 7 Symbol Table
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 6: Type Systems Fall 2009.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded.
Chapter 2 A Simple Compiler
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Types I was eventually persuaded of the need to design programming notations so as to maximize.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Attribute Grammars They extend context-free grammars to give parameters to non-terminals, have rules to combine attributes Attributes can have any type,
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
Imperative Programming
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
5-1 5 Compilation  Overview  Compilation phases –syntactic analysis –contextual analysis –code generation  Abstract syntax trees  Case study: Fun language.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as.
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Semantic Analysis Semantic Analysis v Lexically and syntactically correct programs may still contain other errors v Lexical and syntax analyses.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Bernd Fischer RW713: Compiler and Software Language Engineering.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
1 Languages and Compilers (SProg og Oversættere) Semantic Analysis.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 10.
Operational Semantics of Scheme
Lecture 9 Symbol Table and Attributed Grammars
Sixth Lecture ArrayList Abstract Class and Interface
Name Spaces: ALL versus OOL
Constructing Precedence Table
Semantic Analysis with Emphasis on Name Analysis
Compiler Design 22. ANTLR AST Traversal (AST as Input, AST Grammars)
Basic Program Analysis: AST
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Metacircular Evaluator
Course Overview PART I: overview material PART II: inside a compiler
Compiler Construction
6.001 SICP Interpretation Parts of an interpreter
Compiler Construction
Presentation transcript:

7-1 7 Contextual analysis  Aspects of contextual analysis  Scope checking  Type checking  Case study: Fun contextual analyser  Representing types  Representing scopes Programming Languages 3 © 2012 David A Watt, University of Glasgow

7-2 Aspects of contextual analysis  Contextual analysis checks whether the source program (represented by an AST) satisfies the source language’s scope rules and type rules.  Contextual analysis can be broken down into: –scope checking (ensuring that every identifier used in the source program is declared) –type checking (ensuring that every operation has operands with the expected types).

7-3 Example: Fun compilation (1)  Source program: int n = 15 # pointless program proc main (): while n > 1: n = n/2..

7-4 Example: Fun compilation (2)  AST after syntactic analysis (slightly simplified): PROG NOFORMAL DIV ASSN PROC WHILE GT VAR ID ‘n’ INT ‘int’ ID ‘n’ NUM ‘1’ NUM ‘2’ ID ‘n’ NUM ‘15’ ID ‘n’ ID ‘main’

7-5 Example: Fun compilation (3)  AST after contextual analysis: Type table (simplified) ‘n’ INT ‘main’ VOID → VOID :BOOL :INT PROG DIV ASSN PROC WHILE GT ID ‘n’ INT ‘int’ ID ‘n’ NUM ‘1’ NUM ‘2’ ID ‘n’ NUM ‘15’ ID ‘n’ ID ‘main’ NOFORMAL VAR

7-6 Scope checking (1)  Scope checking is the collection and dissemination of information about declared identifiers.  The contextual analyser employs a type table. This contains the type of each declared identifier. E.g.: ‘n’BOOL ‘fac’INT → INT ‘main’INT → VOID

7-7 Scope checking (2)  Wherever an identifier is declared, put the identifier and its type into the type table. –If the identifier is already in the type table (in the same scope), report a scope error.  Wherever an identifier is used (e.g., in a command or expression), check that it is in the type table, and retrieve its type. –If the identifier is not in the type table, report a scope error.

7-8 Example: Fun scope checking  Declaration of a variable identifier:  Use of a variable identifier: VAR type ID ‘x’ expr ASSN expr ID ‘x’ put the identifier ‘x’ into the type table, along with the type. lookup the identifier ‘x’ in the type table, and retrieve its type.

7-9 Type checking (1)  Type checking is the process of checking that every command and expression is well-typed, i.e., free of type errors.  Note: The compiler performs type checking only if the source language is statically-typed.

7-10 Type checking (2)  At each expression, check the type of any sub- expression. Infer the type of the expression as a whole. –If a sub-expression has unexpected type, report a type error.  At each command, check the type of any constituent expression. –If an expression has unexpected type, report a type error.

7-11 Example: Fun type checking  Expression with binary operator:  Assignment-command: lookup ‘x’ and retrieve its type; walk expr and note its type; check that the two types are equivalent GT expr 1 expr 2 walk expr 1, and check that its type is INT; walk expr 2, and check that its type is INT; infer that the type of the whole expression is BOOL  If-command: walk expr, and check that its type is BOOL; walk com IF exprcom ASSN expr ID ‘x’

7-12 Contextual analysis with ANTLR (1)  In ANTLR we can write a “tree grammar” which describes the ASTs.  Each rule in the tree grammar is a pattern match for part of the AST.  From the tree grammar, ANTLR generates a depth-first left-to-right tree walker.  We can enhance the tree grammar with actions to perform scope and type checking. ANTLR will insert these actions into the tree walker.  Important: The position of an action determines when it will be performed during the tree walk.

7-13 Contextual analysis with ANTLR (2)  Examples of AST pattern matches: com :^(ASSN ID expr) |^(IF expr com) ; This pattern matches and makes expr refer to the right subtree. ASSN ID This pattern matches and makes expr and com refer to the left and right subtrees. IF

7-14 Case study: Fun tree grammar in ANTLR (1)  Fun tree grammar (outline): tree grammar FunChecker; options { tokenVocab = Fun; …; } prog :^(PROG var_decl* proc_decl+) ;

7-15 Case study: Fun tree grammar in ANTLR (2)  Fun tree grammar (continued): var_decl :^(VAR type ID expr) ; type :BOOL |INT ;

7-16 Case study: Fun tree grammar in ANTLR (3)  Fun tree grammar (continued): com :^(ASSN ID expr) |^(IF expr com) |^(SEQ com*) |… ;

7-17 Case study: Fun tree grammar in ANTLR (4)  Fun tree grammar (continued): expr :NUM |ID |^(EQ expr expr) |^(PLUS expr expr) |^(NOT expr) |… ;

7-18 Case study: Fun tree grammar with contextual analysis actions (1)  Fun tree grammar with actions (outline): tree grammar FunChecker; options { tokenVocab = Fun; …; { private SymbolTable typeTable; … } SymbolTable is a table that records identifiers with attributes of type A.

7-19 Case study: Fun tree grammar with contextual analysis actions (2)  Fun tree grammar with actions (continued): exprreturns [Type typ] :NUM { set $typ to INT; } |ID { lookup the identifier in type - Table, and let its type be t ; set $typ to t ; } |^(EQ t1=expr// check the left expr t2=expr// check the right expr ) { check that t1 and t2 are INT; set $typ to BOOL; } |…

7-20 Case study: Fun tree grammar with contextual analysis actions (3)  Fun tree grammar with actions (continued): |^(PLUS t1=expr// check the left expr t2=expr// check the right expr ){ check that t1 and t2 are INT; set $typ to INT; } |^(NOT t=expr// check the expr ){ check that t is BOOL; set $typ to BOOL; } |… ;

7-21 Case study: Fun tree grammar with contextual analysis actions (4)  Fun tree grammar with actions (continued): com :^(ASSN ID t=expr// check the expr ){ lookup the identifier in type - Table, and let its type be ti ; check that ti is t ; } |…

7-22 Case study: Fun tree grammar with contextual analysis actions (5)  Fun tree grammar with actions (continued): |^(IF t=expr// check the expr com// check the com ){ check that t is BOOL; } |^(SEQ com*// check the com* ) |… ;

7-23 Case study: Fun tree grammar with contextual analysis actions (6)  Fun tree grammar with actions (continued): var_decl :^(VAR t1=type ID t2=expr// check the expr ){ put the identifier into typeTable along with t1 ; check that t1 is t2 ; } ;

7-24 Case study: Fun tree grammar with contextual analysis actions (7)  Fun tree grammar with actions (continued): typereturns [Type typ] :BOOL{ set $typ to BOOL; } |INT{ set $typ to INT; } ;

7-25 Case study: Fun tree grammar with contextual analysis actions (8)  Fun tree grammar with actions (continued): prog :^(PROG { put ‘read’ and ‘write’ with their types into typeTable ; } var_decl*// check the var_decl* proc_decl+// check the proc_decl+ ){ check that ‘main’ is in typeTable and has type VOID → VOID; } ;

7-26 Case study: Fun syntactic and contextual analysers (1)  Put the above tree grammar in a file named FunChecker.g.  Feed this as input to ANTLR: …$ java org.antlr.Tool FunChecker.g  ANTLR generates a class FunChecker containing methods that walk the AST and perform the contextual analysis actions.

7-27 Case study: Fun syntactic and contextual analysers (2)  Program to run the Fun syntactic and contextual analysers: public class FunRun { public static void main (String[] args) { // Syntactic analysis: … CommonTree ast = (CommonTree) parser.prog().getTree(); // Contextual analysis: FunChecker checker = new FunChecker( new CommonTreeNodeStream(ast)); checker.prog(); } }

7-28 Representing types  To implement type checking, we need a way to represent the source language’s types.  We can use the concepts of §2: –primitive types –cartesian product types (T 1 × T 2 ) –disjoint union types (T 1 + T 2 ) –mapping types (T 1 → T 2 )

7-29 Case study: Fun types (1)  Represent Fun primitive data types by BOOL and INT.  Represent the type of each Fun function by a mapping type: func T ' f ( T x): …. T → T ' func T ' f (): …. VOID → T '  Similarly, represent the type of each Fun proper procedure by a mapping type: proc p ( T x): …. T → VOID proc p (): …. VOID → VOID

7-30 Case study: Fun types (2)  Represent the type of each Fun operator by a combination of product and mapping types: + - * / (INT × INT) → INT == (INT × INT) → BOOL not BOOL → BOOL

7-31 Case study: implementation of Fun types (1)  Outline of class Type : public abstract class Type { public abstract boolean equiv (Type t); public class Primitive extends Type { … } public class Pair extends Type { … } public class Mapping extends Type { … } }

7-32 Case study: implementation of Fun types (2)  Subclass Type.Primitive has a field that distinguishes different primitive types.  Class Type exports: public static final Type VOID = new Type.Primitive(0), BOOL = new Type.Primitive(1), INT = new Type.Primitive(2);

7-33 Case study: implementation of Fun types (2)  Subclass Type.Pair has two Type fields, which are the types of the pair components. E.g.: Type prod = new Type.Pair(Type.BOOL, Type.INT); represents BOOL × INT

7-34 Case study: implementation of Fun types (3)  Subclass Type.Mapping has two Type fields. These are the domain type and range type of the mapping type. E.g.: Type proctype = new Type.Mapping(Type.INT, Type.VOID); Type optype = new Type.Mapping( new Type.Pair(Type.INT, Type.INT), Type.BOOL); represents INT → VOID represents (INT × INT) → BOOL

7-35 Representing scopes (1)  Consider a PL in which all declarations are either global or local. Such a PL is said to have flat block structure (see §10).  The same identifier can be declared both globally and locally. E.g., in Fun: int x = 1 proc main (): int x = 2 write(x). proc p (bool x): if x: write(9).. writes 2 global variable local variable

7-36 Representing scopes (2)  The type table must distinguish between global and local entries.  Global entries are always present.  Local entries are present only when analysing an inner scope.  At any given point during analysis of the source program, the same identifier may occur in: –at most one global entry, and –at most one local entry.

7-37 Case study: Fun scopes (1)  Type table during contextual analysis of a Fun program: int x = 1 proc main (): int x = 2 write(x). proc p (bool x): if x: write(9).. global‘x’INT global local ‘x’INT ‘main’VOID → VOID ‘x’INT global local ‘x’INT ‘main’VOID → VOID ‘p’BOOL → VOID ‘x’BOOL

7-38 Case study: Fun scopes (2)  Such a table can be implemented by a pair of hash-tables, one for globals and one for locals: public class SymbolTable { // A SymbolTable object represents a scoped // table in which each entry consists of an identifier // and an attribute of type A. private HashMap globals, locals; public SymbolTable () { globals = new HashMap (); locals = null; // Initially there are no locals. }

7-39 Case study: Fun scopes (2)  Implementation in Java (continued): public void enterLocalScope () { locals = new HashMap (); } public void exitLocalScope () { locals = null; }

7-40 Case study: Fun scopes (3)  Implementation in Java (continued): public void put (String id, A attr) { … } // Add an entry ( id, attr ) to the locals (if not null), // otherwise add the entry to the globals. public A get (String id) { … } // Retrieve the attribute corresponding to id in // the locals (if any), otherwise retrieve it from // the globals. }  Now the type table can be declared thus: SymbolTable typeTable;

7-41 Case study: Fun scopes (4)  In the Fun tree grammar (simplified): proc_decl :^(PROC ID { enter local scope in typeTable ; } t=formal var_decl*// check the var_decl* com// check the com ){ exit local scope in typeTable ; put the identifier into typeTable with t → VOID; } |… ;