a two-dimensional separation of concerns for compiler construction carl (xiaoqing) wu, suman...

20
A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University of Alabama at Birmingham, USA Marjan Mernik University of Maribor, Slovenia

Upload: emily-mitchell

Post on 14-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

A Two-Dimensional Separation of Concernsfor Compiler Construction

Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray

The University of Alabama at Birmingham, USA

Marjan MernikUniversity of Maribor, Slovenia

Page 2: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Language Evolution

• During language evolution, compiler construction is usually performed along two dimensions:

• The Inheritance pattern and the Visitor pattern are used to improve modularity in each case

defining new abstract syntax tree (AST) nodes (Inheritance)

adding new semantic operations

(Visitor)

Page 3: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Inheritance Pattern (I)

• A pure object-oriented approach based on AST nodes– Declare an abstract super class for all AST nodes with virtual methods

for node operations inside the class.

– Define a class for each AST node and implement the methods that inherit from the super node class.

NodeTypeEval()ValueEval()PrettyPrint()

SumTypeEval()ValueEval()PrettyPrint()

DifferenceTypeEval()ValueEval()PrettyPrint()

Page 4: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Inheritance Pattern (II)

• Pros– Easy to extend the grammar. Any new symbols can

be added to the base grammar as a separated class

• Cons– The semantic operations crosscut the various other

class boundaries– Adding a new operation requires an invasive change

throughout the existing class hierarchy.

NodeTypeEval()ValueEval()PrettyPrint()

SumTypeEval()ValueEval()PrettyPrint()

DifferenceTypeEval()ValueEval()PrettyPrint()

NodeTypeEval()ValueEval()

SumTypeEval()ValueEval()

DifferenceTypeEval()ValueEval()

NodeTypeEval()ValueEval()

SumTypeEval()ValueEval()

ProductTypeEval()ValueEval()

DifferenceTypeEval()ValueEval()

×

Page 5: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Visitor Pattern (I)

• Semantics operation oriented– All the methods pertaining to one operation of the nodes are

encapsulated into a single visitor unit

– Visitor is separated with node classes and can be freely added or deleted

• Object-Oriented Implementation• Aspect-Oriented Implementation

Page 6: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Visitor Pattern (II)

• Object-Oriented VisitorNodeVisitorVisitSum()VisitProduct()

TypeEvalVisitorVisitSum()VisitProduct()

PrettyPrintVisitorVisitSum()VisitProduct()

NodeAccept(NodeVisitor)

SumNodeAccept(NodeVisitor)

ProductNodeAccept(NodeVisitor)

Program

• Aspect-Oriented Visitor

TypeEvalVisitorSum.typeEval()Product.typeEval()

PrettyPrintVisitorSum.prettyPrint()Product.prettyPrint()

Node

SumNode ProductNode

Program

Page 7: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Visitor Pattern (III)

• Pros– Adding new semantics

operations is easy

• Cons– Operations belong to

one AST node crosscut several visitor classes. Adding a new node to the existing class hierarchy will cause an invasive change to all of the visitors

TypeEvalVisitorSum.typeEval()Difference.typeEval()

PrettyPrintVisitorSum.prettyPrint()Difference.prettyPrint()

TypeEvalVisitorSum.typeEval()Difference.typeEval()Product.typeEval()

PrettyPrintVisitorSum.prettyPrint()Difference.prettyPrint()Product.prettyPrint()

TypeEvalVisitorSum.typeEval()Difference.typeEval()

PrettyPrintVisitorSum.prettyPrint()Difference.prettyPrint()

ValueEvalVisitorSum.prettyPrint()Difference.prettyPrint()

√×

Page 8: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Inheritance or Visitor

Page 9: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Compiler Matrix

Vertical modularization → each column = an class → Inheritance Pattern Horizontal modularization → each row = an aspect → Visitor Pattern

Node1 Node2 Node3 Node4

Operation1

Operation2

Operation3

Compiler Matrix

