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

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.
1 Languages and Compilers (SProg og Oversættere) Code Generation.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Análise Sintática – Parte 2 Árvores Sintáticas Abstratas (ASTs) Scanning.
Representação do código objeto public class Instruction { public byte op; // op-code (0..15) public byte r; // register field (0..15) public byte n; //
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch. 3: Compilation Spring 2007 Marco Valtorta.
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.5: Contextual Analysis Spring 2007 Marco.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Languages and Compilers (SProg og Oversættere) Lecture 11 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.8: Interpretation Spring 2008 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.7: Code Generation Spring 2008 Marco Valtorta.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
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.5: Contextual Analysis Spring 2008 Marco.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Syntax Analysis (Chapter 4) 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.
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.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
7-1 7 Contextual analysis  Aspects of contextual analysis  Scope checking  Type checking  Case study: Fun contextual analyser  Representing types.
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.
Interpretation (Chapter 8) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
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.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Introduction (Chapter 1) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
5-1 5 Compilation  Overview  Compilation phases –syntactic analysis –contextual analysis –code generation  Abstract syntax trees  Case study: Fun language.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
/21 Clang Tutorial CS453 Automated Software Testing.
Runtime Organization (Chapter 6) 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.5: Contextual Analysis Spring 2013 Marco.
1 Languages and Compilers (SProg og Oversættere) Lecture 6 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
Contextual Analysis (Chapter 5) 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.
Interfaces and Inner Classes
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
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) Lecture 9 (1) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
Code Generation (Chapter 7) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
1 Languages and Compilers (SProg og Oversættere) Semantic Analysis.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
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.
Course Overview PART I: overview material PART II: inside a compiler
Enumeration Types In Triangle.
Languages and Compilers (SProg og Oversættere)
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
The fattest knight at King Arthur's round table was Sir Cumference
null, true, and false are also reserved.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Chapter 1: Computer Systems
Building Java Programs
Syntax-Directed Translation
Languages and Compilers (SProg og Oversættere)
Course Overview PART I: overview material PART II: inside a compiler
CSCE 531 Compiler Construction Ch.7: Code Generation
Languages and Compilers (SProg og Oversættere)
Course Overview PART I: overview material PART II: inside a compiler
Languages and Compilers (SProg og Oversættere)
Course Overview PART I: overview material PART II: inside a compiler
Languages and Compilers (SProg og Oversættere)
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 Traversal over the AST: Visitor Design Pattern Visitor Design Pattern? What’s that? A “visitor” is an often used “design trick” for defining an operation over a tree-like structure, without having to scatter the implementation of that operation all over the class tree. For example, printing an AST AST print() DeclarationExpression ConstDecl print() {…} VarDecl print() {…} SequentialDecl print() {…} Print code is all over the place => Solution: use a “visitor”

Contextual Analysis (Chapter 5) 3 Visitor Example: Printing with a visitor AST visit(Visitor) Expression ConstDecl visit(Visitor v) { v.visitConstDecl(this) } VarDecl visit(Visitor v) { v.visitVarDecl(this) } Visitor visitConstDecl(ConstDecl) visitVarDecl(VarDecl)... Declaration PrintVisitor visit All implementation code related to printing AST is now here!

