Visitors. Move functionality
|
|
- Maximilian Alexander
- 5 years ago
- Views:
Transcription
1 Visitors Compiler Construction Visitor pattern, Semantic analysis Lennart Andersson How to modularize in Java (or any other OO language) if we do not have access to AOP mechanisms? Revision Compiler Construction 2011 F07-1 Example How can we factor out the code for print()? class Add extends Expr { Expr expr1, expr2; void print() { expr1.print(); System.out.print( + ); expr2.print(); class IntExpr extends Expr { int value; void print() { System.out.print(value); Compiler Construction 2011 F07-2 Move functionality class Add extends Expr { class Visitor { Expr expr1, expr2; void visit(add node) { void print() { node.expr1.print(); expr1.print(); System.out.print( + ); System.out.print( + ); node.expr2.print(); expr2.print(); class IntExpr extends Expr { void visit(intexpr node){ int value; System.out.print( void print() { node.value); System.out.print(value); Compiler Construction 2011 F07-3 Compiler Construction 2011 F07-4
2 Missing functionality Delegate class Add extends Expr { class Visitor { Expr expr1, expr2; void visit(add node) { node.expr1.print(); System.out.print( + ); node.expr2.print(); class IntExpr extends Expr { int value; void visit(intexpr node){ System.out.print( node.value); class Add extends Expr { class Visitor { Expr expr1, expr2; void visit(add node) { void accept(visitor v) { node.expr1.accept(this); v.visit(this); System.out.print( + ); node.expr2.accept(this); class IntExpr extends Expr { void visit(intexpr node){ int value; System.out.print( void accept(visitor v) { node.value); v.visit(this); Compiler Construction 2011 F07-5 Why not call visit directly? Compiler Construction 2011 F07-6 Generalise class Add extends Expr { class Visitor { Expr expr1, expr2; void visit(add node) { void accept(visitor v) { visit(node.expr1); v.visit(this); System.out.print( + ); visit(node.expr2); class IntExpr extends Expr { void visit(intexpr node){ int value; System.out.print( void accept(visitor v) { node.value); v.visit(this); class Add extends Expr { interface Visitor { Expr expr1, expr2; void visit(add node); void accept(visitor v) { void visit(intexpr node); v.visit(this); class PrintVisitor implements Visitor { class IntExpr extends Expr { void visit(add node) { int value; node.expr1.accept(this); void accept(visitor v) { System.out.print( + ); v.visit(this); node.expr1.accept(this); Compiler Construction 2011 F07-7 Compiler Construction 2011 F07-8
3 Generalise with parameter and return interface Visitor { Object visit(add node, Object data); Object visit(intexpr node, Object data); class IntExpr extends Expr { Expr expr1, expr2; Object accept(visitor v, Object data) { return v.visit(this, data); class PrintVisitor implements Visitor { Object visit(intexpr node, Object data) { System.out.print(node.value); return null; Compiler Construction 2011 F07-9 The Visitor Pattern Intent Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. Another visitor class ValueVisitor implements Visitor { Object visit(add node, Object data) { int n1 = (Integer) node.expr1.accept(this, data); int n2 = (Integer) node.expr2.accept(this, data); return new Integer(n1+n2); Object visit(intexpr node, Object data) { return new Integer(node.value); Sketch Expr expr = new Add( ); expr.accept(new PrintVisitor(), null); int value = expr.accept(new ValueVisitor(), null); Compiler Construction 2011 F07-10 Expr Sub class Add accept() each AST class has an Object accept(visitor, Object) method ValueVistor.java delegates computation class ValueVisitor implements Visitor { Object visit(add, Object) { Object visit(sub, Object) { Visitor.java interface Visitor { Object visit(add, Object); Object visit(sub, Object); //one visit method for each AST class UnparseVisitor.java class UnparseVisitor implements Visitor { Object visit(add, Object) { Object visit(sub, Object) { Compiler Construction 2011 F07-11 Compiler Construction 2011 F07-12
4 Interface Visitor interface Visitor { Object visit(add node, Object data); Object visit(sub node, Object data); Object visit(intexpr node, Object data); The visit method is overloaded for different AST argument types Each method returns an untyped object Each method has an untyped argument (data) Compiler Construction 2011 F07-13 One more example Count the number of identifiers in a program abstract Stmt; IfStmt : Stmt ::= Cond:Expr Then:Stmt [Else:Stmt]; abstract Expr; abstract BinExpr : Expr ::= Left:Expr Right:Expr; Add : BinExpr ::= ; Sub : BinExpr ::= ; Int : Expr ::= <INT:String>; IdExpr : Expr ::= <ID:String>; How can the Visitor be implemented? Compiler Construction 2011 F07-15 Visitor support in AST nodes Method accept that delegates the computation to a Visitor abstract class Expr { abstract Object accept(visitor v, Object data); abstract class BinExpr extends Expr { Object accept(visitor v, Object data) { return v.visit(this, data); class Add extends BinExpr { Object accept(visitor v, Object data) { return v.visit(this, data); class IntExpr extends Expr { Object accept(visitor v, Object data) { return v.visit(this, data); Compiler Construction 2011 F07-14 TraversingVisitor class TraversingVisitor implements Visitor { Object visit(ifstmt node, Object data) { node.getcond().accept(this, data); node.getthen().accept(this, data); if (node.haselse()) { node.getelse().accept(this, data); Object visit(add node, Object data) { node.getleft().accept(this, data); node.getright().accept(this, data); The code above is independent of the computation and could have been generated from the abstract grammar (but this is currently not done in JJTree or JastAdd). Compiler Construction 2011 F07-16
5 CountIdentifiers as a visitor Intertype declarations vs. Visitor class CountIdentifiers extends TraversingVisitor { Example of use types for arguments and return values separate compilation? pure Java? declar- intertype tions what can be modularized? instance variables, methods, implements clauses arbitrary no preprocessor required no requires additional tools Visitor only methods Object visit(, Object) (one untyped argument and one result) yes yes Compiler Construction 2011 F07-17 Using the modularization techniques Compiler Construction 2011 F07-18 Semantic analysis Interpretation Unparsing Metrics Semantic Analysis Name analysis connect an identifier to its declaration Type analysis? compute the type of an expression... Code generation Compute the size needed for objects and methods Generate instructions... Name analysis bind each identifier to the appropriate declaration Type analysis compute the type for each expression Error checking Is the identifier declared? Is the expression of a legal type? Does the procedure call have the correct number of arguments? Compiler Construction 2011 F07-19 Compiler Construction 2011 F07-20
6 Name analysis Program example Which identifiers are IdDecls? Which are IdUses? We introduce two different AST classes for identifiers: IdDecl a declared occurrence an identifier that names a declaration IdUse an applied occurrence an identifier that refers to a declaration Name analysis Bind each IdUse to the IdDecl to which it refers. class GraphicalObject { Position pos; Position getpos() { return pos; class Circle extends GraphicalObject { float radius; float area() { return Math.PI*radius*radius; Compiler Construction 2011 F07-21 Scope (synlighetsområde) Block Compiler Construction 2011 F07-22 The scope of a declaration the parts of the program where the name of the declaration is visible Block a syntactic unit with declarations and statements may require memory allocation during execution (once or several times) Block structure (nesting) a block can have inner blocks (recursively) declarations in a block are visible also in the inner blocks Example of block structure an anonymous block inside a method a method inside a class a method inside another method a class inside another class Compiler Construction 2011 F07-23 Compiler Construction 2011 F07-24
7 Special blocks Scope rules (visibility rules) Govern how IdUses are bound to IdDecls Global declarations can be viewed as belonging to an outermost block Static fields A global block can be created for each class to hold its static fields Typical factors (differ in different languages) combination how can blocks be combined? name collisions what happens if the same name is declared in many blocks? declaration order does it affect the bindings? method overloading can there be several methods of the same name, but with different argument types? What are the binding rules? parameters how do they relate to local variables? return values are they named explicitly? visibility restrictions private, public,... qualified access access via another name Compiler Construction 2011 F07-25 How can blocks be combined? Compiler Construction 2011 F07-26 Name collisions Block structure declarations in an outer block are visible also in an inner block Inheritance declarations in a class are visible also in subclasses Combined block structure and inheritance e.g., a method in a subclass can access instance variables in a superclass Shadowing (skuggning) inner declarations shadow outer declarations of the same name {int x; {int x; Forbidden shadowing Some languages prohibit inner blocks to declare a name that is already present in an outer block. Compiler Construction 2011 F07-27 Compiler Construction 2011 F07-28
8 Declaration order Other name issues Homogeneous blocks the order between the declarations is irrelevant Declarations in Java classes All declarations in Algol Declare-before-use a name must be declared before it is used Declarations in Java methods Declarations in C Declarations in Pascal Can the same name be used for declarations of different kind? E.g., fields and methods in Java: int c, int c() what about class c? Do all names share the same lexical definition? or are there different kinds of identifiers? In Smalltalk, attributes must start with a lower case letter, classes with an upper case letter. In Java it is just a convention that class names should start with a capital letter. Compiler Construction 2011 F07-29 Overloaded method names Overloaded method The same method name Different signatures (method signature name, parameter types, return type) Bind to the method with the most specific signature (with respect to the static types) void visit(expr node) { void visit(add node) { Expr e = new Add(); visit(e); which method is called? e.accept(visitor); which method is called? Compiler Construction 2011 F07-31 Compiler Construction 2011 F07-30 Parameters Usually, parameters can be seen as special local variables void m(int x, y) { int s = 2; x = s + y; Usually, it is an error to declare a local variable with the same name void m(int x, y) { int x; // Multiple declaration of x Compiler Construction 2011 F07-32
9 Return value Visibility modifiers The value can be returned by a special return statement (C, Java, ) int m() { return 3; The value can be returned by using the function name as a variable (Algol, Pascal, ) int m() { m = 3; Explicit modifiers private, protected, public (Java) hidden, protected (Simula) friend, (C++) Default rules what rules hold for classes/methods/fields without modifiers? Compiler Construction 2011 F07-33 Qualified access Compiler Construction 2011 F07-34 AST based name analysis Indirect access via another name circle.area() circle.getpos(); Bindings Represent the binding as a reference variable in the IdUses Algorithm Traverse the AST Keep track of visible declarations in a symbol table Look up the declaration for each IdUse Symbol table maps names to declarations a stack can be used to handle block structure Compiler Construction 2011 F07-35 Compiler Construction 2011 F07-36
10 Bindnings List(Decl*) VarDecl Program Block List(Stmt*) Assignment IntType IdDecl x IdUse x IntConst 3 Distinguish between identifier uses and declarations in the AST Replace IdExpr by IdDecl declared identifier occurrence IdUse applied identifier occurrence (has a reference to an IdDecl node) Compiler Construction 2011 F07-37 Properties of this simple language declarations appear before statements (cannot be mixed like in C or Java) no IdUses inside the declaration part blocks can be nested begin int x; x = 3; end; begin int x; int y; x = 3; begin int z; z = x + 5; y = z + 1; end; end; Compiler Construction 2011 F07-39 Example Program ::= Block; begin Block ::= Decl* Stmt*; int x; abstract Decl; x = 3; VarDecl: Decl ::= Type IdDecl; end; abstract Type; IntType: Type ::=; IdDecl ::= <ID>; begin abstract Stmt; int x; AssignStmt: Stmt ::= IdUse Expr; int y; BlockStmt: Stmt ::= Block; x = 3; abstract Expr; begin IdUse: Expr ::= <ID>; int z; IntConst: Expr ::= <INT>; z = x + 5; Add: Expr ::= Left:Expr Right:Expr; y = z + 1; end; end; Compiler Construction 2011 F07-38 Implementation of name analysis aspect NameAnalysis { void Program.nameAnalysis() { SymbolTable table = new SymbolTable(); nameanalysis(table); void ASTNode.nameAnalysis(SymbolTable table) { for (k=0; k<getnumchild(); k++) { getchild(k).nameanalysis(table); void Block.nameAnalysis(SymbolTable table) { table.enterblock(); getdecllist().adddecls(table); getstmtlist().nameanalysis(table); table.exitblock(); Compiler Construction 2011 F07-40
11 ... Implementation of name analysis void ASTNode.addDecls(SymbolTable table) { for (k=0; k<getnumchild(); k++) { getchild(k).adddecls(table); void IdDecl.addDecls(SymbolTable table) { table.add(getid(), this); Implementation of the symbol table a stack of hashmaps (one hashmap for each block) a new hashmap is pushed when entering a block a hashmap is popped when exiting a block the symbol table is empty after the name analysis IdDecl IdUse.decl; void IdUse.nameAnalysis(SymbolTable table) { decl = table.lookup(getid()); Compiler Construction 2011 F07-41 Compiler Construction 2011 F07-42 Symbol table API Constructor summary SymbolTable() Method summary void add(string symbol, Object meaning) Adds the symbol and its associated meaning to the top table boolean alreadydeclared(string symbol) Returns true if the symbol is already in the top table int blocklevel() Returns the current block level (= the number of dictionaries on the stack) void enterblock() Adds a new table to the stack void exitblock() Removes the top table from the stack Object lookup(string symbol) Returns the meaning of symbol Compiler Construction 2011 F07-43 Compiler Construction 2011 F07-44
12 Implementation of SymbolTable public class SymbolTable { private class LinkedHashMap extends HashMap<String, Object>() { LinkedHashMap next; LinkedHashMap(LinkedHashMap next) { this.next = next; Object lookup(string symbol) { Object result = get(symbol); if (result!= null next == null) return result; else return next.lookup(symbol); Compiler Construction 2011 F07-45 Variations Implementation of SymbolTable public class SymbolTable { private LinkedHashMap top = null; public void add(string symbol, Object meaning) { top.put(symbol, meaning); public Object lookup(string symbol) { return top.lookup(symbol); public void enterblock() { top = new LinkedHashMap(top); public void exitblock() { top = top.next; Compiler Construction 2011 F07-46 The declaration part could contain IdUses, e.g., variables with initial values names of user defined types (e.g., classes and interfaces) The declaration part could contain blocks, e.g., methods, inner classes Mixed declarations and statements as in C and Java Order between the declarations Decl-before-use like in C and Java methods Compiler Construction 2011 F07-47
Semantic analysis. Compiler Construction. An expression evaluator. Examples of computations. Computations on ASTs Aspect oriented programming
Semantic analysis Compiler Construction source code scanner semantic analysis Computations on ASTs Aspect oriented programming tokens AST with attributes Lennart Andersson parser code generation Revision
More information.jj file with actions
Hand-coded parser without actions Compiler Construction Computations on ASTs Lennart Andersson Revision 2011-02-07 2011 void stmt() { switch(token) { case IF: accept(if); expr(); accept(then); stmt();
More informationLL(k) Compiler Construction. Choice points in EBNF grammar. Left recursive grammar
LL(k) Compiler Construction More LL parsing Abstract syntax trees Lennart Andersson Revision 2012 01 31 2012 Related names top-down the parse tree is constructed top-down recursive descent if it is implemented
More informationLL(k) Compiler Construction. Top-down Parsing. LL(1) parsing engine. LL engine ID, $ S 0 E 1 T 2 3
LL(k) Compiler Construction More LL parsing Abstract syntax trees Lennart Andersson Revision 2011 01 31 2010 Related names top-down the parse tree is constructed top-down recursive descent if it is implemented
More informationEDA180: Compiler Construc6on. More Top- Down Parsing Abstract Syntax Trees Görel Hedin Revised:
EDA180: Compiler Construc6on More Top- Down Parsing Abstract Syntax Trees Görel Hedin Revised: 2013-02- 05 Compiler phases and program representa6ons source code Lexical analysis (scanning) Intermediate
More informationProf. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan
Language Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Semantic Analysis Compiler Architecture Front End Back End Source language Scanner (lexical analysis)
More informationStatic Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1
Static Semantics Lecture 15 (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Current Status Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing
More informationCSE 307: Principles of Programming Languages
1 / 26 CSE 307: Principles of Programming Languages Names, Scopes, and Bindings R. Sekar 2 / 26 Topics Bindings 1. Bindings Bindings: Names and Attributes Names are a fundamental abstraction in languages
More informationSyntax Errors; Static Semantics
Dealing with Syntax Errors Syntax Errors; Static Semantics Lecture 14 (from notes by R. Bodik) One purpose of the parser is to filter out errors that show up in parsing Later stages should not have to
More informationThe Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.
The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees
More informationA Short Summary of Javali
A Short Summary of Javali October 15, 2015 1 Introduction Javali is a simple language based on ideas found in languages like C++ or Java. Its purpose is to serve as the source language for a simple compiler
More informationCS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 17: Types and Type-Checking 25 Feb 08 CS 412/413 Spring 2008 Introduction to Compilers 1 What Are Types? Types describe the values possibly
More informationInformal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility
Informal Semantics of Data semantic specification names (identifiers) attributes binding declarations scope rules visibility 1 Ways to Specify Semantics Standards Documents (Language Definition) Language
More informationCompiler Passes. Semantic Analysis. Semantic Analysis/Checking. Symbol Tables. An Example
Semantic Analysis Having figured out the program s structure, now figure out what it means Compiler Passes Analysis of input program (front-end) character stream Lexical Analysis token stream Syntactic
More informationLecture 16: Static Semantics Overview 1
Lecture 16: Static Semantics Overview 1 Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing Produces trees Detects & eliminates ill-formed parse trees Static semantic analysis
More informationSemantic Analysis. Compiler Architecture
Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Source Compiler Architecture Front End Scanner (lexical tokens Parser (syntax Parse tree Semantic Analysis
More informationScope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule
Scope CSC 4181 Compiler Construction Scope and Symbol Table A scope is a textual region of the program in which a (name-to-object) binding is active. There are two types of scope: Static scope Dynamic
More informationProgrammiersprachen (Programming Languages)
2016-05-13 Preface Programmiersprachen (Programming Languages) coordinates: lecturer: web: usable for: requirements: No. 185.208, VU, 3 ECTS Franz Puntigam http://www.complang.tuwien.ac.at/franz/ps.html
More informationInformatica 3 Syntax and Semantics
Informatica 3 Syntax and Semantics Marcello Restelli 9/15/07 Laurea in Ingegneria Informatica Politecnico di Milano Introduction Introduction to the concepts of syntax and semantics Binding Variables Routines
More informationCompiler construction
Compiler construction Martin Steffen March 13, 2017 Contents 1 Abstract 1 1.1 Symbol tables. 1 1.1.1 Introduction 1 1.1.2 Symbol table design and interface.. 2 1.1.3 Implementing symbol tables 3 1.1.4
More informationTail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)
CMSC 330: Organization of Programming Languages Tail Calls A tail call is a function call that is the last thing a function does before it returns let add x y = x + y let f z = add z z (* tail call *)
More informationSEMANTIC ANALYSIS TYPES AND DECLARATIONS
SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether
More informationDeclarations and Access Control SCJP tips
Declarations and Access Control www.techfaq360.com SCJP tips Write code that declares, constructs, and initializes arrays of any base type using any of the permitted forms both for declaration and for
More informationToday's Topics. CISC 458 Winter J.R. Cordy
Today's Topics Last Time Semantics - the meaning of program structures Stack model of expression evaluation, the Expression Stack (ES) Stack model of automatic storage, the Run Stack (RS) Today Managing
More informationCSCE 314 Programming Languages. Type System
CSCE 314 Programming Languages Type System Dr. Hyunyoung Lee 1 Names Names refer to different kinds of entities in programs, such as variables, functions, classes, templates, modules,.... Names can be
More informationThe Decaf Language. 1 Lexical considerations
The Decaf Language In this course, we will write a compiler for a simple object-oriented programming language called Decaf. Decaf is a strongly-typed, object-oriented language with support for inheritance
More informationLecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4
CS412/413 Introduction to Compilers and Translators Spring 00 Outline Typechecking Symbol tables Using symbol tables for analysis Lecture 8: Semantic Analysis and Symbol Tables Lecture 8 CS 412/413 Spring
More informationLecture 7: Type Systems and Symbol Tables. CS 540 George Mason University
Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing
More informationExamination in Compilers, EDAN65
Examination in Compilers, EDAN65 Department of Computer Science, Lund University 2016 10 28, 08.00-13.00 Note! Your exam will be marked only if you have completed all six programming lab assignments in
More informationImplementing Subprograms
Implementing Subprograms 1 Topics The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing Subprograms with Stack-Dynamic Local Variables Nested Subprograms Blocks Implementing
More informationThe compilation process is driven by the syntactic structure of the program as discovered by the parser
Semantic Analysis The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on its syntactic structure
More informationFundamental Concepts and Definitions
Fundamental Concepts and Definitions Identifier / Symbol / Name These terms are synonymous: they refer to the name given to a programming component. Classes, variables, functions, and methods are the most
More informationIC Language Specification
CS 301 Spring 2016 IC Language Specification The IC Language For the implementation project, you will build a compiler for an object-oriented language called IC (for Irish Coffee 1 ), which is essentially
More informationCSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1
CSE P 501 Compilers Implementing ASTs (in Java) Hal Perkins Autumn 2009 10/20/2009 2002-09 Hal Perkins & UW CSE H-1 Agenda Representing ASTs as Java objects Parser actions Operations on ASTs Modularity
More informationJastAdd an aspect-oriented compiler construction system
JastAdd an aspect-oriented compiler construction system Görel Hedin Eva Magnusson Department of Computer Science, Lund University, Sweden Abstract We describe JastAdd, a Java-based system for compiler
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationThe Decaf language 1
The Decaf language 1 In this course, we will write a compiler for a simple object-oriented programming language called Decaf. Decaf is a strongly-typed, object-oriented language with support for inheritance
More informationCS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline
CS 0 Lecture 8 Chapter 5 Louden Outline The symbol table Static scoping vs dynamic scoping Symbol table Dictionary associates names to attributes In general: hash tables, tree and lists (assignment ) can
More informationTopics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited
CMSC 330: Organization of Programming Languages Type Systems, Names & Binding Topics Covered Thus Far Programming languages Syntax specification Regular expressions Context free grammars Implementation
More informationCSCI312 Principles of Programming Languages!
CSCI312 Principles of Programming Languages! Scope Xu Liu ! 4.1 Syntactic Issues! 4.2 Variables! 4.3 Scope! 4.4 Symbol Table! 4.5 Resolving References! 4.6 Dynamic Scoping! 4.7 Visibility! 4.8 Overloading!
More informationCSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1
CSE P 501 Compilers Static Semantics Hal Perkins Winter 2008 1/22/2008 2002-08 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Attribute grammars Representing types Symbol tables Note: this covers
More informationCPS 506 Comparative Programming Languages. Programming Language
CPS 506 Comparative Programming Languages Object-Oriented Oriented Programming Language Paradigm Introduction Topics Object-Oriented Programming Design Issues for Object-Oriented Oriented Languages Support
More informationCSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1
CSE P 501 Compilers Implementing ASTs (in Java) Hal Perkins Winter 2008 1/22/2008 2002-08 Hal Perkins & UW CSE H-1 Agenda Representing ASTs as Java objects Parser actions Operations on ASTs Modularity
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Type Systems, Names and Binding CMSC 330 - Spring 2013 1 Topics Covered Thus Far! Programming languages Ruby OCaml! Syntax specification Regular expressions
More informationAnatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?
Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator
More information1 Lexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler
More informationSemantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 13: Types and Type-Checking 19 Feb 07 Semantic Analysis Last time: Semantic errors related to scopes Symbol tables Name resolution This lecture:
More informationSemantic Analysis and Type Checking
Semantic Analysis and Type Checking The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on
More informationCS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find
CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS
More informationCA Compiler Construction
CA4003 - Compiler Construction Semantic Analysis David Sinclair Semantic Actions A compiler has to do more than just recognise if a sequence of characters forms a valid sentence in the language. It must
More information10. Abstract Data Types
10. Abstract Data Types 11.1 The Concept of Abstraction The concept of abstraction is fundamental in programming Nearly all programming languages support process abstraction with subprograms Nearly all
More informationSemantic Analysis Wilhelm/Seidl/Hack: Compiler Design Syntactic and Semantic Analysis, Chapter 4
Semantic Analysis Wilhelm/Seidl/Hack: Compiler Design Syntactic and Semantic Analysis, Chapter 4 Reinhard Wilhelm Universität des Saarlandes wilhelm@cs.uni-sb.de Standard Structure source(text) lexical
More informationProject 2 Interpreter for Snail. 2 The Snail Programming Language
CSCI 2400 Models of Computation Project 2 Interpreter for Snail 1 Overview In this assignment you will use the parser generator yacc to construct an interpreter for a language called Snail containing the
More information3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.
3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.
More informationObject-oriented Programming. Object-oriented Programming
2014-06-13 Object-oriented Programming Object-oriented Programming 2014-06-13 Object-oriented Programming 1 Object-oriented Languages object-based: language that supports objects class-based: language
More informationChapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures
Chapter 5: Procedural abstraction Proper procedures and function procedures Abstraction in programming enables distinction: What a program unit does How a program unit works This enables separation of
More informationG Programming Languages - Fall 2012
G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level
More informationOutline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring
Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,
More informationDecaf Language Reference Manual
Decaf Language Reference Manual C. R. Ramakrishnan Department of Computer Science SUNY at Stony Brook Stony Brook, NY 11794-4400 cram@cs.stonybrook.edu February 12, 2012 Decaf is a small object oriented
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationCS143 Handout 03 Summer 2012 June 27, 2012 Decaf Specification
CS143 Handout 03 Summer 2012 June 27, 2012 Decaf Specification Written by Julie Zelenski and updated by Jerry Cain and Keith Schwarz. In this course, we will write a compiler for a simple object oriented
More informationThe SPL Programming Language Reference Manual
The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming
More informationMASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I
Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY 6.035 Fall 2014 Test I You have 50 minutes to finish this quiz. Write your name and athena username on this
More informationCPS2000 Compiler Theory & Practice
CPS2000 Compiler Theory & Practice Notes on Handcrafting a Parser Gordon Mangion Source File Compiler Lexical Analyser Keyword Table Abstract Syntax Tree Parser Symbol Table? Error Module? Abstract Syntax
More informationProgramming Languages Third Edition. Chapter 7 Basic Semantics
Programming Languages Third Edition Chapter 7 Basic Semantics Objectives Understand attributes, binding, and semantic functions Understand declarations, blocks, and scope Learn how to construct a symbol
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationSyntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax
Susan Eggers 1 CSE 401 Syntax Analysis/Parsing Context-free grammars (CFG s) Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax
More informationCSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:
Expression evaluation CSE 504 Order of evaluation For the abstract syntax tree + + 5 Expression Evaluation, Runtime Environments + + x 3 2 4 the equivalent expression is (x + 3) + (2 + 4) + 5 1 2 (. Contd
More informationLexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a
More informationSemantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far
Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Statically vs. Dynamically typed languages
More informationLecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction
Lecture 13: Object orientation Object oriented programming Introduction, types of OO languages Key concepts: Encapsulation, Inheritance, Dynamic binding & polymorphism Other design issues Smalltalk OO
More informationRSL Reference Manual
RSL Reference Manual Part No.: Date: April 6, 1990 Original Authors: Klaus Havelund, Anne Haxthausen Copyright c 1990 Computer Resources International A/S This document is issued on a restricted basis
More informationContext-free grammars (CFG s)
Syntax Analysis/Parsing Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax tree (AST) AST: captures hierarchical structure of
More informationMid-Term 2 Grades
Mid-Term 2 Grades 100 46 1 HW 9 Homework 9, in untyped class interpreter: Add instanceof Restrict field access to local class Implement overloading (based on argument count) Due date is the same as for
More informationAcknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.
Acknowledgement CS3300 - Compiler Design Intro to Semantic Analysis V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this
More informationSemantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End
Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors
More informationLecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.
Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)
More informationCSE 401/M501 Compilers
CSE 401/M501 Compilers ASTs, Modularity, and the Visitor Pattern Hal Perkins Autumn 2018 UW CSE 401/M501 Autumn 2018 H-1 Agenda Today: AST operations: modularity and encapsulation Visitor pattern: basic
More informationThe role of semantic analysis in a compiler
Semantic Analysis Outline The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors
More informationOBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++ Abstract Data Types and Encapsulation Concepts ISBN 0-321-33025-0 Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract
More informationObject Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.
CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java
More informationLexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler
More informationBuilding Compilers with Phoenix
Building Compilers with Phoenix Parser Generators: ANTLR History of ANTLR ANother Tool for Language Recognition Terence Parr's dissertation: Obtaining Practical Variants of LL(k) and LR(k) for k > 1 PCCTS:
More informationStatic Semantics. Winter /3/ Hal Perkins & UW CSE I-1
CSE 401 Compilers Static Semantics Hal Perkins Winter 2009 2/3/2009 2002-09 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Symbol tables General ideas for now; details later for MiniJava project
More informationWeiss Chapter 1 terminology (parenthesized numbers are page numbers)
Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement
More informationToday s lecture. CS 314 fall 01 C++ 1, page 1
Today s lecture Midterm Thursday, October 25, 6:10-7:30pm general information, conflicts Object oriented programming Abstract data types (ADT) Object oriented design C++ classes CS 314 fall 01 C++ 1, page
More informationThe PCAT Programming Language Reference Manual
The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language
More informationObject-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1
Object-Oriented Languages and Object-Oriented Design Ghezzi&Jazayeri: OO Languages 1 What is an OO language? In Ada and Modula 2 one can define objects encapsulate a data structure and relevant operations
More informationConcepts Introduced in Chapter 7
Concepts Introduced in Chapter 7 Storage Allocation Strategies Static Stack Heap Activation Records Access to Nonlocal Names Access links followed by Fig. 7.1 EECS 665 Compiler Construction 1 Activation
More informationInheritance. Transitivity
Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance Inheritance The property that instances of a sub-class can access both data and behavior associated
More informationCompiler construction
Compiler construction Martin Steffen March 13, 2017 Contents 1 Abstract 1 1.1 Types and type checking....................................... 1 1.1.1 Intro...............................................
More informationCSE 12 Abstract Syntax Trees
CSE 12 Abstract Syntax Trees Compilers and Interpreters Parse Trees and Abstract Syntax Trees (AST's) Creating and Evaluating AST's The Table ADT and Symbol Tables 16 Using Algorithms and Data Structures
More informationSemantic actions for declarations and expressions
Semantic actions for declarations and expressions Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate
More informationCompilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam
Compilers Type checking Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Summary of parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser:
More informationTopics Covered Thus Far CMSC 330: Organization of Programming Languages
Topics Covered Thus Far CMSC 330: Organization of Programming Languages Names & Binding, Type Systems Programming languages Ruby Ocaml Lambda calculus Syntax specification Regular expressions Context free
More informationCompilers CS S-05 Semantic Analysis
Compilers CS414-2003S-05 Semantic Analysis David Galles Department of Computer Science University of San Francisco 05-0: Syntax Errors/Semantic Errors A program has syntax errors if it cannot be generated
More informationEDAN65: Compilers, Lecture 08 Reference A<ribute Grammars AG mechanisms, SemanBc analysis. Görel Hedin Revised:
EDAN65: Compilers, Lecture 08 Reference A
More informationClass, Variable, Constructor, Object, Method Questions
Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx
More informationContext Analysis. Mooly Sagiv. html://www.cs.tau.ac.il/~msagiv/courses/wcc11-12.html
Context Analysis Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc11-12.html 1 Short Decaf Program Interface not declared class MyClass implements MyInterface { string myinteger; void dosomething()
More informationChapter 11. Abstract Data Types and Encapsulation Concepts
Chapter 11 Abstract Data Types and Encapsulation Concepts The Concept of Abstraction An abstraction is a view or representation of an entity that includes only the most significant attributes The concept
More information