OBJECTS

AS

PE

CT

S

AST Nodes

Operations

Page 10: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Pattern Transformation

Class1 Class2

Aspect1

Aspect2

Class1 Class2

Aspect1

Aspect2

Visitor Pattern

Inheritance Pattern

Aspect Weaving

Aspect Unweaving

Class3

Class3

Page 11: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

A Simple Expression Language

• Syntax Grammarexpression ::= term | binary_expression

binary_expression ::= sum | difference

sum ::= expression ‘+’ expression

difference ::= expression ‘-’ expression

term ::= integer_literal | real_literal

• Semantics operationsValue evaluation

Type checking

Pretty print

Page 12: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Phase I: Build AST Nodes

Expression Term Sum Difference

class Sum implements Binary_expression, ASTNode{ public Expression expression1; public Expression expression2; public Sum (Expression expression1 , Expression expression2){ this.expression1 = expression1; this.expression2 = expression2; }; }

Page 13: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Phase II: Adding Semantics Operations

Expression Term Sum Difference

ValueEval

TypeEval

PrettyPrint

aspect ValueEval { public abstract Double ASTNode.valueEval(); public Double Real_literal.valueEval(){ return Double.valueOf(lexeme); }; public Double Integer_literal.valueEval(){ return Double.valueOf(lexeme); }; public Double Difference.valueEval(){ Double value1 = expression1.valueEval(); Double value2 = expression2.valueEval(); return new Double (value1.doubleValue()-value2.doubleVal

ue()); }; public Double Sum.valueEval(){ Double value1 = expression1.valueEval(); Double value2 = expression2.valueEval(); return new Double (value1.doubleValue()+value2.doubleVa

lue()); };}

Page 14: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Phase III: Extending Syntax Grammar

• expression ::= term | binary_expression | unary_expression

• binary-expression ::= sum | difference | quotient | product

• sum ::= expression ‘+’ expression• difference ::= expression ‘-’ expression• quotient ::= expression ‘/’ expression• product ::= expression ‘*’ expression• unary_expression ::= ‘-’ term• term ::= integer_literal | real_literal | parenthesized_ex

pression• parenthesized_expression ::= ‘(’expression ‘)’

Page 15: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Phase III: Extending Syntax Grammar

Expression Term Sum

ValueEval

TypeEval

PrettyPrint

. Difference

.

.

.

Product Quotient

class Sum implements Binary_expression, ASTNode{ public Expression expression1; public Expression expression2; public Sum (Expression expression1 , Expression expression2){ this.expression1 = expression1; this.expression2 = expression2; }; public Double valueEval() { Double value1 = expression1.valueEval(); Double value2 = expression2.valueEval(); return new Double(value1.doubleValue()+value2.doubleValue()); } //pretty print method //type checking method}

Page 16: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Aspect Weaving and Unweaving

Demo

Page 17: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

A General Example

• Payroll System

Executive Regular Contractor

Name

Wage

Print

Concern Matrix

CLASSES

ASPECTS

EmployeesFunctions

Page 18: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Future Work

• More complex programming language designs • Aspect weaving and unweaving

– Multiple visitor functions for one semantics operation of one class– Attributes of aspects

• The pattern transformation approach for other patterns– Observer– Mediator– Abstract Factory

Page 19: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Conclusion

• By exploring the essence of the compiler matrix, we developed an approach for compiler construction in two dimensions

– Based on pattern transformation – Using object-orientation and aspect-orientation

• The pattern transformation approach can be also utilized in other software system development and extended to other patterns – Multi-dimensional evolution needs exist in software development, no

single design principle or pattern offers a panacea toward addressing problems of change evolution.

– Transformation techniques applied to design patterns offer an alternative to alleviating this problem.

Page 20: A Two-Dimensional Separation of Concerns for Compiler Construction Carl (Xiaoqing) Wu, Suman Roychoudhury, Barrett R. Bryant and Jeffrey G. Gray The University

Questions?