1 Xiaoqing Wu, Barrett R. Bryant, Jeff Gray and Suman Roychoudhury University of Alabama at Birmingham Separation of Concerns in Compiler Development Using.

Slides:



Advertisements
Similar presentations
Testing Relational Database
Advertisements

Mohamed ElBendary and John Boyland University of Wisconsin-Milwaukee.
CPSC 388 – Compiler Design and Construction
Semantics Static semantics Dynamic semantics attribute grammars
Intermediate Code Generation
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Aspect Oriented Programming. AOP Contents 1 Overview 2 Terminology 3 The Problem 4 The Solution 4 Join point models 5 Implementation 6 Terminology Review.
Workshop: From Embedded Systems to Cyber-Physical Systems
1 JAC : Aspect Oriented Programming in Java An article review by Yuval Nir and Limor Lahiani.
Each design pattern is designed to facilitate one kind of change, i.e. changes in one dimension. However, software evolution can happen in multiple dimensions.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Software Testing and Quality Assurance
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
More on AspectJ. aspect MoveTracking { private static boolean _flag = false; public static boolean testAndClear() { boolean result = _flag; _flag = false;
Chapter 2 A Simple Compiler
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Introduction to Aspect- Oriented Programming CS 3360 Gregor Kiczales, et. al. Getting started with AspectJ, CACM, 44(10):59-65, October Fall 2012.
Outline Introduction Problem Statement Object-Oriented Design Aspect-Oriented Design Conclusion Demo.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Design-Making Projects Work (Chapter7) n Large Projects u Design often distinct from analysis or coding u Project takes weeks, months or years to create.
Java: Chapter 1 Computer Systems Computer Programming II.
Introduction to Aspect Oriented Programming Presented By: Kotaiah Choudary. Ravipati M.Tech IInd Year. School of Info. Tech.
Semantic Analysis (Generating An AST) CS 471 September 26, 2007.
An Examination of DSLs for Concisely Representing Model Traversals and Transformations Jeff Gray University of Alabama at Birmingham Gábor Karsai Vanderbilt.
Extensible Plug-ins for Aspect-Oriented Programming Macneil Shonle*Ankit Shah.
Institute for Software Integrated Systems Vanderbilt University Cyber Physical Systems: New Challenges for Model-based Design Janos Sztipanovits ISIS,
The Java Programming Language
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Methodology: The AOP Refactoring Process Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton Advisor:
Design Rules for Increasing Modularity with CaesarJ Carlos Eduardo Pontual Advisor: Paulo Borba 17/06/2010.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
An Approach for Supporting Aspect-Oriented Domain Modeling GPCE 2003 – Erfurt, Germany September 24, 2003 Jeff Gray, Ted Bapty, Sandeep Neema, Doug Schmidt,
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
11/25/2015© Hal Perkins & UW CSEH-1 CSE P 501 – Compilers Implementing ASTs (in Java) Hal Perkins Winter 2008.
A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University.
Inter-Type Declarations in AspectJ Awais Rashid Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
Weaving a Debugging Aspect into Domain-Specific Language Grammars SAC ’05 PSC Track Santa Fe, New Mexico USA March 17, 2005 Hui Wu, Jeff Gray, Marjan Mernik,
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
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.
Applying Aspect-Orientation in Designing Security Systems Shu Gao Florida International University Center for Advanced Distributed Systems Engineering.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Introduction to Aspect- Oriented Programming CS 3360 Gregor Kiczales, et. al. Getting started with AspectJ, CACM, 44(10):59-65, October Spring 2012.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
1 An AOP Implementation Framework for Extending Join Point Models Naoyasu Ubayashi(Kyushu Institute of Technology, Japan) Hidehiko Masuhara(University.
1 Aspectual Caml an Aspect-Oriented Functional Language Hideaki Tatsuzawa Hidehiko Masuhara Akinori Yonezawa University of Tokyo.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Adding Concurrency to a Programming Language Peter A. Buhr and Glen Ditchfield USENIX C++ Technical Conference, Portland, Oregon, U. S. A., August 1992.
A Simple Syntax-Directed Translator
Introduction to Parsing (adapted from CS 164 at Berkeley)
Java Primer 1: Types, Classes and Operators
Aspect-Oriented Programming
CS 3304 Comparative Languages
Zilong Ye, Ph.D. Cyber physical system Zilong Ye, Ph.D.
Presentation transcript:

1 Xiaoqing Wu, Barrett R. Bryant, Jeff Gray and Suman Roychoudhury University of Alabama at Birmingham Separation of Concerns in Compiler Development Using Aspect-Orientation Marjan Mernik University of Maribor

2 The Problem: Modularization of Compiler Phases package org.apache.tomcat.session; import org.apache.tomcat.core.*; import org.apache.tomcat.util.StringManager; import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; /** * Core implementation of a server session * James Duncan Davidson James Todd */ public class ServerSession { private StringManager sm = StringManager.getManager("org.apache.tomcat.session"); private Hashtable values = new Hashtable(); private Hashtable appSessions = new Hashtable(); private String id; private long creationTime = System.currentTimeMillis();; private long thisAccessTime = creationTime; private int inactiveInterval = -1; ServerSession(String id) { this.id = id; } public String getId() { return id; } public long getCreationTime() { return creationTime; } public ApplicationSession getApplicationSession(Context context, boolean create) { ApplicationSession appSession = (ApplicationSession)appSessions.get(context); if (appSession == null && create) { // XXX // sync to ensure valid? appSession = new ApplicationSession(id, this, context); appSessions.put(context, appSession); } // XXX // make sure that we haven't gone over the end of our // inactive interval -- if so, invalidate and create // a new appSession return appSession; } void removeApplicationSession(Context context) { appSessions.remove(context); } /** * Called by context when request comes in so that accesses and * inactivities can be dealt with accordingly. */ void validate() package org.apache.tomcat.session; import org.apache.tomcat.core.*; import org.apache.tomcat.util.StringManager; import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; /** * Core implementation of a server session * James Duncan Davidson James Todd */ public class ServerSession { private StringManager sm = StringManager.getManager("org.apache.tomcat.session"); private Hashtable values = new Hashtable(); private Hashtable appSessions = new Hashtable(); private String id; private long creationTime = System.currentTimeMillis();; private long thisAccessTime = creationTime; private int inactiveInterval = -1; ServerSession(String id) { this.id = id; } public String getId() { return id; } public long getCreationTime() { return creationTime; } public ApplicationSession getApplicationSession(Context context, boolean create) { ApplicationSession appSession = (ApplicationSession)appSessions.get(context); if (appSession == null && create) { // XXX // sync to ensure valid? appSession = new ApplicationSession(id, this, context); appSessions.put(context, appSession); } // XXX // make sure that we haven't gone over the end of our // inactive interval -- if so, invalidate and create // a new appSession return appSession; } void removeApplicationSession(Context context) { appSessions.remove(context); } /** * Called by context when request comes in so that accesses and * inactivities can be dealt with accordingly. */ void validate() package org.apache.tomcat.session; import org.apache.tomcat.core.*; import org.apache.tomcat.util.StringManager; import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; /** * Core implementation of a server session * James Duncan Davidson James Todd */ public class ServerSession { private StringManager sm = StringManager.getManager("org.apache.tomcat.session"); private Hashtable values = new Hashtable(); private Hashtable appSessions = new Hashtable(); private String id; private long creationTime = System.currentTimeMillis();; private long thisAccessTime = creationTime; private int inactiveInterval = -1; ServerSession(String id) { this.id = id; } public String getId() { return id; } public long getCreationTime() { return creationTime; } public ApplicationSession getApplicationSession(Context context, boolean create) { ApplicationSession appSession = (ApplicationSession)appSessions.get(context); if (appSession == null && create) { // XXX // sync to ensure valid? appSession = new ApplicationSession(id, this, context); appSessions.put(context, appSession); } // XXX // make sure that we haven't gone over the end of our // inactive interval -- if so, invalidate and create // a new appSession return appSession; } void removeApplicationSession(Context context) { appSessions.remove(context); } /** * Called by context when request comes in so that accesses and * inactivities can be dealt with accordingly. */ void validate() Compiler implementation is often an intricate task due to the complexity and interconnected nature of various stages within the compiler. Type checking static analysis Code generation

3 Problems 1. Code scatters all over the classes. 2. Hard to maintain and evolve. Pure Object-Oriented Design

4 The Goal: Separation of Concerns The key challenge is to provide exceptional modularity that assists in properly separating several crosscutting concerns, which not only helps the developer to divide-and-conquer the complexity, but also improves the readability, reusability and extensibility of the compiler implementation. Syntax analysis Type checking static analysis Code generation

5 Popular Solution: the Visitor Pattern Problems 1. Introduce a lot of extra code. 2. Forces all concrete visitors share the same interface. 3. New semantics are always introduced by traversal of the whole tree. 4. Cannot access private members of a node class.

6 New solution: Aspect-Oriented Programming Aspect-Oriented Programming provides special language constructs called aspects to modularize crosscutting concerns in conventional program structures. –Introduction (inter-type declarations) –Interception (join-points) AOP: handling crosscutting concerns Semantic phase: a concern that crosscuts various AST nodes AOP is an excellent programming technology to apply to semantic analysis

7 Aspect-Oriented Semantics Implementation Each concern is modularized as an aspect –An independent semantic pass –A group of action codes Semantic pass (i.e., a visitor) –Implemented as introductions of the AST classes Crosscutting actions applied to a group of nodes –Weaved into AST classes as interceptions.

8 Introduction

9 /* */ parser_code_part : := PARSER CODE CODE_STRING:u ser_code opt_semi {: if (emit.parser_code! =null) lexer.emit_error(" Redundant parser code (skipping)"); else /* save the user included code string */ emit.parser_code = user_code; :} ; /* */ init_code ::= INIT WITH CODE_STRING:u ser_code opt_semi {: if (emit.init_code!=n ull) lexer.emit_error(" Redundant init code (skipping)"); else /* save the user code */ emit.init_code = user_code; :} ; /* */ scan_code ::= SCAN WITH CODE_STRING:u ser_code opt_semi {: /* reset the accumulated multipart name */ multipart_name = new String(); :} SEMI ; /* */ declares_term ::= term_name_list {: /* reset the accumulated multipart name */ multipart_name = new String(); :} SEMI ; /* */ declares_non_term ::= non_term_name_list {: /* reset the accumulated multipart name */ multipart_name = new String(); :} SEMI ; /* */ term_name_list ::= term_name_list COMMA new_term_id | new_term_id; /* */ non_term_name_list ::= non_term_name_list COMMA new_non_term_id | new_non_term_id /* */ precedence_list ::= precedence_l | empty; /* */ precedence_l ::= precedence_l preced | preced; /* */ preced ::= PRECEDENCE LEFT {: update_precedence(assoc.left); :} terminal_list SEMI | PRECEDENCE RIGHT {: update_precedence(assoc.right); :} terminal_list SEMI | PRECEDENCE NONASSOC {: update_precedence(assoc.nonassoc); :} terminal_list SEMI if (emit.scan_code!=null) lexer.emit_error("Redundant scan code (skipping)"); else /* save the user code */ emit.scan_code = user_code; :}; /* */ symbol_list ::= symbol_list symbol | symbol; /* */ symbol ::= TERMINAL type_id declares_term TERMINAL declares_term | non_terminal type_id declares_non_term | non_terminal declares_non_term | /* error recovery productions -- sync on semicolon */ TERMINAL error {: /* reset the accumulated multipart name */ multipart_name = new String(); :} SEMI if (emit.parser_code!=null) lexer.emit_error("Redundant parser code (skipping)"); else /* save the user included code string */ emit.parser_code = user_code; if (emit.init_code!=nul) lexer.emit_error("Redundant init code (skipping)"); else /* save the user code */ emit.init_code = user_code; if (emit.scan_code!=null) lexer.emit_error("Redundant scan code (skipping)"); else /* save the user code */ emit.scan_code = user_code; /* reset the accumulated multipart name */ multipart_name = new String(); /* reset the accumulated multipart name */ multipart_name = new String(); /* reset the accumulated multipart name */ multipart_name = new String(); /* reset the accumulated multipart name */ multipart_name = new String(); update_precedence(assoc.left); update_precedence(assoc.right); :} {: update_precedence(assoc.nonassoc); /* */ parser_code_part ::= PARSER CODE CODE_STRING:user_code opt_semi {: :} ; /* */ init_code ::= INIT WITH CODE_STRING:user_code opt_semi {: :} ; /* */ scan_code ::= SCAN WITH CODE_STRING:user_code opt_semi {: :} ; /* */ symbol_list ::= symbol_list symbol | symbol; /* */ symbol ::= TERMINAL type_id declares_term | TERMINAL declares_term | non_terminal type_id declares_non_term | non_terminal declares_non_term | /* error recovery productions -- sync on semicolon */ TERMINAL error {: :} SEMI | non_terminal error {: :} SEMI ; /* */ declares_term ::= term_name_list {: :} SEMI ; /* */ declares_non_term ::= non_term_name_list {: :} SEMI ; /* */ term_name_list ::= term_name_list COMMA new_term_id | new_term_id; /* */ non_term_name_list :: = non_term_name_list COMMA new_non_term_id | new_non_term_id ; /* */ precedence_list ::= precedence_l | empty; /* */ precedence_l ::= precedence_l preced | preced; /* */ preced ::= PRECEDENCE LEFT {: :} terminal_list SEMI | PRECEDENCE RIGHT {: terminal_list SEMI | PRECEDENCE NONASSOC :} terminal_list SEMI ; parser.cup Interception Action.aj

10 Benefits ( I ) Aspect-orientation can isolate crosscutting semantic behavior in an explicit way. –Each semantic aspect can be freely attached to (generated) AST nodes without “polluting” the parser or AST node structure. –Different aspects can be selectively plugged in for different purposes at compile time. –Since each aspect is separated with other aspects, developers can always come back to the previous phase while developing a later phase.

11 Benefits ( II ) Inter-type declaration (Introduction) –Defined within class scope, direct access to AST node class members (include private members) –Preserve the object-oriented inheritance relationship. Join-point model (Interception) –Provides flexibility to insert semantic behaviors into AST nodes or parser –Avoid code duplication –Trace facility Introduction + Interception –Tree traversal –Phase combination

12 Inter-Type Declaration Example class UnparseVisitor extends Visitor{ protected PrintStream out = System.out; public Object print(Node node){ //... } // Other utility routines //... public Object visit(Node node){ return print(node); } public Object visit( ASTCompilationUnit node){ return print(node); } // same visit methods for another 83 nodes. //... } aspect Unparse{ protected static PrintStream out = System.out; public static void print(Node node){ //... } // other utility routines //... public void Node.unparse(){ Unparse.print(this); }

13 Join-Point Model Example pointcut scopeEvaluate(): target(ScopeNode+) && call (* *.evaluate()) ; before() : scopeEvaluate(){ } after() : scopeEvaluate(){ } symTabs.push(currentSymTab); SymbolTable tmp = currentSymTab; currentSymTab = new SymbolTable(); currentSymTab.parentScope = tmp; currentSymTab = (SymbolTable)symTabs.pop(); Executed each time entering a new scope Executed each time leaving a scope Occurred 46 times in a parser!

14 … // node is an instance of ScopeNode node.evaluate(); … before() : scopeEvaluate(){ symTabs.push(currentSymTab); SymbolTable tmp = currentSymTab; currentSymTab = new SymbolTable(); currentSymTab.parentScope = tmp; } after() : scopeEvaluate(){ currentSymTab = (SymbolTable)symTabs.pop(); } … // node is an instance of ScopeNode symTabs.push(currentSymTab); SymbolTable tmp = currentSymTab; currentSymTab = new SymbolTable(); currentSymTab.parentScope = tmp; node.evaluate(); currentSymTab = (SymbolTable)symTabs.pop(); … ASPECT WEAVING

15 Summary The major difficulty in compiler construction is separation of concerns. Traditional object-oriented design and the Visitor pattern cannot address the problem adequately. Using aspect-orientation to describe semantics supersedes both approaches without generating side- effects. Various AOP language features fulfill the computational needs of tree traversal. The proposed approach improves the overall modularization of the system and provides flexibility for future evolution of the compiler.

16 Thank you!