[antlr-interest] Re: Translators Should Use Tree Grammars
Anakreon
anakreonmejdi at yahoo.gr
Mon Nov 22 03:24:01 PST 2004
> Yea, I suppose a treewalker is fine when the output is so very
> close to just a printing of the inorder traversal of the AST.
> But I'm still not convinced that a treewalker is the best
> for very complex AST-to-AST transformations.
>
> Andy
In my opinion treewalker is capable for complex transformations.
For example, in ASPA a Jscript class declaration is transformed
into a PHP class declaration.
See this code for example:
function getWidth() {
return this.width;
}
function setWidth(w) {
this.width = w;
}
function getDimension() {
return Math.pow(this.width, 2);
}
function Rectangle() {
this.pos = new Point(0, 0);
this.width = 10;
this.attr = aa;
this.getDimension = getDimension;
this.getWidth = getWidth
this.setWidth = setWidth;
this.getPosition = getPosition
this.setPosition = setPosition;
this.incPos = function() {
this.pos.x++;
this.pos.y++;
}
this.toString = function() {
return "[" + this.pos.x
+ ", " + this.pos.y
+ ", " + (this.pos.x + this.width)
+ ", " + (this.pos.y + this.width) + "]";
}
}
function getPosition() {
return this.pos;
}
function setPosition(x, y) {
this.pos.setX(x);
this.pos.setY(y);
}
function Point(x, y) {
this.x = x;
this.y = y;
this.setX = _X;
this.setY = _Y
}
function _X(x) {
this.x = x;
}
function _Y(y) {
this.y = y;
}
which declares a class Rectangle and Point
The transformed code in PHP looks like:
class Rectangle {
var $width;
var $pos;
var $attr;
function Rectangle() {
global $aa;
$this->pos = new Point(0, 0);
$this->width = 10;
$this->attr = $aa;
}
function getDimension() {
return pow($this->width, 2);
}
function getWidth() {
return $this->width;
}
function toString() {
return "[" . $this->pos->$x . ", " . $this->pos->$y . ", " . ($this->pos->$x + $this->width)
. ", " . ($this->pos->$y + $this->width) . "]";
}
function setWidth($w) {
$this->width = $w;
}
function setPosition($x, $y) {
$this->pos->setX($x);
$this->pos->setY($y);
}
function incPos() {
$this->pos->$x++;
$this->pos->$y++;
}
function getPosition() {
return $this->pos;
}
}
class Point {
var $y;
var $x;
function Point($x, $y) {
$this->x = $x;
$this->y = $y;
}
function setY($y) {
$this->y = $y;
}
function setX($x) {
$this->x = $x;
}
}
The transformations involved are complex and are handled with the treewalker
Antrl generates.
The code generation is handled by a TreeParser which recognizes the generated AST of the
translator(AST of PHP) and a class of 171 lines (including the header which declares that
the source is GPL).
Personally I found no limitations in the TreeParser approach although I believe that the
transformations involved are quite complex.
Anakreon
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list