a two-dimensional separation of concerns for compiler construction carl (xiaoqing) wu, suman...
TRANSCRIPT
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
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)
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()
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()
√
×
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
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
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()
√×
Inheritance or Visitor
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
Pattern Transformation
Class1 Class2
Aspect1
Aspect2
Class1 Class2
Aspect1
Aspect2
Visitor Pattern
Inheritance Pattern
Aspect Weaving
Aspect Unweaving
Class3
Class3
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
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; }; }
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()); };}
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 ‘)’
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}
Aspect Weaving and Unweaving
Demo
A General Example
• Payroll System
Executive Regular Contractor
Name
Wage
Concern Matrix
CLASSES
ASPECTS
EmployeesFunctions
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
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.
Questions?