Contextual Analysis (Chapter 5) 4 Traversal over the AST: Visitor Design Pattern public interface Visitor { // Programs public Object visitProgram(Program p, Object arg); // Commands public Object visitAssignCommand (AssignCommand c, Object arg); public Object visitCallCommand (CallCommand c, Object arg);... // Expressions public Object visitVnameExpression (VnameExpression e, Object arg); public Object visitUnaryExpression (UnaryExpression e, Object arg);... public interface Visitor { // Programs public Object visitProgram(Program p, Object arg); // Commands public Object visitAssignCommand (AssignCommand c, Object arg); public Object visitCallCommand (CallCommand c, Object arg);... // Expressions public Object visitVnameExpression (VnameExpression e, Object arg); public Object visitUnaryExpression (UnaryExpression e, Object arg);... For passing extra arguments/info to a traversal Traversal may compute a value

Contextual Analysis (Chapter 5) 5 Traversal over the AST: Visitor Design Pattern public abstract class AST {... public abstract Object visit(Visitor v, Object arg); } public abstract class AST {... public abstract Object visit(Visitor v, Object arg); } public class AssignCommand extends AST {... public Object visit(Visitor v, Object arg) { return v.visitAssignCommand(this,arg); } public class IfCommand extends AST {... public Object visit(Visitor v, Object arg) { return v.visitIfCommand(this,arg); } public class AssignCommand extends AST {... public Object visit(Visitor v, Object arg) { return v.visitAssignCommand(this,arg); } public class IfCommand extends AST {... public Object visit(Visitor v, Object arg) { return v.visitIfCommand(this,arg); } In every concrete AST class add:

Contextual Analysis (Chapter 5) 6 Example: Mini-Triangle Contextual Analyzer public class Type { private byte kind; // BOOL, INT, or ERROR public static final byte BOOL=0, INT=1, ERROR= -1; private Type(byte kind) {... } public boolean equals(Object other) {... } public static Type boolT = new Type(BOOL); public static Type intT = new Type(INT); public static Type errorT = new Type(ERROR); } public class Type { private byte kind; // BOOL, INT, or ERROR public static final byte BOOL=0, INT=1, ERROR= -1; private Type(byte kind) {... } public boolean equals(Object other) {... } public static Type boolT = new Type(BOOL); public static Type intT = new Type(INT); public static Type errorT = new Type(ERROR); } Mini Triangle Types

Contextual Analysis (Chapter 5) 7 Example: Mini-Triangle Contextual Analyzer public class Checker implements Visitor { private IdentificationTable idTable; public void check(Program prog) { idTable = new IdentificationTable( ); // initialize with standard environment idTable.enter(“false”,...);... idTable.enter(“putint”,...); prog.visit(this, null); }... public class Checker implements Visitor { private IdentificationTable idTable; public void check(Program prog) { idTable = new IdentificationTable( ); // initialize with standard environment idTable.enter(“false”,...);... idTable.enter(“putint”,...); prog.visit(this, null); }... Contextual Analyzer as an AST visitor Checker is a traversal of AST Start AST traversal with this checker

Contextual Analysis (Chapter 5) 8 What the Checker Visitor Does visitProgramCheck whether program is well-formed and return null. visit…CommandCheck whether the command is well-formed and return null. visit…ExpressionCheck expression, decorate it with its type, and return that type. visitSimpleVNameCheck whether name is declared. Decorate it with its type, and indicate whether it is a variable or constant. Return its type. visit…DeclarationCheck that declaration is well-formed. Enter declared identifier into ID table. Return null. visitSimpleTypeDenCheck that type denoter is well-formed. Decorate with its type. Return the type. visitIdentifierCheck whether identifier is declared. Decorate with link to its declaration. Return declaration.

Contextual Analysis (Chapter 5) 9 Example: Mini-Triangle Contextual Analyzer public class Checker implements Visitor {... // check commands public Object visitAssignCommand (AssignCommand com, Object arg) { Type vType = (Type) com.V.visit(this, null); Type eType = (Type) com.E.visit(this, null); if (! com.V.variable) report error: v is not a variable (cannot assign to a constant) if (! eType.equals(vType) ) report error: incompatible types in assignCommand return null; }... public class Checker implements Visitor {... // check commands public Object visitAssignCommand (AssignCommand com, Object arg) { Type vType = (Type) com.V.visit(this, null); Type eType = (Type) com.E.visit(this, null); if (! com.V.variable) report error: v is not a variable (cannot assign to a constant) if (! eType.equals(vType) ) report error: incompatible types in assignCommand return null; }...

Contextual Analysis (Chapter 5) 10 Example: Mini-Triangle Contextual Analyzer... public Object visitIfCommand (IfCommand com, Object arg) { Type eType = (Type) com.E.visit(this, null); if (! eType.equals(Type.boolT) ) report error: expression in ifCommand is not boolean com.C1.visit(this, null); com.C2.visit(this, null); return null; }... public Object visitIfCommand (IfCommand com, Object arg) { Type eType = (Type) com.E.visit(this, null); if (! eType.equals(Type.boolT) ) report error: expression in ifCommand is not boolean com.C1.visit(this, null); com.C2.visit(this, null); return null; }...

Contextual Analysis (Chapter 5) 11 Example: Mini-Triangle Contextual Analyzer... public Object visitSequentialCommand (SequentialCommand com, Object arg) { com.C1.visit(this, null); com.C2.visit(this, null); return null; } public Object visitLetCommand (LetCommand com, Object arg) { idTable.openScope( ); com.D.visit(this, null); // enters declarations into idTable com.C.visit(this, null); idTable.closeScope( ); return null; }... public Object visitSequentialCommand (SequentialCommand com, Object arg) { com.C1.visit(this, null); com.C2.visit(this, null); return null; } public Object visitLetCommand (LetCommand com, Object arg) { idTable.openScope( ); com.D.visit(this, null); // enters declarations into idTable com.C.visit(this, null); idTable.closeScope( ); return null; }...

Contextual Analysis (Chapter 5) 12 Example: Mini-Triangle Contextual Analyzer // check expressions public Object visitIntegerExpression (IntegerExpression expr, Object arg) { expr.type = Type.intT; // decoration return expr.type; } public Object visitVnameExpression (VnameExpression expr, Object arg) { Type vType = (Type) expr.V.visit(this, null); expr.type = vType; // decoration return expr.type; } // check expressions public Object visitIntegerExpression (IntegerExpression expr, Object arg) { expr.type = Type.intT; // decoration return expr.type; } public Object visitVnameExpression (VnameExpression expr, Object arg) { Type vType = (Type) expr.V.visit(this, null); expr.type = vType; // decoration return expr.type; }

Contextual Analysis (Chapter 5) 13 Example: Mini-Triangle Contextual Analyzer public Object visitBinaryExpression (BinaryExpression expr, Object arg) { Type e1Type = expr.E1.visit(this, null); Type e2Type = expr.E2.visit(this, null); OperatorDeclaration opdecl = (OperatorDeclaration) expr.O.visit(this, null); if (opdecl==null) { // error: operator not defined expr.type = Type.error; } else if (opdecl instanceof BinaryOperatorDecl) { // check binary operator, see details on next slide } else { // error: operator not binary expr.type = Type.errorT; } return expr.type; } public Object visitBinaryExpression (BinaryExpression expr, Object arg) { Type e1Type = expr.E1.visit(this, null); Type e2Type = expr.E2.visit(this, null); OperatorDeclaration opdecl = (OperatorDeclaration) expr.O.visit(this, null); if (opdecl==null) { // error: operator not defined expr.type = Type.error; } else if (opdecl instanceof BinaryOperatorDecl) { // check binary operator, see details on next slide } else { // error: operator not binary expr.type = Type.errorT; } return expr.type; }

Contextual Analysis (Chapter 5) 14 Example: Mini-Triangle Contextual Analyzer public Object visitBinaryExpression (BinaryExpression expr, Object arg) {... } else if (opdecl instanceof BinaryOperatorDecl) { BinaryOperatorDecl bopdecl = (BinaryOperatorDecl) opdecl; if (! e1Type.equals(bopdecl.operand1Type) ) // error: first argument wrong type if (! e2Type.equals(bopdecl.operand2Type) ) // error: second argument wrong type expr.type = bopdecl.resultType; } else { // error: operator not binary... } return expr.type; } public Object visitBinaryExpression (BinaryExpression expr, Object arg) {... } else if (opdecl instanceof BinaryOperatorDecl) { BinaryOperatorDecl bopdecl = (BinaryOperatorDecl) opdecl; if (! e1Type.equals(bopdecl.operand1Type) ) // error: first argument wrong type if (! e2Type.equals(bopdecl.operand2Type) ) // error: second argument wrong type expr.type = bopdecl.resultType; } else { // error: operator not binary... } return expr.type; }

Contextual Analysis (Chapter 5) 15 Example: Mini-Triangle Contextual Analyzer // check declarations public Object visitVarDeclaration (VarDeclaration decl, Object arg) { decl.T.visit(this, null); idTable.enter(decl.I.spelling, decl); return null; } public Object visitConstDeclaration (ConstDeclaration decl, Object arg) { decl.E.visit(this, null); idTable.enter(decl.I.spelling, decl); return null; }... // check declarations public Object visitVarDeclaration (VarDeclaration decl, Object arg) { decl.T.visit(this, null); idTable.enter(decl.I.spelling, decl); return null; } public Object visitConstDeclaration (ConstDeclaration decl, Object arg) { decl.E.visit(this, null); idTable.enter(decl.I.spelling, decl); return null; }...

Contextual Analysis (Chapter 5) 16 Example: Mini-Triangle Contextual Analyzer // check type denoters public Object visitSimpleTypeDenoter (SimpleTypeDenoter den, Object arg) { if (den.I.spelling.equals(“Integer”)) den.type = Type.intT; else if (den.I.spelling.equals(“Boolean”)) den.type = Type.boolT; else { // error: unknown type denoter den.type = Type.errorT; } return den.type; }... // check type denoters public Object visitSimpleTypeDenoter (SimpleTypeDenoter den, Object arg) { if (den.I.spelling.equals(“Integer”)) den.type = Type.intT; else if (den.I.spelling.equals(“Boolean”)) den.type = Type.boolT; else { // error: unknown type denoter den.type = Type.errorT; } return den.type; }...

Contextual Analysis (Chapter 5) 17 Example: Mini-Triangle Contextual Analyzer // check VNames public Object visitSimpleVName (SimpleVname vname, Object arg) { Declaration decl = vname.I.visit(this, null); if (decl==null) { // error: VName not declared vname = Type.errorT; vname.variable = true; } else if (decl instanceof ConstDeclaration) { vname.type = ((ConstDeclaration) decl).E.type); vname.variable = false; } else if (decl instanceof VarDeclaration) { vname.type = ((VarDeclaration) decl).T.type); vname.variable = true; } return vname.type; } // check VNames public Object visitSimpleVName (SimpleVname vname, Object arg) { Declaration decl = vname.I.visit(this, null); if (decl==null) { // error: VName not declared vname = Type.errorT; vname.variable = true; } else if (decl instanceof ConstDeclaration) { vname.type = ((ConstDeclaration) decl).E.type); vname.variable = false; } else if (decl instanceof VarDeclaration) { vname.type = ((VarDeclaration) decl).T.type); vname.variable = true; } return vname.type; }

Contextual Analysis (Chapter 5) 18 Example: Mini-Triangle Contextual Analyzer... // check applied occurrences of Identifiers public Object visitIdentifier (Identifier id, Object arg) { id.decl = idTable.retrieve(id.spelling); return id.decl; }... // check applied occurrences of Identifiers public Object visitIdentifier (Identifier id, Object arg) { id.decl = idTable.retrieve(id.spelling); return id.decl; } Note: the above method is only called with applied identifiers. (Previous code to check declarations does not visit those identifiers.)