Download presentation
Presentation is loading. Please wait.
Published byEmily Mitchell Modified over 9 years ago
1
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
2
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)
3
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.
4
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. √ ×
5
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
6
Visitor Pattern (II) Object-Oriented VisitorAspect-Oriented Visitor
7
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 √ ×
8
Inheritance or Visitor
9
Compiler Matrix Vertical modularization → each column = an class → Inheritance Pattern Horizontal modularization → each row = an aspect → Visitor Pattern
10
Pattern Transformation
11
A Simple Expression Language Syntax Grammar expression ::= term | binary_expression binary_expression ::= sum | difference sum ::= expression ‘+’ expression difference ::= expression ‘-’ expression term ::= integer_literal | real_literal Semantics operations Value evaluation Type checking Pretty print
12
Phase I: Build AST Nodes class Sum implements Binary_expression, ASTNode{ public Expression expression1; public Expression expression2; public Sum (Expression expression1, Expression expression2){ this.expression1 = expression1; this.expression2 = expression2; }; }
13
Phase II: Adding Semantics Operations 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.doubleValue()); }; public Double Sum.valueEval(){ Double value1 = expression1.valueEval(); Double value2 = expression2.valueEval(); return new Double (value1.doubleValue()+value2.doubleValue()); }; }
14
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_expression parenthesized_expression ::= ‘(’expression ‘)’
15
Phase III: Extending Syntax Grammar 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 }
16
Aspect Weaving and Unweaving Demo
17
A General Example Payroll System
18
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
19
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.
20
Questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.